使用 odeint 时数组的形状不均匀
Inhomogeneous shape of an array when using odeint
我正在尝试使用 odeint 求解描述一些聚合过程的方程组。为此,我需要使用系数,这取决于我要解决的变量之一。当我尝试 运行 代码时:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
T = 25+298.15
p = 1
omega0= 0.3
def kp(x):
return (3.3*10**5+3.8*10**6*np.exp((-5.3*omega0*(1-x))/(1-omega0*x)))*np.exp((-1.88*10**3+(0.096+0.11*omega0*(1-x)*p/(1-omega0*x)))/T)
k_sd = 9.3*10**6
c_nu = 13
k_td = 10**9
Crd = 100
def nu(x):
return np.exp(c_nu*x)
def kt(x):
return 1/(k_sd**(-1)+nu(x)/k_td)+Crd*(1-x)*kp(x)
f = 0.64
kd = 1.58*10**(-4)
parameters = (f, kd)
IC = [0.05, 3.5, 0, 0, 0, 0]
def omega(c):
return c*86/1000
def X(a):
return (omega(IC[1])-omega(a))/(omega(IC[1])-omega(c)*omega(IC[1]))
t = np.linspace(0,500,10001)
def model_basic(y, t, f, kd):
I, M, Y0, Y1, Q0, Q2 = y
dydt = [-kd*I,
-kp(X(M))*M*Y0,
2*f*kd*I-2*kt(X(M))*Y0*Y0,
kp(X(M))*M*Y0-2*kt(X(M))*Y0*Y1,
2*f*kd*I-kt(X(M))*Y0*Y0/2,
kp(X(M))*M*Y0+2*kp(X(M))*M*Y1+kt(X(M))*Y1*Y1]
return dydt
sol_2 = odeint(model_basic, IC, t, parameters, hmax = 0.1)
我收到以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-45-bec4df54cef7> in <module>
9 kp(X(M))*M*Y0+2*kp(X(M))*M*Y1+kt(X(M))*Y1*Y1]
10 return dydt
---> 11 sol_2 = odeint(model_basic, IC, t, parameters, hmax = 0.1)
C:\eclipse\miniConda\lib\site-packages\scipy\integrate\odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg, tfirst)
239 t = copy(t)
240 y0 = copy(y0)
--> 241 output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
242 full_output, rtol, atol, tcrit, h0, hmax, hmin,
243 ixpr, mxstep, mxhnil, mxordn, mxords,
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (6,) + inhomogeneous part.
关于类似问题的其他主题总是与不正确的初始值列表有关。我检查了我的代码,没有发现初始值和我传递给 odeint
的函数 model_basic
之间有任何不一致。有人可以帮我吗?
将您的代码复制到干净的工作中 space,我得到的第一个错误是 def X(a)
中未定义变量 c
。如果你在会话中的其他地方有那个,并且之前的 c
是一个列表,那么你会得到你报告的错误。
我正在尝试使用 odeint 求解描述一些聚合过程的方程组。为此,我需要使用系数,这取决于我要解决的变量之一。当我尝试 运行 代码时:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
T = 25+298.15
p = 1
omega0= 0.3
def kp(x):
return (3.3*10**5+3.8*10**6*np.exp((-5.3*omega0*(1-x))/(1-omega0*x)))*np.exp((-1.88*10**3+(0.096+0.11*omega0*(1-x)*p/(1-omega0*x)))/T)
k_sd = 9.3*10**6
c_nu = 13
k_td = 10**9
Crd = 100
def nu(x):
return np.exp(c_nu*x)
def kt(x):
return 1/(k_sd**(-1)+nu(x)/k_td)+Crd*(1-x)*kp(x)
f = 0.64
kd = 1.58*10**(-4)
parameters = (f, kd)
IC = [0.05, 3.5, 0, 0, 0, 0]
def omega(c):
return c*86/1000
def X(a):
return (omega(IC[1])-omega(a))/(omega(IC[1])-omega(c)*omega(IC[1]))
t = np.linspace(0,500,10001)
def model_basic(y, t, f, kd):
I, M, Y0, Y1, Q0, Q2 = y
dydt = [-kd*I,
-kp(X(M))*M*Y0,
2*f*kd*I-2*kt(X(M))*Y0*Y0,
kp(X(M))*M*Y0-2*kt(X(M))*Y0*Y1,
2*f*kd*I-kt(X(M))*Y0*Y0/2,
kp(X(M))*M*Y0+2*kp(X(M))*M*Y1+kt(X(M))*Y1*Y1]
return dydt
sol_2 = odeint(model_basic, IC, t, parameters, hmax = 0.1)
我收到以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-45-bec4df54cef7> in <module>
9 kp(X(M))*M*Y0+2*kp(X(M))*M*Y1+kt(X(M))*Y1*Y1]
10 return dydt
---> 11 sol_2 = odeint(model_basic, IC, t, parameters, hmax = 0.1)
C:\eclipse\miniConda\lib\site-packages\scipy\integrate\odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg, tfirst)
239 t = copy(t)
240 y0 = copy(y0)
--> 241 output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
242 full_output, rtol, atol, tcrit, h0, hmax, hmin,
243 ixpr, mxstep, mxhnil, mxordn, mxords,
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (6,) + inhomogeneous part.
关于类似问题的其他主题总是与不正确的初始值列表有关。我检查了我的代码,没有发现初始值和我传递给 odeint
的函数 model_basic
之间有任何不一致。有人可以帮我吗?
将您的代码复制到干净的工作中 space,我得到的第一个错误是 def X(a)
中未定义变量 c
。如果你在会话中的其他地方有那个,并且之前的 c
是一个列表,那么你会得到你报告的错误。