An ordinary differential equation (ODE) contains one or more derivatives of a dependent variable, y, with respect to a single independent variable, t, usually referred to as time. The notation used here for representing derivatives of y with respect to t is for a first derivative, for a second derivative, and so on. The order of the ODE is equal to the highest-order derivative of y that appears in the equation.
For example, this is a second order ODE:
In an initial value problem, the ODE is solved by starting from an initial state. Using the initial condition, , as well as a period of time over which the answer is to be obtained, , the solution is obtained iteratively. At each step the solver applies a particular algorithm to the results of previous steps. At the first such step, the initial condition provides the necessary information that allows the integration to proceed. The final result is that the ODE solver returns a vector of time steps as well as the corresponding solution at each step .
The ODE solvers in MATLAB® solve these types of first-order ODEs:
Explicit ODEs of the form .
Linearly implicit ODEs of the form , where is a nonsingular mass matrix. The mass matrix can be time- or state-dependent, or it can be a constant matrix. Linearly implicit ODEs involve linear combinations of the first derivative of y, which are encoded in the mass matrix.
Linearly implicit ODEs can always be transformed to an explicit form, . However, specifying the mass matrix directly to the ODE solver avoids this transformation, which is inconvenient and can be computationally expensive.
If some components of are
missing, then the equations are called differential algebraic
equations, or DAEs, and the system of DAEs contains some algebraic
variables. Algebraic variables are dependent variables
whose derivatives do not appear in the equations. A system of DAEs
can be rewritten as an equivalent system of first-order ODEs by taking
derivatives of the equations to eliminate the algebraic variables.
The number of derivatives needed to rewrite a DAE as an ODE is called
the differential index. The
can solve index-1 DAEs.
Fully implicit ODEs of the form .
Fully implicit ODEs cannot be rewritten in an explicit form, and might
also contain some algebraic variables. The
is designed for fully implicit problems, including index-1 DAEs.
You can supply additional information to the solver for some
types of problems by using the
to create an options structure.
You can specify any number of coupled ODE equations to solve, and in principle the number of equations is only limited by available computer memory. If the system of equations has n equations,
then the function that encodes the equations returns a vector with n elements, corresponding to the values for . For example, consider the system of two equations
A function that encodes these equations is
function dy = myODE(t,y) dy(1) = y(2); dy(2) = y(1)*y(2)-2;
The MATLAB ODE solvers only solve first-order equations. You must rewrite higher-order ODEs as an equivalent system of first-order equations using the generic substitutions
The result of these substitutions is a system of n first-order equations
For example, consider the third-order ODE
Using the substitutions
results in the equivalent first-order system
The code for this system of equations is then
function dydt = f(t,y) dydt(1) = y(2); dydt(2) = y(3); dydt(3) = y(1)*y(3)-1;
Consider the complex ODE equation
where . To solve it, separate the real and imaginary parts into different solution components, then recombine the results at the end. Conceptually, this looks like
For example, if the ODE is , then you can represent the equation using a function file.
function f = complexf(t,y) % Define function that takes and returns complex values f = y.*t + 2*i;
Then, the code to separate the real and imaginary parts is
function fv = imaginaryODE(t,yv) % Construct y from the real and imaginary components y = yv(1) + i*yv(2); % Evaluate the function yp = complexf(t,y); % Return real and imaginary in separate components fv = [real(yp); imag(yp)];
When you run a solver to obtain the solution, the initial condition
also separated into real and imaginary parts to provide an initial
condition for each solution component.
y0 = 1+i; yv0 = [real(y0); imag(y0)]; tspan = [0 2]; [t,yv] = ode45(@imaginaryODE, tspan, yv0);
Once you obtain the solution, combine the real and imaginary components together to obtain the final result.
y = yv(:,1) + i*yv(:,2);
ode45 performs well with most ODE problems
and should generally be your first choice of solver. However,
be more efficient than
ode45 for problems with
looser or tighter accuracy requirements.
Some ODE problems exhibit stiffness, or
difficulty in evaluation. Stiffness is a term that defies a precise
definition, but in general, stiffness occurs when there is a difference
in scaling somewhere in the problem. For example, if an ODE has two
solution components that vary on drastically different time scales,
then the equation might be stiff. You can identify a problem as stiff
if nonstiff solvers (such as
ode45) are unable
to solve the problem or are extremely slow. If you observe that a
nonstiff solver is very slow, try using a stiff solver such as
When using a stiff solver, you can improve reliability and efficiency
by supplying the Jacobian matrix or its sparsity pattern.
This table provides general guidelines on when to use each of the different solvers.
|Solver||Problem Type||Accuracy||When to Use|
Most of the time.
|Low to High|
|Stiff||Low to Medium|
If there is a mass matrix, it must be constant.
For details and further recommendations about when to use each solver, see .
There are several example files available that serve as excellent starting points for most ODE problems. To run the Differential Equations Examples app, which lets you easily explore and run examples, type
To open an individual example file for editing, type
To run an example, type
This table contains a list of the available ODE and DAE example files as well as the solvers and options they use. Links are included for the subset of examples that are also published directly in the documentation.
|Example File||Solver Used||Options Specified||Description||Documentation Link|
Stiff DAE — electrical circuit with constant, singular mass matrix
|Solve Stiff Differential Algebraic Equation|
Simple event location — bouncing ball
|ODE Event Location|
ODE with time- and state-dependent mass matrix — motion of a baton
Stiff large problem — diffusion in a chemical reaction (the Brusselator)
|Solve Stiff ODEs|
ODE with strongly state-dependent mass matrix — Burgers' equation solved using a moving mesh technique
Stiff problem with a time-dependent mass matrix — finite element method
Stiff problem with a constant mass matrix — finite element method
Stiff ODE problem solved on a very long interval — Robertson chemical reaction
Stiff, linearly implicit DAE from a conservation law — Robertson chemical reaction
|Solve Robertson Problem as Semi-Explicit Differential Algebraic Equations (DAEs)|
Stiff, fully implicit DAE — Robertson chemical reaction
|Solve Robertson Problem as Implicit Differential Algebraic Equations (DAEs)|
Implicit ODE system — Burgers’ equation
The “knee problem” with nonnegativity constraints
|Nonnegative ODE Solution|
Advanced event location — restricted three body problem
|ODE Event Location|
Nonstiff problem — Euler equations of a rigid body without external forces
|Solve Nonstiff ODEs|
Parameterizable van der Pol equation (stiff for large μ)
|Solve Stiff ODEs|
 Shampine, L. F. and M. K. Gordon, Computer Solution of Ordinary Differential Equations: the Initial Value Problem, W. H. Freeman, San Francisco, 1975.
 Forsythe, G., M. Malcolm, and C. Moler, Computer Methods for Mathematical Computations, Prentice-Hall, New Jersey, 1977.
 Kahaner, D., C. Moler, and S. Nash, Numerical Methods and Software, Prentice-Hall, New Jersey, 1989.
 Shampine, L. F., Numerical Solution of Ordinary Differential Equations, Chapman & Hall, New York, 1994.
 Shampine, L. F. and M. W. Reichelt, “The MATLAB ODE Suite,” SIAM Journal on Scientific Computing, Vol. 18, 1997, pp. 1–22.
 Shampine, L. F., Gladwell, I. and S. Thompson, Solving ODEs with MATLAB, Cambridge University Press, Cambridge UK, 2003.