Magsol
Magsol

Reputation: 4690

scipy.integrate.ode with two coupled ODEs?

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?

Upvotes: 6

Views: 9208

Answers (2)

Paul
Paul

Reputation: 43620

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

Upvotes: 7

Ben
Ben

Reputation: 41

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.

Upvotes: 4

Related Questions