scipy.integrate.ode 具有两个耦合的 ODE?
我目前正在尝试使用 SciPy 的integrate.ode 包来求解一对耦合的一阶 ODE:例如 Lotka-Volterra 捕食者-被捕食者方程。但是,这意味着在集成循环期间,我必须更新每次迭代时发送给方法的参数,并且只需跟踪先前的值并在每次迭代时调用 set_f_params()
就不行了。似乎没有成功。
hprev = Ho
pprev = Po
yh = np.zeros(0)
yp = np.zeros(0)
while dh.successful() and dp.successful() and dp.t < endtime and dh.t < endtime:
hparams = [alpha, beta, pprev]
pparams = [delta, gamma, hprev]
dh.set_f_params(hparams)
dp.set_f_params(pparams)
dh.integrate(dh.t + stepsize)
dp.integrate(dp.t + stepsize)
yh = np.append(yh, dh.y)
yp = np.append(yp, dp.y)
hprev = dh.y
pprev = dp.y
我通过 set_f_params
在每次迭代中设置的值似乎不会传播到回调方法,考虑到网络上的示例似乎都不涉及“实时”,这并不令人意外变量传递给回调,但这是我认为将这些值获取到回调方法中的唯一方法。
有人对如何使用 SciPy 对这些 ODE 进行数值积分有什么建议吗?
I'm currently trying to use SciPy's integrate.ode package to solve a pair of first-order ODEs that are coupled: say, the Lotka-Volterra predator-prey equation. However, this means during the integration loop I have to update the parameters I'm sending to the methods on every iteration, and simply keeping track of the previous value and calling set_f_params()
on each iteration doesn't seem to be doing the trick.
hprev = Ho
pprev = Po
yh = np.zeros(0)
yp = np.zeros(0)
while dh.successful() and dp.successful() and dp.t < endtime and dh.t < endtime:
hparams = [alpha, beta, pprev]
pparams = [delta, gamma, hprev]
dh.set_f_params(hparams)
dp.set_f_params(pparams)
dh.integrate(dh.t + stepsize)
dp.integrate(dp.t + stepsize)
yh = np.append(yh, dh.y)
yp = np.append(yp, dp.y)
hprev = dh.y
pprev = dp.y
The values I'm setting at each iteration through set_f_params
don't seem to be propagated to the callback methods, which wasn't terribly surprising given none of the examples on the web seem to involve "live" variable passing to the callbacks, but this was the only method by which I could think to get these values into the callback methods.
Does anyone have any advice on how to use SciPy to numerically integrate these ODEs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能是错的,但是 这个示例 似乎非常接近您的问题。 :) 它使用 odeint 来求解 ODE 系统。
I could be wrong, but this example seems very close to your problem. :) It uses
odeint
to solve the system of ODEs.我有类似的问题。事实证明,积分器不会在每次调用Integrate()时重新评估微分方程函数,而是在其自己的内部时间进行。我将积分器的 max_step 选项更改为与 stepsize 相同,这对我有用。
I had a similar issue. Turns out, the integrator doesn't re-evaluate the differential equation function for every call of integrate(), but does it at its own internal times. I changed max_step option of the integrator to be the same as stepsize and that worked for me.