【发布时间】:2021-10-31 12:39:10
【问题描述】:
我正在尝试求解一个微分方程,但出现以下错误
/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])`
我知道这里有人问过这个类似的问题。 How to solve a system of differential equations。但是我想使用 np.array 来解决它。非常感谢!
【问题讨论】:
-
如果您提供minimal reproducible example,别人会更容易帮助您。您还没有定义
f,我怀疑f的值是问题的原因。例如,如果我指定f = 0.1,您的代码将运行没有错误。 -
非常感谢!虽然我不确定如何改进我的问题以成为最小的可重复示例。事情是 f 没有定义,它实际上是变量( t )我正在改变。我尝试将我的代码修复为 t 到 f。但是它不起作用。