简体   繁体   English

有没有办法在python中求解耦合微分方程组?

[英]Any way to solve a system of coupled differential equations in python?

I've been working with sympy and scipy, but can't find or figure out how to solve a system of coupled differential equations (non-linear, first-order). 我一直在研究sympy和scipy,但找不到或想出如何求解耦合微分方程组(非线性,一阶)。

So is there any way to solve coupled differential equations? 那么有没有办法解决耦合微分方程?

The equations are of the form: 方程的形式如下:

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)

with initial conditions for v11(s), v22(s), v12(s). 初始条件为v11(s),v22(s),v12(s)。

For the numerical solution of ODEs with scipy, see scipy.integrate.solve_ivp , scipy.integrate.odeint or scipy.integrate.ode . 有关scipy的ODE的数值解,请参阅scipy.integrate.solve_ivpscipy.integrate.odeintscipy.integrate.ode

Some examples are given in the SciPy Cookbook (scroll down to the section on "Ordinary Differential Equations"). SciPy Cookbook中给出了一些例子(向下滚动到“常微分方程”部分)。

In addition to SciPy methods odeint and ode that were already mentioned, it now has solve_ivp which is newer and often more convenient. 除了已经提到的SciPy方法odeintode ,它现在还有更新且更方便的solve_ivp A complete example, encoding [v11, v22, v12] as an array v : 一个完整的示例,将[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])

This solves the system on the interval (0, 0.1) with initial value [2, 3, 4] . 这解决了系统在间隔(0, 0.1)上的初始值[2, 3, 4] The result has independent variable (s in your notation) as res.t : 结果有自变量(在你的表示法中为s)为res.t

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

These values were chosen automatically. 这些值是自动选择的。 One can provide t_eval to have the solution evaluated at desired points: for example, t_eval=np.linspace(0, 0.1) . 可以提供t_eval以在期望点处评估解:例如, t_eval=np.linspace(0, 0.1)

The dependent variable (the function we are looking for) is in res.y : 因变量(我们正在寻找的函数)在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]])

With Matplotlib, this solution is plotted as plt.plot(res.t, res.yT) (the plot would be smoother if I provided t_eval as mentioned). 使用Matplotlib,此解决方案被绘制为plt.plot(res.t, res.yT) (如果我提供t_eval ,则绘图会更平滑)。

解决方案的情节

Finally, if the system involved equations of order higher than 1, one would need to use reduction to a 1st order system . 最后,如果系统涉及的顺序高于1的方程式,则需要使用简化为一阶系统

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM