【问题标题】:Any way to solve a system of coupled differential equations in python?有什么方法可以解决python中的耦合微分方程系统?
【发布时间】:2013-05-30 08:58:01
【问题描述】:

我一直在使用 sympy 和 scipy,但无法找到或弄清楚如何求解耦合微分方程组(非线性、一阶)。

那么有什么方法可以求解耦合微分方程吗?

方程的形式为:

V11'(s) = -12*v12(s)**2
v22'(s) = 12*v12(s)**2
v12'(s) = 6*v11(s)*v12(s) - 6*v12(s)*v22(s) - 36*v12(s)

具有 v11(s)、v22(s)、v12(s) 的初始条件。

【问题讨论】:

  • 看看sage。它使用 python 语法提供类似数学的功能。它也许能够解决 diff eqs。
  • 您在寻找解析解还是数值解? (您提到使用 sympy,因此您可能希望有一个分析解决方案,如果有的话。)
  • @WarrenWeckesser 一个数值解,类似于数学的 NDsolve。
  • 这是一阶,但对我来说这看起来不像线性系统,因为你有因变量的幂和乘积。
  • @Bitrex 你是对的,我错误地写了线性而不是非线性。帖子已更新。好收获!

标签: python numpy scipy enthought sympy


【解决方案1】:

关于使用 scipy 的 ODE 的数值解,请参阅 scipy.integrate.solve_ivpscipy.integrate.odeintscipy.integrate.ode

SciPy Cookbook 中给出了一些示例(向下滚动到“常微分方程”部分)。

【讨论】:

  • 文档似乎有点难以理解,但你链接的食谱非常适合我想要做的事情,谢谢!
【解决方案2】:

除了已经提到的 SciPy 方法 odeintode,它现在还有 solve_ivp,它更新并且通常更方便。一个完整的例子,将[v11, v22, v12]编码为数组v

from scipy.integrate import solve_ivp
def rhs(s, v): 
    return [-12*v[2]**2, 12*v[2]**2, 6*v[0]*v[2] - 6*v[2]*v[1] - 36*v[2]]
res = solve_ivp(rhs, (0, 0.1), [2, 3, 4])

这解决了区间(0, 0.1) 上的系统,初始值为[2, 3, 4]。结果有自变量(用你的符号表示)为res.t

array([ 0.        ,  0.01410735,  0.03114023,  0.04650042,  0.06204205,
        0.07758368,  0.0931253 ,  0.1       ])

这些值是自动选择的。可以提供t_eval 以在所需点评估解决方案:例如t_eval=np.linspace(0, 0.1)

因变量(我们要找的函数)在res.y

array([[ 2.        ,  0.54560138,  0.2400736 ,  0.20555144,  0.2006393 ,
         0.19995753,  0.1998629 ,  0.1998538 ],
       [ 3.        ,  4.45439862,  4.7599264 ,  4.79444856,  4.7993607 ,
         4.80004247,  4.8001371 ,  4.8001462 ],
       [ 4.        ,  1.89500744,  0.65818761,  0.24868116,  0.09268216,
         0.0345318 ,  0.01286543,  0.00830872]])

使用 Matplotlib,此解决方案被绘制为 plt.plot(res.t, res.y.T)(如果我提供 t_eval,则绘图会更平滑)。

最后,如果系统涉及阶数高于 1 的方程,则需要使用reduction to a 1st order system

【讨论】:

    猜你喜欢
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    相关资源
    最近更新 更多