scipy
package has been chosen for these applications since it has proven
very robust.
fsolve
, that takes
two input arguments. The first argument is the function for which you
are seeking the roots, and the second argument is a value at which you
want fsolve
to start its search for a root. Searching
for a root for the function starting at would thus look like
this:
from scipy.optimize import fsolve f = lambda x: (x-2)*(x+1) fsolve(f,3)
array([2.])
fsolve(f,-2)
array([-1.])
x1 = fsolve(f,3) x2 = fsolve(f,-2) x3 = fsolve(f,0) (x1, x2, x3)
(array([2.]), array([-1.]), array([-1.]))
[float(x) for x in (x1, x2, x3)]
, thereby getting:
[2.0, -1.0, -1.0]
.
The two values, and , are the -values for which gives zero. If we plot
the entire function and indicate those two place with points, and , one can
see that these -values are indeed the ones for which is zero:
from scipy.optimize import fsolve import numpy as np f = lambda x : np.cos(x) - x fsolve(f,0)
array([0.73908513])
scipy.optimize
's fmin
function like this:
import scipy.optimize import numpy as np f = lambda x: 1/10*x**2 + np.cos(x)**2 xmin = scipy.optimize.fmin(func=f, x0=1) xmin,f(xmin)
Optimization terminated successfully. Current function value: 0.224167 Iterations: 14 Function evaluations: 28 Out[1]: (array([1.42617188]), array([0.22416743]))
x0=1
instructs fmin
to start the search near .
[float(a) for a in [xmin, f(xmin)]]
[1.426171875000001, 0.22416743026027897]
quad
from the scipy.integrate
module.
quad
takes three arguments:
from scipy.integrate import quad f = lambda x: pow(x,3) quad(f, 0, 2)
(4.0, 4.440892098500626e-14)
4.0
is the result
and 4.440892098500626e-14
is an estimate of
the absolute error in the result.
quad(f, 0, 2)[0]
4.0
.
from scipy.integrate import quad dxdtheta = lambda theta: -np.sin(theta) dydtheta = lambda theta: np.cos(theta) quad(lambda theta: np.sqrt(dxdtheta(theta)**2 + dydtheta(theta)**2),0,2*np.pi)[0]
6.283185307179586
which is the numerical representation of .
quad
for different values of :
f = lambda x: 1/np.sqrt(2*np.pi) * np.exp(-x**2/2) quad(f,-0.5,0.5)
(0.3829249225480263, 4.251320657156236e-15)
quad(f,-0.75,0.75)[0] # the [0] is added so that the output becomes the value of the integral
0.5467452952462637
fsolve
:
import numpy as np from scipy.integrate import quad from scipy.optimize import fsolve f = lambda x: 1/np.sqrt(2*np.pi) * np.exp(-x**2/2) x0sol = fsolve(lambda x0: 0.5 - quad(f,-x0,x0)[0],1)[0] print('x0sol:',x0sol)
x0sol: 0.6744897501960817
f = lambda x,sigma: 1/np.sqrt(2*np.pi)/sigma * np.exp(-np.power(x,2)/2/np.power(sigma,2))
print('For sigma=1 ',quad(lambda x: f(x,1),-1,1)[0]) print('For sigma=1.25',quad(lambda x: f(x,1.25),-1,1)[0]) print('For sigma=1.5 ',quad(lambda x: f(x,1.5),-1,1)[0])
[0]
is selecting the first value in the tuple provided by quad
).
For sigma=1 0.682689492137086 For sigma=1.25 0.5762892028332066 For sigma=1.5 0.4950149249061543
fsolve
:
f = lambda x,sigma: 1/np.sqrt(2*np.pi)/sigma * np.exp(-np.power(x,2)/2/np.power(sigma,2)) sigma_sol = fsolve(lambda sigma: 0.5 - quad(lambda x: f(x,sigma),-1,1)[0],1)[0] sigma_sol
1.4826022185056023
quad(lambda x: f(x,sigma_sol),-1,1)
(0.4999999999999999, 5.551115123125781e-15)
Choose which booklet to go to: