Mathematics Previous page   Next Page

Examples: Solving Explicit ODE Problems

This section uses the van der Pol equation

to describe the process for solving initial value ODE problems using the ODE solvers.

Example: Solving an IVP ODE (van der Pol Equation, Nonstiff)

This example explains and illustrates the steps you need to solve an initial value ODE problem:

  1. Rewrite the problem as a system of first-order ODEs. Rewrite the van der Pol equation (second-order)
  1. where 0 -->mu > is a scalar parameter, by making the substitution y sub 1 prime  = y sub 2. The resulting system of first-order ODEs is

  1. See Working with Higher Order ODEs for more information.

  1. Code the system of first-order ODEs. Once you represent the equation as a system of first-order ODEs, you can code it as a function that an ODE solver can use. The function must be of the form
  1. Although t and y must be the function's two arguments, the function does not need to use them. The output dydt, a column vector, is the derivative of y.

    The code below represents the van der Pol system in the function, vdp1. The vdp1 function assumes that mu equals 1. The variables y sub 1 and y sub 2 are the entries y(1) and y(2) of a two-element vector.

    Note that, although vdp1 must accept the arguments t and y, it does not use t in its computations.

  1. Apply a solver to the problem. Decide which solver you want to use to solve the problem. Then call the solver and pass it the function you created to describe the first-order system of ODEs, the time interval on which you want to solve the problem, and an initial condition vector. See Examples: Solving Explicit ODE Problems and the ODE solver reference page for descriptions of the ODE solvers.
  1. For the van der Pol system, you can use ode45 on time interval [0 20] with initial values y(1) = 2 and y(2) = 0.

    This example uses @ to pass vdp1 as a function handle to ode45. The resulting output is a column vector of time points t and a solution array y. Each row in y corresponds to a time returned in the corresponding row of t. The first column of y corresponds to y sub 1, and the second column to y sub 2.

  1. View the solver output. You can simply use the plot command to view the solver output.
  1. As an alternative, you can use a solver output function to process the output. The solver calls the function specified in the integration property OutputFcn after each successful time step. Use odeset to set OutputFcn to the desired function. See Solver Output Properties, in the reference page for odeset, for more information about OutputFcn.

Example: The van der Pol Equation, µ = 1000 (Stiff)

This example presents a stiff problem. For a stiff problem, solutions can change on a time scale that is very short compared to the interval of integration, but the solution of interest changes on a much longer time scale. Methods not designed for stiff problems are ineffective on intervals where the solution changes slowly because they use time steps small enough to resolve the fastest possible change.

When mu is increased to 1000, the solution to the van der Pol equation changes dramatically and exhibits oscillation on a much longer time scale. Approximating the solution of the initial value problem becomes a more difficult task. Because this particular problem is stiff, a solver intended for nonstiff problems, such as ode45, is too inefficient to be practical. A solver such as ode15s is intended for such stiff problems.

The vdp1000 function evaluates the van der Pol system from the previous example, but with mu = 1000.

Now use the ode15s function to solve the problem with the initial condition vector of [2; 0], but a time interval of [0 3000]. For scaling reasons, plot just the first component of y(t).

Parameterizing an ODE Function

The preceding sections showed how to solve the van der Pol equation for two different values of the parameter µ. In those examples, the values µ = 1 and µ=1000 are hard-coded in the ODE functions. If you are solving an ODE for several different parameter values, it might be more convenient to include the parameter in the ODE function and assign a value to the parameter each time you run the ODE solver. This section explains how to do this for the van der Pol equation.

One way to provide parameter values to the ODE function is to write an M-file that

The following code illustrates this:

Because the ODE function vdp is a nested function, the value of the parameter mu is available to it.

To run the M-file for mu = 1, as in Example: Solving an IVP ODE (van der Pol Equation, Nonstiff), enter

To run the code for µ = 1000, as in Example: The van der Pol Equation, µ = 1000 (Stiff), enter

See the vdpode code for a complete example based on these functions.

Evaluating the Solution at Specific Points

The numerical methods implemented in the ODE solvers produce a continuous solution over the interval of integration [a, b]. You can evaluate the approximate solution, S(x), at any point in [a, b] using the function deval and the structure sol returned by the solver. For example, if you solve the problem described in Example: Solving an IVP ODE (van der Pol Equation, Nonstiff) by calling ode45 with a single output argument sol,

ode45 returns the solution as a structure. You can then evaluate the approximate solution at points in the vector xint = 1:5 as follows:

The deval function is vectorized. For a vector xint, the ith column of Sxint approximates the solution y(xint(i)).


Previous page  Solvers for Explicit and Linearly Implicit ODEs Solver for Fully Implicit ODEs Next page

© 1994-2005 The MathWorks, Inc.