scipy.integrate.ode with two coupled ODEs?

Magsol picture Magsol · Apr 20, 2011 · Viewed 8.9k times · Source

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?

Answer

Paul picture Paul · Apr 20, 2011

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