[SciPy-User] Fitting Differential Equations to a Curve

Pauli Virtanen pav@iki...
Tue Feb 7 13:04:00 CST 2012


07.02.2012 11:43, Pappu Kumar kirjoitti:
> I am trying to fit the differential equation ay' + by''=0 to a curve by
> varying a and b The following code does not work:
> 
> from scipy.integrate import odeint
> from scipy.optimize import curve_fit
> from numpy import linspace, random, array
> 
> time = linspace(0.0,10.0,100)
> def deriv(time,a,b):
>     dy=lambda y,t : array([ y[1], a*y[0]+b*y[1] ])
>     yinit = array([0.0005,0.2]) # initial values
>     Y=odeint(dy,yinit,time)
>     return Y[:,0]
> 
> z = deriv(time, 2, 0.1)
> zn = z + 0.1*random.normal(size=len(time))
> 
> popt, pcov = curve_fit(deriv, time, zn)
> print popt # it only outputs the initial values of a, b!

For me, this prints
[ 1.999963    0.10002353]

So seems to be working as expected, as the function to fit was made with
parameters [2, 0.1] plus some added noise.

-- 
Pauli Virtanen



More information about the SciPy-User mailing list