有什么方法可以解决python中的耦合微分方程组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16909779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any way to solve a system of coupled differential equations in python?
提问by bynx
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)。
采纳答案by Warren Weckesser
For the numerical solution of ODEs with scipy, see scipy.integrate.solve_ivp, scipy.integrate.odeintor scipy.integrate.ode.
对于常微分方程的与SciPy的,看到的数值解scipy.integrate.solve_ivp, scipy.integrate.odeint或scipy.integrate.ode。
Some examples are given in the SciPy Cookbook(scroll down to the section on "Ordinary Differential Equations").
SciPy Cookbook中给出了一些示例(向下滚动到“常微分方程”部分)。
回答by Warren Weckesser
In addition to SciPy methods odeintand odethat were already mentioned, it now has solve_ivpwhich is newer and often more convenient. A complete example, encoding [v11, v22, v12]as an array v:
除了SciPy的方法odeint和ode那些已经提到的,它现在有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])
This solves the system on the interval (0, 0.1)with initial value [2, 3, 4]. The result has independent variable (s in your notation) as res.t:
这在(0, 0.1)具有初始值的区间上求解系统[2, 3, 4]。结果具有自变量(符号中的 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_evalto 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.y.T)(the plot would be smoother if I provided t_evalas mentioned).
使用 Matplotlib,这个解决方案被绘制为plt.plot(res.t, res.y.T)(如果我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 阶的方程,则需要使用归约到一阶系统。

