首页 > 解决方案 > Python中的矩阵常微分方程

问题描述

我需要以下列形式求解 ODE:

在此处输入图像描述

其中,我想找到A(t)C(t)是一个已知的8x8矩阵。问题是我只能把这个矩阵写成一个矩阵列表,每次我有一个矩阵写在一个列表中(它的维数是[8,8,1000], 1000因为我已经在区间 [0, 1] dt = .001)。之所以这样写,是因为我不知道它的解析显式表达。

我找到了解决矩阵 ODE 的这个库:

odeintw

但是当我尝试使用它时,我得到:

from odeintw import odeintw
def asys(a, t, c):
    return c.dot(a) -a.dot(c)
c = Mymatrix #[8,8,1000]
a0 = rho0
t = np.linspace(0,1,1000)
sol = odeintw(asys, a0, t)

这个错误:

ValueError: shapes (8,8,1000) and (8,8) not aligned: 1000 (dim 2) != 8 (dim 0)

但我不知道如何使用它来解决我的问题或是否可能。有人能帮助我吗?

标签: pythonmatrixode

解决方案


该错误TypeError: 'module' object is not callable通常是导入语句错误的结果。

案例 1 - 导入“odeintw”包

import numpy as np
import odeintw

def asys(a, t, c):
    return c.dot(a) - a.dot(c)

a0 = 9
t = np.linspace(0,1,1000)
sol = odeintw(asys, a0, t)

结果

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    sol = odeintw(asys, a0, t)
TypeError: 'module' object is not callable

案例 2 -odeintw从包中导入函数

import numpy as np
from odeintw import odeintw

def asys(a, t, c):
    return c.dot(a) - a.dot(c)

c = np.array([[1, 2], [3, 4]])
a0 = np.array([[5, 6], [7, 8]])
t = np.linspace(0,1,1000)
sol = odeintw(asys, a0, t)

结果

[[[   5.            6.        ]
  [   7.            8.        ]]

 [[   4.99602602    5.98801394]
  [   7.01201813    8.00397398]]

 [[   4.99211203    5.97607958]
  [   7.02404867    8.00788797]]

 ...

 [[ 176.46661225  -74.83312649]
  [ 385.4496081  -163.46661225]]

 [[ 177.46587768  -75.29026227]
  [ 387.63420993 -164.46587768]]

 [[ 178.47090603  -75.75003391]
  [ 389.83140992 -165.47090603]]]

分析

案例之间的主要区别在于,在案例 1 中,您正在导入一个完整的模块,然后尝试调用它并将其返回值分配给sol. 在案例 2 中,您odeintw从模块中导入函数,然后调用它并将其值分配给sol.


推荐阅读