matplotlib - 绘制 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)
解决方案
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")
...
推荐阅读
- makefile - 如何将目标文件复制到 Makefile 中的子文件夹?
- arrays - 从 tcl 中的数组中检索数字索引
- reactjs - 如何在我的反应组件中使用在 mongodb 上通过 multer 上传的图像?
- java - Android Popup Menu + Database 和查看更改问题
- c# - UWP 从 inkToolbarPenButton 中删除大小滑块
- arrays - 如何从 np.arange 中检索值?
- postgresql - 即使服务器关闭了连接,我的 PostgreSQL 查询是否仍在运行?
- excel - 索引匹配其他表中的数字,如果找不到,则匹配前 3 位数字
- r - 使用 tidymodels 编写分类工作流。得到“错误:列`.row`必须是长度..”
- d3.js - D3 v5:未为折线图绘制线,并且刻度未显示在 y 轴上