ODEINT无法调用函数

时间:2014-04-18 18:20:31

标签: python odeint

所以我试图解决一个由三个ODE组成的系统,并开发了以下代码来使用ODEint解决它们。但是当我运行时,ODEint有一个问题是调用我的方程组函数。

from scipy.integrate import odeint



#initial parameters
X0 = 75.
M0 = 150.
G0 = 105.
N0 = 80.
T0 = 7.
u10 = 0.0301231859
u20 = 0.0078947020
u30 = 0.0010708464
Rxg = 1.92
Rxm = 3.84
Rxma = 5.76

#define system of equations
def sugar(t,y,u10,u20,u30,Rxg,Rxm,Rxma):
    ddt = [-u10*(X0+(Rxg*(G0-y[0]))+(Rxm*(M0-y[1]))+(Rxma*(N0-y[2]))),
           -u20*(X0+(Rxg*(G0-y[0]))+(Rxm*(M0-y[1]))+(Rxma*(N0-y[2]))),
           -u30*(X0+(Rxg*(G0-y[0]))+(Rxm*(M0-y[1]))+(Rxma*(N0-y[2])))]
    return(ddt)

#time parameter
tinit = 0.
tend = 10.
h = 2.
t = arange(tinit,tend+h,h)

#initial guess
y0 = [G0,M0,N0]

#run odeint
y = odeint(sugar,y0,t,args = (u10,u20,u30,Rxg,Rxm,Rxma))

print 't =',t
print 'y =',y

在我的输出中,我得到了:

t = [  0.   2.   4.   6.   8.  10.]
y = [[ 105.  150.   80.]
 [ 105.  150.   80.]
 [ 105.  150.   80.]
 [ 105.  150.   80.]
 [ 105.  150.   80.]
 [ 105.  150.   80.]]
error: Error occurred while calling the Python function named sugar

更改y0和t的顺序仍然会导致相同的错误。我不明白这个错误,也没有指导我解决这个问题。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

根据the docssugar的签名应为

func(y, t, ...)

而不是

func(t, y ...)

import numpy as np
import scipy.integrate as integrate

# initial parameters
X0 = 75.
M0 = 150.
G0 = 105.
N0 = 80.
T0 = 7.
u10 = 0.0301231859
u20 = 0.0078947020
u30 = 0.0010708464
Rxg = 1.92
Rxm = 3.84
Rxma = 5.76

# define system of equations


def sugar(y, t, u10, u20, u30, Rxg, Rxm, Rxma):
    ddt = [-u10 * (
        X0 + (Rxg * (G0 - y[0])) + (Rxm * (M0 - y[1])) + (Rxma * (N0 - y[2]))),
        -u20 * (X0 + (Rxg * (G0 - y[0])) + (Rxm * (M0 - y[1])) + (
                Rxma * (N0 - y[2]))),
        -u30 * (X0 + (Rxg * (G0 - y[0])) + (Rxm * (M0 - y[1])) + (Rxma * (N0 - y[2])))]
    return ddt

# time parameter
tinit = 0.
tend = 10.
h = 2.
t = np.arange(tinit, tend + h, h)

# initial guess
y0 = [G0, M0, N0]

# run odeint
y = integrate.odeint(sugar, y0, t, args=(u10, u20, u30, Rxg, Rxm, Rxma))

print('t = {t}'.format(t=t))
print('y = {y}'.format(y=y))

产量

t = [  0.   2.   4.   6.   8.  10.]
y = [[ 105.          150.           80.        ]
 [ 100.02722703  148.69673278   79.82322335]
 [  94.02207128  147.12289808   79.60974661]
 [  86.77020667  145.22232521   79.35195073]
 [  78.01280485  142.92718027   79.04063465]
 [  67.43730555  140.15554729   78.66468719]]