python - Python中的矩阵常微分方程
问题描述
我需要以下列形式求解 ODE:
其中,我想找到A(t)和C(t)是一个已知的8x8矩阵。问题是我只能把这个矩阵写成一个矩阵列表,每次我有一个矩阵写在一个列表中(它的维数是[8,8,1000]
, 1000因为我已经在区间 [0, 1] dt = .001)。之所以这样写,是因为我不知道它的解析显式表达。
我找到了解决矩阵 ODE 的这个库:
但是当我尝试使用它时,我得到:
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)
但我不知道如何使用它来解决我的问题或是否可能。有人能帮助我吗?
解决方案
该错误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
.
推荐阅读
- sql - 如何在 Shell 脚本中比较两个 SQL 文件
- hibernate - JPA Criteria API selectCase in where 子句
- dataframe - 获取 DataFrame 的 Column 值导致 'Column' 对象不可调用
- php - 缓存存储 [] 未定义
- python - Bigtable python客户端:如何检索大于给定值的最小行键
- css - Tailwind/CSS 使用自定义字体
- node.js - request-promise post 请求到 axios 请求
- vespa - ES "minimum_should_match" 查询如何转换为 vespa 查询?
- flutter - Flutter 插件混淆
- cors - Lumen 8 CORS 问题 2020 年 12 月