[SciPy-User] making use of args in odeint

Rob Clewley rob.clewley@gmail....
Mon Oct 19 19:05:31 CDT 2009

On Mon, Oct 19, 2009 at 5:58 PM, Luke <hazelnusse@gmail.com> wrote:
> I am integrating some ODE's and want to calculate some extra quantities
> during numerical integration.  I was hoping to use the args option of odeint
> as a way to parameters which could be 'output' at each time step, but it
> isn't working the way I would expect.  Here is a short example of
> integrating the equation:
> dx/dt = -a*x
> where a is a parameter, and we also want to calculate at every time step:
> b = t / 2.

odeint may call f for t values beyond those you specify, leaving the
last t/2 value to not be what you want it to be, as you suggest.
Abusing the params option is not a recommended way to use odeint.
Instead, why can't you dump the t value used when b is calculated to a
global *list* that will be dynamically allocated? Then you won't need
to worry what internal time steps are taken or abuse params. I.e.,

aux_t = []
aux_b = []

def f(x, t, params):
    a = params[0]
    aux_b.append(t / 2.) # or any other function of t, x
    return -a*x

Then at least you have a consistent set of (t, b) pairs after integration.

> The reason for even taking this approach is that there are lot of
> repetitious calculations that are done in more complicated systems that I
> would like to avoid doing in a separate step after numerical integration.
> These calculations are being done regardless In this example this isn't
> apparent, but in other systems, the calculations are more significant and so
> there is some computational savings to be had from this approach.

If you really have such a complex problem that you can't just do that
(or it's time/memory inefficient) then you'll need to use a more
sophisticated ODE solver that allows you to make user-defined
functions and "auxiliary" variables that are output along with the
state variables. That would be PyDSTool, IMO. You can also require
those solvers to pass through predetermined time steps (for the
output) and/or use interpolated output so that you'd guarantee your b
values are where you want them to be.


More information about the SciPy-User mailing list