Next: Gaussian quadrature (integrate.gauss_quadtol) Up: Integration (integrate) Previous: General integration (integrate.quad)

Ordinary differential equations (integrate.odeint)

Integrating a set of ordinary differential equations (ODEs) given initial conditions is another useful example. The function odeint is available in SciPy for integrating a first-order vector differential equation:

\begin{displaymath}
\frac{d\mathbf{y}}{dt}=\mathbf{f}\left( \mathbf{y},t\right) ,\end{displaymath}

given initial conditions \( \mathbf{y}\left( 0\right) =y_{0}, \) where \( \mathbf{y} \) is a length \( N \) vector and \( \mathbf{f} \) is a mapping from \( {\cal R}^{N} \) to \( {\cal R}^{N}. \) A higher-order ordinary differential equation can always be reduced to a differential equation of this type by introducing intermediate derivatives into the \( \mathbf{y} \) vector.

For example suppose it is desired to find the solution to the following second-order differential equation:

\begin{displaymath}
\frac{d^{2}w}{dz^{2}}-zw(z)=0\end{displaymath}

with initial conditions \( w\left( 0\right) =\frac{1}{\sqrt[3]{3^{2}}\Gamma \left( \frac{2}{3}\right) } \) and \( \left. \frac{dw}{dz}\right\vert _{z=0}=-\frac{1}{\sqrt[3]{3}\Gamma \left( \frac{1}{3}\right) }. \) It is known that the solution to this differential equation with these boundary conditions is the Airy function

\begin{displaymath}
w=\textrm{Ai}\left( z\right) ,\end{displaymath}

which gives a means to check the integrator using special.airy.

First, convert this ODE into standard form by setting \( \mathbf{y}=\left[ \frac{dw}{dz},w\right] \) and \( t=z. \) Thus, the differential equation becomes

\begin{displaymath}
\frac{d\mathbf{y}}{dt}=\left[ \begin{array}{c}
ty_{1}\\
y_{...
...\begin{array}{cc}
0 & t\\
1 & 0
\end{array}\right] \mathbf{y}.\end{displaymath}

In other words,

\begin{displaymath}
\mathbf{f}\left( \mathbf{y},t\right) =\mathbf{A}\left( t\right) \mathbf{y}.\end{displaymath}

As an interesting reminder, if \( \mathbf{A}\left( t\right) \) commutes with \( \int _{0}^{t}\mathbf{A}\left( \tau \right)   d\tau \) under matrix multiplication, then this linear differential equation has an exact solution using the matrix exponential:

\begin{displaymath}
\mathbf{y}\left( t\right) =\exp \left( \int _{0}^{t}\mathbf{A}\left( \tau \right) d\tau \right) \mathbf{y}\left( 0\right) ,\end{displaymath}

However, in this case, \( \mathbf{A}\left( t\right) \) and its integral do not commute.

There are many optional inputs and outputs available when using odeint which can help tune the solver. These additional inputs and outputs are not needed much of the time, however, and the three required input arguments and the output solution suffice. The required inputs are the function defining the derivative, fprime, the initial conditions vector, y0, and the time points to obtain a solution, t, (with the initial value point as the first element of this sequence). The output to odeint is a matrix where each row contains the solution vector at each requested time point (thus, the initial conditions are given in the first output row).

The following example illustrates the use of odeint including the usage of the Dfun option which allows the user to specify a gradient (with respect to \( \mathbf{y} \)) of the function, \( \mathbf{f}\left( \mathbf{y},t\right) \).


>>> from scipy.integrate import odeint
>>> from scipy.special import gamma, airy
>>> y1_0 = 1.0/3**(2.0/3.0)/gamma(2.0/3.0)
>>> y0_0 = -1.0/3**(1.0/3.0)/gamma(1.0/3.0)
>>> y0 = [y0_0, y1_0]
>>> def func(y, t):
        return [t*y[1],y[0]]

>>> def gradient(y,t):
        return [[0,t],[1,0]]

>>> x = arange(0,4.0, 0.01)
>>> t = x
>>> ychk = airy(x)[0]
>>> y = odeint(func, y0, t)
>>> y2 = odeint(func, y0, t, Dfun=gradient)

>>> import sys
>>> sys.float_output_precision = 6
>>> print ychk[:36:6]
[ 0.355028  0.339511  0.324068  0.308763  0.293658  0.278806]

>>> print y[:36:6,1]
[ 0.355028  0.339511  0.324067  0.308763  0.293658  0.278806]

>>> print y2[:36:6,1]
[ 0.355028  0.339511  0.324067  0.308763  0.293658  0.278806]



Next: Gaussian quadrature (integrate.gauss_quadtol) Up: Integration (integrate) Previous: General integration (integrate.quad)
2001-07-27