Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scipy.integrate.ode with two coupled ODEs?

Tags:

python

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?

like image 937
Magsol Avatar asked Apr 20 '11 01:04

Magsol


People also ask

How do you integrate ODE?

We can use an integrating factor μ(t) to solve any first order linear ODE. Recall that such an ODE is linear in the function and its first derivative. The general form for a first order linear ODE in x(t) is dxdt+p(t)x(t)=q(t).

What is the difference between Odeint and solve_ivp?

The primary advantage is that solve_ivp offers several methods for solving differential equations whereas odeint is restricted to one. We get started by setting up our system of differential equations and some parameters of the simulation. UPDATE 07.02.


2 Answers

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

like image 118
Paul Avatar answered Sep 19 '22 02:09

Paul


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.

like image 27
Ben Avatar answered Sep 21 '22 02:09

Ben