[SciPy-user] Getting the set of parameters from a leastsq function at each iteration.

Doreen Mbabazi doreen@aims.ac...
Fri Apr 4 12:01:49 CDT 2008

```Hi,

I am still working at my code and I have not yet been able to get optimal
parameters. The problem being that actually my fitting function which is a
system of odes is evaluated at only one set of parameters(the initial)
thus the final results I get is the initial set of parameters. I need to
find a way to be able to find integrate the system of odes at each
iteration as the set of parameters change. In other words, Is there a way
to get the parameters at each iteration while using the leastsq function?
If that is possible how can I integrate my system of odes using these sets
of parameters at each iteration? Some help please.
Code is below.

from scipy import *
from scipy.integrate import odeint
from scipy.optimize import leastsq
import scipy.io.array_import
import Gnuplot,Gnuplot.funcutils
gp=Gnuplot.Gnuplot(persist=1)  # , debug=1

def residuals(p,V,t):
err = V - S(t,p)
return err

#y[0]=T, y[1]=T*, y[2] = V, lamda = p[0], d = p[1], k=p[2], delta=p[3], pi
= p[4], c = p[5]
initial_y = [10,0,10e-6] # initial conditions T(0)= 10cells , T*(0)=0,
V(0)=10e-6

filename=('essay1.dat')  # data file is stored

t = data[:,0]
V = data[:,1]

pname = (['lamda','d','k','delta','pi','c'])
lamda_0 = 0.1; d_0 = 0.01;  k_0 = 0.60e-3;  delta_0 = 0.35;   pi_0 = 800;
c_0 = 3.0
p = array([lamda_0 , d_0, k_0, delta_0,pi_0,c_0])

def f(y,t,p):
y_dot = [0.,0.,0.]
y_dot[0] = p[0] - p[1]*y[0] - p[2]*y[0]*y[2]
y_dot[1] = p[2]*y[0]*y[2] - p[3]*y[1]
y_dot[2] = p[4]*y[1] - p[5]*y[2]
return y_dot

y = odeint(f,initial_y,t,args=(p,))

def S(t,p):
v = y[:,2]
return v

pbest = leastsq(residuals, p, args=(V,t),maxfev=2000)
print pbest[0]

Doreen.

Rob Clewley
> Hi Doreen,
>
>>  def residuals(p,y,t):
>>     return [V - S(t,p) for i in xrange(len(t))]
>>
>
> Minpack expects an array type to be returned. You're returning a
> python list. So try this:
>
> def residuals(p,y,t):
>     return array([V - S(t_val,p) for t_val in t])
>
> Here I've also removed the unnecessary i index variable -- Python lets
> you iterate directly over the contents of the list t.
>
> -Rob
>
> --
> Robert H. Clewley, Ph. D.
> Assistant Professor
> Department of Mathematics and Statistics
> Georgia State University
> 720 COE, 30 Pryor St
> Atlanta, GA 30303, USA
>
> tel: 404-413-6420 fax: 404-651-2246
> http://www.mathstat.gsu.edu/~matrhc
> http://brainsbehavior.gsu.edu/
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>

```