OptimizationAndFitDemo1 - SciPy wiki dump (original) (raw)
This is a quick example of creating data from several Bessel functions and finding local maxima, then fitting a curve using some spline functions from the scipy.interpolate module. The enthought.chaco package and wxpython are used for creating the plot. PyCrust (which comes with wxpython) was used as the python shell.
1 from enthought.chaco.wx import plt
2 from scipy import arange, optimize, special
3
4 plt.figure()
5 plt.hold()
6 w = []
7 z = []
8 x = arange(0,10,.01)
9
10 for k in arange(1,5,.5):
11 y = special.jv(k,x)
12 plt.plot(x,y)
13 f = lambda x: -special.jv(k,x)
14 x_max = optimize.fminbound(f,0,6)
15 w.append(x_max)
16 z.append(special.jv(k,x_max))
17
18 plt.plot(w,z, 'ro')
19 from scipy import interpolate
20 t = interpolate.splrep(w, z, k=3)
21 s_fit3 = interpolate.splev(x,t)
22 plt.plot(x,s_fit3, 'g-')
23 t5 = interpolate.splrep(w, z, k=5)
24 s_fit5 = interpolate.splev(x,t5)
25 plt.plot(x,s_fit5, 'y-')
#class left
inline:chacoscreenshot.png Optimization Example
SciPy: Cookbook/OptimizationAndFitDemo1 (last edited 2015-10-24 17:48:26 by anonymous)