Integration using scipy odeint
Laura Creighton
lac at openend.se
Sun Sep 13 20:24:03 CEST 2015
In a message of Sun, 13 Sep 2015 07:49:37 -0700, sagar k writes:
>Dear all
>
>I'm using Python 3.4.3. I am facing a problem in integrating using odeint solver. In the following code, tran is a function and in those are the variables which are arrays. These variables change their values with respect to time (the time which I pass to the function). I tried defining a loop inside the function, but Python throws an error saying float object not iterable. Please guide me how to solve this problem.
>
>#initial is an array containing initial values for the integration, dt is the time array with unequal time steps, which I have captured from other part of the code
>
>def tran(initial,dt):
> for i in dt:
> k1 [i] = 2.8e8 * math.exp(-21100/ta[i]) # k1,k2,k3 & k4 are constants which change according to temperature (ta), which in turn changes with respect to time.
> k2 [i] = 1.4e2 * math.exp(-12100/ta[i]) # ta changes its values according to dt, which is array. It runs from 0-6 with unequal time steps.
> k3 [i] = 1.4e5 * ta[i] * math.exp(-19680/ta[i]) # I've captured dt and all other arrays here from another loop, which is not of importtance.
> k4 [i] = 1.484e3 * ta[i]
> y = initial[0]
> z = initial[1]
> dn = (6*rho*initial[1])
> dd = (math.pi*2000*initial[0])
> ds = (dn/dd)**1/3
> dydt = (166.2072*ta[i]*cca[i] - (1.447e13 * ta[i]**0.5 * rho**1/6 * y**1/6 * rho**1/6 * z**1/6 * 0.0832)) # cca,ta are arrays. y & z are 2 dependent variables
> dzdt = (k1[i]*ta[i]*cca[i]*12.011 + (21.2834*k2[i]*ta[i]*cca[i] * y**1/2 *ds) - (k3[i]*ta[i]*12.011*y*math.pi*ds**2 * cco[i]) -
> (phi*k4[i]*ta[i]*xo[i]*12.011*y*math.pi*ds**2))
>return [dydt,dzdt] # dydt & dzdt are my final time integrated values
>
>initial = [0.01,1e-5]
>sol = odeint(tran, initial, dt) #this is my function call
>
>If I pass array in my function call, it says only length-1 can be converted to Python scalars.
>
>So I request all the experts in the group to tell me where I'm going wrong.
>
>Regards
You are getting this error, correct?
TypeError: only length-1 arrays can be converted to Python scalars
I don't know anything about odeint. But when I get these errors it
is because I passed a numpy array into something that wanted a python
list. This may be what you are doing.
numpy arrays have a tolist method.
see if passing array.tolist() instead of array works for you.
Laura
More information about the Python-list
mailing list