The easiest way to implement this is a generator function:
Use with AIfrom __future__ import division
def linspace(start, stop, n):
if n == 1:
yield stop
return
h = (stop - start) / (n - 1)
for i in range(n):
yield start + h * i
Example usage:
Use with AI>>> list(linspace(1, 3, 5))
[1.0, 1.5, 2.0, 2.5, 3.0]
That said, you should definitely consider using NumPy, which provides the numpy.linspace() function and lots of other features to conveniently work with numerical arrays.
The easiest way to implement this is a generator function:
Use with AIfrom __future__ import division
def linspace(start, stop, n):
if n == 1:
yield stop
return
h = (stop - start) / (n - 1)
for i in range(n):
yield start + h * i
Example usage:
Use with AI>>> list(linspace(1, 3, 5))
[1.0, 1.5, 2.0, 2.5, 3.0]
That said, you should definitely consider using NumPy, which provides the numpy.linspace() function and lots of other features to conveniently work with numerical arrays.
No, it doesn't. You can write your own (whicn isn't difficult), but if you are using Python to fulfil some of matlab's functionality then you definitely want to install numpy, which has numpy.linspace.
You may find NumPy for Matlab users informative.
Since your functions are differentiable, you could use the Newton-Raphson method implemented by scipy.optimize:
>>> scipy.optimize.newton(lambda x: 1.5*(1-math.exp(-x))-x, 10)
0.8742174657987283
Computing the error is very straightforward:
>>> def f(x): return 1.5*(1-math.exp(-x))
...
>>> x = scipy.optimize.newton(lambda x: f(x)-x, 10)
>>> error = f(x) - x
>>> x, error
(0.8742174657987283, -4.218847493575595e-15)
I've somewhat arbitrarily chosen x0=10 as the starting point. Some care needs to be take here to make sure the method doesn't converge to x=0, which in your example is also a root.
I'm not a mathematician so perhaps you can explain me sth here, but I don't understand what exactly you mean by "unsolvable analytically".
That's what sympy returns:
from sympy import *
x = symbols('x')
a = 1.5
y1 = x
y2 = a*(1 - exp(-x))
print(solve(y1-y2))
# [0.874217465798717]
Does pandas contain a similar api to do a numpy.linspace?
I want to use it but I don’t really want to add another dependency to the script
Given numpy, you could use linspace:
Including the right endpoint (5):
In [46]: import numpy as np
In [47]: np.linspace(0,5,10)
Out[47]:
array([ 0. , 0.55555556, 1.11111111, 1.66666667, 2.22222222,
2.77777778, 3.33333333, 3.88888889, 4.44444444, 5. ])
Excluding the right endpoint:
In [48]: np.linspace(0,5,10,endpoint=False)
Out[48]: array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])
You can use the following approach:
[lower + x*(upper-lower)/length for x in range(length)]
lower and/or upper must be assigned as floats for this approach to work.
I have to do a lot of plotting and I always make it such that if I have to vary some parameter everything else adjusts accordingly, but sometimes when I change parameters there's issue with the number of elements in lists which prevents me from plotting along with a whole bunch of other things. (right now I'm using a:0.01:b)
So how do I get a list of n evenly spaced elements from a to b like in
np.linspace(a,b,n)
???