scipy.integrate.

ode#

classscipy.integrate.ode(f,jac=None)[source]#

A generic interface class to numeric integrators.

Solve an equation system\(y'(t) = f(t,y)\) with (optional)jac=df/dy.

Note: The first two arguments off(t,y,...) are in theopposite order of the arguments in the system definition function usedbyscipy.integrate.odeint.

Parameters:
fcallablef(t,y,*f_args)

Right-hand side of the differential equation. t is a scalar,y.shape==(n,).f_args is set by callingset_f_params(*args).f should return a scalar, array or list (not a tuple).

jaccallablejac(t,y,*jac_args), optional

Jacobian of the right-hand side,jac[i,j]=df[i]/dy[j].jac_args is set by callingset_jac_params(*args).

Attributes:
tfloat

Current time.

yndarray

Current variable values.

Methods

get_return_code()

Extracts the return code for the integration to enable better control if the integration fails.

integrate(t[, step, relax])

Find y=y(t), set y as an initial condition, and return y.

set_f_params(*args)

Set extra parameters for user-supplied function f.

set_initial_value(y[, t])

Set initial conditions y(t) = y.

set_integrator(name, **integrator_params)

Set integrator by name.

set_jac_params(*args)

Set extra parameters for user-supplied function jac.

set_solout(solout)

Set callable to be called at every successful integration step.

successful()

Check if integration was successful.

See also

odeint

an integrator with a simpler interface based on lsoda from ODEPACK

quad

for finding the area under a curve

Notes

Available integrators are listed below. They can be selected usingtheset_integrator method.

“vode”

Real-valued Variable-coefficient Ordinary Differential Equationsolver, with fixed-leading-coefficient implementation. It providesimplicit Adams method (for non-stiff problems) and a method based onbackward differentiation formulas (BDF) (for stiff problems).

Source:http://www.netlib.org/ode/vode.f

This integrator accepts the following parameters inset_integratormethod of theode class:

  • atol : float or sequenceabsolute tolerance for solution

  • rtol : float or sequencerelative tolerance for solution

  • lband : None or int

  • uband : None or intJacobian band width, jac[i,j] != 0 for i-lband <= j <= i+uband.Setting these requires your jac routine to return the jacobianin packed format, jac_packed[i-j+uband, j] = jac[i,j]. Thedimension of the matrix must be (lband+uband+1, len(y)).

  • method: ‘adams’ or ‘bdf’Which solver to use, Adams (non-stiff) or BDF (stiff)

  • with_jacobian : boolThis option is only considered when the user has not supplied aJacobian function and has not indicated (by setting either band)that the Jacobian is banded. In this case,with_jacobian specifieswhether the iteration method of the ODE solver’s correction step ischord iteration with an internally generated full Jacobian orfunctional iteration with no Jacobian.

  • nsteps : intMaximum number of (internally defined) steps allowed during onecall to the solver.

  • first_step : float

  • min_step : float

  • max_step : floatLimits for the step sizes used by the integrator.

  • order : intMaximum order used by the integrator,order <= 12 for Adams, <= 5 for BDF.

“zvode”

Complex-valued Variable-coefficient Ordinary Differential Equationsolver, with fixed-leading-coefficient implementation. It providesimplicit Adams method (for non-stiff problems) and a method based onbackward differentiation formulas (BDF) (for stiff problems).

Source:http://www.netlib.org/ode/zvode.f

This integrator accepts the same parameters inset_integratoras the “vode” solver.

Note

When using ZVODE for a stiff system, it should only be used forthe case in which the function f is analytic, that is, when each f(i)is an analytic function of each y(j). Analyticity means that thepartial derivative df(i)/dy(j) is a unique complex number, and thisfact is critical in the way ZVODE solves the dense or banded linearsystems that arise in the stiff case. For a complex stiff ODE systemin which f is not analytic, ZVODE is likely to have convergencefailures, and for this problem one should instead use DVODE on theequivalent real system (in the real and imaginary parts of y).

“lsoda”

Real-valued Variable-coefficient Ordinary Differential Equationsolver, with fixed-leading-coefficient implementation. It providesautomatic method switching between implicit Adams method (for non-stiffproblems) and a method based on backward differentiation formulas (BDF)(for stiff problems).

This integrator uses the C translation of the original Fortran 77 ODEPACKlibrary, which can be found athttp://www.netlib.org/odepack

This integrator accepts the following parameters inset_integratormethod of theode class:

  • atol : float or sequenceabsolute tolerance for solution

  • rtol : float or sequencerelative tolerance for solution

  • lband : None or int

  • uband : None or intJacobian band width, jac[i,j] != 0 for i-lband <= j <= i+uband.Setting these requires your jac routine to return the jacobianin packed format, jac_packed[i-j+uband, j] = jac[i,j].

  • with_jacobian : boolNot used.

  • nsteps : intMaximum number of (internally defined) steps allowed during onecall to the solver.

  • first_step : float

  • min_step : float

  • max_step : floatLimits for the step sizes used by the integrator.

  • max_order_ns : intMaximum order used in the nonstiff case (default 12).

  • max_order_s : intMaximum order used in the stiff case (default 5).

  • max_hnil : intMaximum number of messages reporting too small step size (t + h = t)(default 0)

  • ixpr : intWhether to generate extra printing at method switches (default False).

“dopri5”

This is an explicit runge-kutta method of order (4)5 due to Dormand &Prince (with stepsize control and dense output).

Authors:

E. Hairer and G. WannerUniversite de Geneve, Dept. de MathematiquesCH-1211 Geneve 24, Switzerlande-mail:ernst.hairer@math.unige.ch,gerhard.wanner@math.unige.ch

This code is described in[HNW93].

This integrator accepts the following parameters in set_integrator()method of the ode class:

  • atol : float or sequenceabsolute tolerance for solution

  • rtol : float or sequencerelative tolerance for solution

  • nsteps : intMaximum number of (internally defined) steps allowed during onecall to the solver.

  • first_step : float

  • max_step : float

  • safety : floatSafety factor on new step selection (default 0.9)

  • ifactor : float

  • dfactor : floatMaximum factor to increase/decrease step size by in one step

  • beta : floatBeta parameter for stabilised step size control.

  • verbosity : intSwitch for printing messages (< 0 for no messages).

“dop853”

This is an explicit runge-kutta method of order 8(5,3) due to Dormand& Prince (with stepsize control and dense output).

Options and references the same as “dopri5”.

References

[HNW93]

E. Hairer, S.P. Norsett and G. Wanner, Solving OrdinaryDifferential Equations i. Nonstiff Problems. 2nd edition.Springer Series in Computational Mathematics,Springer-Verlag (1993)

Examples

A problem to integrate and the corresponding jacobian:

>>>fromscipy.integrateimportode>>>>>>y0,t0=[1.0j,2.0],0>>>>>>deff(t,y,arg1):...return[1j*arg1*y[0]+y[1],-arg1*y[1]**2]>>>defjac(t,y,arg1):...return[[1j*arg1,1],[0,-arg1*2*y[1]]]

The integration:

>>>r=ode(f,jac).set_integrator('zvode',method='bdf')>>>r.set_initial_value(y0,t0).set_f_params(2.0).set_jac_params(2.0)>>>t1=10>>>dt=1>>>whiler.successful()andr.t<t1:...print(r.t+dt,r.integrate(r.t+dt))1 [-0.71038232+0.23749653j  0.40000271+0.j        ]2.0 [0.19098503-0.52359246j 0.22222356+0.j        ]3.0 [0.47153208+0.52701229j 0.15384681+0.j        ]4.0 [-0.61905937+0.30726255j  0.11764744+0.j        ]5.0 [0.02340997-0.61418799j 0.09523835+0.j        ]6.0 [0.58643071+0.339819j 0.08000018+0.j      ]7.0 [-0.52070105+0.44525141j  0.06896565+0.j        ]8.0 [-0.15986733-0.61234476j  0.06060616+0.j        ]9.0 [0.64850462+0.15048982j 0.05405414+0.j        ]10.0 [-0.38404699+0.56382299j  0.04878055+0.j        ]
On this page