Skip to main content
 首页 » 编程设计

python之在 Python 中模拟 MATLAB 的 ode15s

2024年10月25日7cloudgamer

我正在努力将模型从 MATLAB 转换为 Python。该模型的关键在于MATLAB 的ode15s。在 MATLAB 执行中,ode15s 有标准选项:

options = odeset() 
[t P] = ode15s(@MODELfun, tspan, y0, options, params) 

作为引用,y0 是一个向量(大小为 98),MODELfun 也是。

我在 Python 中的等效尝试如下:

ode15s = scipy.integrate.ode(Model.fun) 
ode15s.set_integrator('vode', method = 'bdf', order = 15) 
ode15s.set_initial_value(y0).set_f_params(params) 
dt = 1  
while ode15s.successful() and ode15s.t < duration: 
     ode15s.integrate(ode15s.t+dt) 

不过,这似乎不起作用。有什么建议或替代方案吗?

编辑: 查看输出后,我从 Python 得到的结果是 y0 的某些元素随时间没有变化,或者 y0 的其余部分在每一步都不断变化。有这样的经验吗?

请您参考如下方法:

根据SciPy wiki for Matlab Users , ode15s 的正确使用方法是

scipy.integrate.ode(f).set_integrator('vode', method='bdf', order=15)