Domanda

so I am attempting to solve a system of three ODEs and developed the following code to solve them using ODEint. But when I run, ODEint has an issue calling on my function for the system of equations.

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

And in my output I get:

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

Changing the order of y0 and t still results in the same error. I do not understand the error nor does it guide me to the issue. Any help would be greatly appreciated.

È stato utile?

Soluzione

Per the docs, the signature for sugar should be

func(y, t, ...)

rather than

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))

yields

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]]
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top