I'm trying to implement coupled differential equations in Python, and as a new user I seem to be stuck at something. I used this tutorial as a guide to how to solve my ODEs, and looked into the documentation to no available
This is where I define the function
def Burnout(t, y, m, nu, S0, V, delta, mu):
S = y[0];
E = [0 for i in range(0,m)]
dEdt = [0 for i in range(0,m)]
for i in range(0,m):
E.append(y[i+1])
P = y[m+1]
dSdt = -nu*S*P*(S/S0)**V
dEdt.append(nu*S*P*(S/S0)**V-m*delta*E[0])
for i in range(1,m):
dEdt.append(m*delta*E[i-1]-m*delta*E[i])
dPdt = m*delta*E[m-1]-mu*P
return [dSdt, *dEdt[0:m], dPdt]
Then, as in the tutorial, I define the initial conditions by
S0 = N
y0.append(S0)
for i in range (0, m):
E.append(0)
y0.append(E[i])
P0 = Z
y0.append(P0)
where N and Z are previously defined things, and E was an empty array. When I finally call odeint(Burnout, y0, t, args = p), I get a 'float' object is not subscriptable pointing to my definition of S in my Burnout function. As I passed a list to odeint I'm kind of confused on why Python says I passed a float. Does anyone see what I did wrong? Thanks in advance!
EDIT: Ok, now here is a minimal, complete and verifiable example that gives me the same error
import numpy as np from scipy.integrate import odeint
def Burnout(t, y, m, nu, S0, V, delta, mu):
S = y[0]
E = [0 for i in range(0,m)]
dEdt = [0 for i in range(0,m)]
for i in range(0,m):
E.append(y[i+1])
P = y[m+1]
dSdt = -nu*S*P*(S/S0)**V
dEdt.append(nu*S*P*(S/S0)**V-m*delta*E[0])
for i in range(1,m):
dEdt.append(m*delta*E[i-1]-m*delta*E[i])
dPdt = m*delta*E[m-1]-mu*P
return [dSdt, *dEdt[0:m], dPdt]
V = 2.97
m = 26
delta = 1/6
mu = 1
nu = 10
S0 = 5
t = np.linspace(0,56,100)
y = [10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100]
p = (m, nu, V, S0, delta, mu)
print(odeint(Burnout,y,t,args=p))
You ordered the arguments in your ode definition wrong. It is possible to have t
before y
, but then you must define tfirst = True
(see docs) .
Swapping the arguments in your definition of Burnout
fixes the problem for me.
def Burnout(y, t, m, nu, S0, V, delta, mu):
# ...
# rest of function
# ...
Alternately you can define the additional keyword tfirst
in the odeint
call:
odeint(Burnout, y, t, args=p, tfirst=True)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.