【问题标题】:Solving 1st order differential equations for matrices求解矩阵的一阶微分方程
【发布时间】: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


【解决方案1】:

正如 Warren Weckesser 在 cmets 中评论的那样,odeintw 可以胜任。

from odeintw import odeintw
import numpy as np

Y0_test=np.array([[0,1],[0,1]])
tmin, tmax, tstep = (0., 200., 1)
t_test=np.arange(tmin, tmax, tstep) #time vector

dydt_testm=np.array([[0.,1.],[2.,3.]])

def dydt_test(y,t):
    return dydt_testm

result = odeintw(dydt_test, #Computes the derivative of y at t 
                                     Y0_test,               #Initial condition on y (can be a vector).
                                     t_test)
plt.plot(t_test,result[:,0,1])
plt.show()

【讨论】:

    猜你喜欢
    • 2018-10-28
    • 2021-04-22
    • 2018-02-08
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 2018-11-01
    相关资源
    最近更新 更多