通过 scipy.integrate.odeint 求解微分方程组
Solving a system of differential equations by scipy.integrate.odeint
我正在尝试求解微分方程,但出现以下错误
/usr/local/lib/python3.7/dist-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)
243 full_output, rtol, atol, tcrit, h0, hmax, hmin,
244 ixpr, mxstep, mxhnil, mxordn, mxords,
245 int(bool(tfirst)))
246 if output[-1] < 0:
247 warning_msg = _msgs[output[-1]] + " Run with full_output = 1 to get quantitative information."
RuntimeError: The array return by func must be one-dimensional, but got ndim=2.**
我的密码是
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
f_ini = [0.09,0.09]
C0 = 0.083/f
k = 0.04
v = 1.05
def rxn1(C,t):
return np.array([f*C0/v-f*C[0]/v-k*C[0], f*C[0]/v-f*C[1]/v-k*C[1]])
t_points = np.linspace(0,500,1000)
c_points = scipy.integrate.odeint(rxn1, f_ini,t_points)
plt.plot(t_points, c_points[:,0])
plt.plot(t_points,c_points[:,1])`
我知道这里有人问过类似的问题。 。但是我想使用 np.array 来解决它。非常感谢!
如果“f”是变量并依赖于“t”,那么您可以将其定义为“t”的函数,并在您的 rxn1 函数中使用该函数而不是“f”。例如:
def f(t):
# relation between f and t
return value
def rxn1(C,t):
return np.array([f(t)*C0/v-f(t)*C[0]/v-k*C[0], f(t)*C[0]/v-f(t)*C[1]/v-k*C[1]])
我正在尝试求解微分方程,但出现以下错误
/usr/local/lib/python3.7/dist-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)
243 full_output, rtol, atol, tcrit, h0, hmax, hmin,
244 ixpr, mxstep, mxhnil, mxordn, mxords,
245 int(bool(tfirst)))
246 if output[-1] < 0:
247 warning_msg = _msgs[output[-1]] + " Run with full_output = 1 to get quantitative information."
RuntimeError: The array return by func must be one-dimensional, but got ndim=2.**
我的密码是
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
f_ini = [0.09,0.09]
C0 = 0.083/f
k = 0.04
v = 1.05
def rxn1(C,t):
return np.array([f*C0/v-f*C[0]/v-k*C[0], f*C[0]/v-f*C[1]/v-k*C[1]])
t_points = np.linspace(0,500,1000)
c_points = scipy.integrate.odeint(rxn1, f_ini,t_points)
plt.plot(t_points, c_points[:,0])
plt.plot(t_points,c_points[:,1])`
我知道这里有人问过类似的问题。
如果“f”是变量并依赖于“t”,那么您可以将其定义为“t”的函数,并在您的 rxn1 函数中使用该函数而不是“f”。例如:
def f(t):
# relation between f and t
return value
def rxn1(C,t):
return np.array([f(t)*C0/v-f(t)*C[0]/v-k*C[0], f(t)*C[0]/v-f(t)*C[1]/v-k*C[1]])