scipy.integrate.ode 具有两个耦合的 ODE?

发布于 2024-11-02 12:48:31 字数 892 浏览 1 评论 0原文

我目前正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只涨不跌 2024-11-09 12:48:31

我可能是错的,但是 这个示例 似乎非常接近您的问题。 :) 它使用 odeint 来求解 ODE 系统。

I could be wrong, but this example seems very close to your problem. :) It uses odeint to solve the system of ODEs.

独孤求败 2024-11-09 12:48:31

我有类似的问题。事实证明,积分器不会在每次调用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文