Solve nonstiff differential equations — low order method
[t,y] = ode23(odefun,tspan,y0)
[t,y] = ode23(odefun,tspan,y0,options)
[t,y,te,ye,ie] = ode23(odefun,tspan,y0,options)
sol = ode23(___)
tspan = [t0 tf], integrates the system of
differential equations from
y0. Each row in the solution
y corresponds to a value returned in column
All MATLAB® ODE solvers can solve systems of equations of
the form ,
or problems that involve a mass matrix, .
The solvers all use similar syntaxes. The
only can solve problems with a mass matrix if the mass matrix is constant.
solve problems with a mass matrix that is singular, known as differential-algebraic
equations (DAEs). Specify the mass matrix using the
finds where functions of (t,y),
called event functions, are zero. In the output,
the time of the event,
ye is the solution at the
time of the event, and
ie is the index of the triggered
For each event function, specify whether the integration is
to terminate at a zero and whether the direction of the zero crossing
matters. Do this by setting the
to a function, such as
and creating a corresponding function: [
For more information, see ODE Event Location.
a structure that you can use with
sol = ode23(___)
deval to evaluate
the solution at any point on the interval
You can use any of the input argument combinations in previous syntaxes.
Simple ODEs that have a single solution component can be specified as an anonymous function in the call to the solver. The anonymous function must accept two inputs
(t,y) even if one of the inputs is not used.
Solve the ODE
Use a time interval of
[0,5] and the initial condition
y0 = 0.
tspan = [0 5]; y0 = 0; [t,y] = ode23(@(t,y) 2*t, tspan, y0);
Plot the solution.
The van der Pol equation is a second order ODE
where is a scalar parameter. Rewrite this equation as a system of first-order ODEs by making the substitution . The resulting system of first-order ODEs is
The function file
vdp1.m represents the van der Pol equation using . The variables and are the entries
y(2) of a two-element vector,
function dydt = vdp1(t,y) %VDP1 Evaluate the van der Pol ODEs for mu = 1 % % See also ODE113, ODE23, ODE45. % Jacek Kierzenka and Lawrence F. Shampine % Copyright 1984-2014 The MathWorks, Inc. dydt = [y(2); (1-y(1)^2)*y(2)-y(1)];
Solve the ODE using the
ode23 function on the time interval
[0 20] with initial values
[2 0]. 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 , and the second column to .
[t,y] = ode23(@vdp1,[0 20],[2; 0]);
Plot the solutions for and against
plot(t,y(:,1),'-o',t,y(:,2),'-o') title('Solution of van der Pol Equation (\mu = 1) with ODE23'); xlabel('Time t'); ylabel('Solution y'); legend('y_1','y_2')
ode23 only works with functions that use two input arguments,
y. However, you can pass in extra parameters by defining them outside the function and passing them in when you specify the function handle.
Solve the ODE
Rewriting the equation as a first-order system yields
odefcn.m represents this system of equations as a function that accepts four input arguments:
function dydt = odefcn(t,y,A,B) dydt = zeros(2,1); dydt(1) = y(2); dydt(2) = (A/B)*t.*y(1);
Solve the ODE using
ode23. Specify the function handle such that it passes in the predefined values for
A = 1; B = 2; tspan = [0 5]; y0 = [0 0.01]; [t,y] = ode23(@(t,y) odefcn(t,y,A,B), tspan, y0);
Plot the results.
ode23 solver is better at solving problems with crude error tolerances.
Compare the performance of
ode23 by solving the moderately-stiff ODE
for . This ODE is a test equation that becomes increasingly stiff as increases. Use
odeset to turn on the display of solver statistics.
opts = odeset('Stats','on'); tspan = [0 2]; y0 = 1; lambda = 1e3; subplot(1,2,1) disp('ode45 stats:')
tic, ode45(@(t,y) -lambda*y, tspan, y0, opts), toc
615 successful steps 35 failed attempts 3901 function evaluations Elapsed time is 6.790876 seconds.
title('ode45') subplot(1,2,2) disp(' ')
tic, ode23(@(t,y) -lambda*y, tspan, y0, opts), toc
822 successful steps 2 failed attempts 2473 function evaluations Elapsed time is 1.254777 seconds.
For this moderately stiff problem,
ode23 executes slightly faster than
ode45 and also has fewer failed steps. The step sizes taken by
ode23 for this problem are limited by the stability requirements of the equation rather than by accuracy. Since steps taken by
ode23 are cheaper than with
ode23 solver executes quicker even though it takes more steps.
ode23 is an implementation of an explicit
Runge-Kutta (2,3) pair of Bogacki and Shampine. It may be more efficient
ode45 at crude tolerances and in the presence
of moderate stiffness.
ode23 is a single-step
solver , .
 Bogacki, P. and L. F. Shampine, “A 3(2) pair of Runge-Kutta formulas,” Appl. Math. Letters, Vol. 2, 1989, pp. 321–325.
 Shampine, L. F. and M. W. Reichelt, “The MATLAB ODE Suite,” SIAM Journal on Scientific Computing, Vol. 18, 1997, pp. 1–22.
Usage notes and limitations:
arguments must be constant.
Code generation does not support a constant mass matrix in the options structure. Provide a mass matrix as a function.
You must provide at least the two output arguments
Input types must be homogeneous—all double or all single.
Variable-sizing support must be enabled. Code generation
requires dynamic memory allocation when
two elements or you use event functions.