首页 > 解决方案 > 绘制 Scipy ODE 解决方案

问题描述

我一直在尝试使用 Scipy 以数值方式求解非线性常微分方程,特别是通过scipy.integrate.RK23命令。它返回<scipy.integrate._ivp.rk.RK23 at 0x7f2b1a908390>。如何绘制解决方案?

预先感谢您的帮助!

编辑:

作为一个简单的测试示例:

import numpy
import scipy.integrate

t0=0;
tf=1;
x0=numpy.array([0]);
def F(t,x): return t**2;

x=scipy.integrate.RK23(F,t0,x0,tf)

标签: matplotlibscipy

解决方案


RK23是一个实现解决 ODE 的方法的类,也就是说,它是一个OdeSolver,因此不应直接使用它,而应在其他函数中使用,例如solve_ivp

import numpy
from scipy.integrate import solve_ivp, RK23
import matplotlib.pyplot as plt

t0=0
tf=1

x0=numpy.array([0])

def F(t,x): return t**2

sol = solve_ivp(F, [t0, tf], x0, RK23)
print(sol)
plt.plot(sol.t, sol.y[0])
plt.show()

OdeSolver 允许开发人员添加自定义方法而无需重写 scipy,但由于 RK23 是 scipy 已经实现的经典方法,您可以只传递名称和 scipy 搜索以查找适当的类:

...
sol = solve_ivp(F, [t0, tf], x0, "RK23")
...

推荐阅读