【问题标题】:system of ODEs in matrix form矩阵形式的 ODE 系统
【发布时间】:2021-07-21 06:21:25
【问题描述】:

我试图弄清楚如何解决和绘制 2x2 矩阵 A 的系统 dx/dt = Ax。我真的不知道该怎么做。我目前的代码如下:

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

def sys(x, t, A):
    x1, x2 = x    
    return [A @ x]

A = np.array([[1, 2], [3, 4]])


x0 = np.array([[1], [2]])

t = np.linspace(0, 100, 10000)

sol = odeint(sys, x0, t, A)

ax = plt.axes()
ax.plot(t, sol)
plt.show()

错误信息是:

output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
odepack.error: Extra arguments must be in a tuple.

非常感谢有关如何使此代码正常工作的帮助。请注意,我对编码很陌生,更不用说编码微分方程了。 非常感谢。

【问题讨论】:

    标签: numpy ode differential-equations


    【解决方案1】:

    您必须在元组中传递其他参数。像这样的:

    sol = odeint(sys, x0, t, args=(A,))
    

    请参阅documentation on scipy.integrate.odeint

    【讨论】:

    • 谢谢。在实施您的建议后,说明传递给函数 sys 的初始条件的附加错误必须是一维的。经过一番研究,我发现我可以将函数更改为 def sys(x, t, A): x1, x2 = x dxdt = A @ x.reshape(2, 1) return np.squeeze(np.asarray(dxdt))这似乎工作得很好。
    • 很高兴能帮上忙!
    • 提供正确的argsx 形状是使用此类scipy 函数时最常见的两个问题。密切关注文档很重要,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多