【发布时间】:2020-05-14 03:28:11
【问题描述】:
我想在 python 中编写一个耦合微分方程系统:dF/dt=A(F) 其中F 是一个矩阵,A(F) 是矩阵F 的一个函数。
当F 和A(F) 是向量时,使用scipy.integrate.odeint 求解方程。
但是,scipy.integrate.odeint 不适用于矩阵,我得到一个错误:
tmin, tmax, tstep = (0., 200., 1)
t_test=np.arange(tmin, tmax, tstep) #time vector
dydt_testm=np.array([[0.,1.],[2.,3.]])
Y0_test=np.array([[0,1],[0,1]])
def dydt_test(y,t):
return dydt_testm
result = si.odeint(dydt_test, Y0_test,t_test)
ValueError:初始条件 y0 必须是一维的。
【问题讨论】:
-
到目前为止你尝试了什么?
-
看看
odeintw。源代码在 github 上github.com/WarrenWeckesser/odeintw -
你可以用
F=F.reshape(n,n)和return dF.flatten()包装ode函数 -
@WarrenWeckesser 在 jupyter notebook 中使用 IPython 时我应该如何导入 odeintw ?它适用于 Python 2 吗?
-
odeintw位于PyPI,因此您可以使用pip命令安装它。 (如果您不熟悉pip,请搜索教程。)我有一段时间没有更新发布的版本,所以它应该仍然适用于 Python 2.7。
标签: python matrix scipy system differential-equations