Main Content

ode23

Solve nonstiff differential equations — low order method

Description

example

[t,y] = ode23(odefun,tspan,y0), wheretspan = [t0 tf], integrates the system of differential equations y ' = f ( t , y ) fromt0totfwith initial conditionsy0. Each row in the solution arrayycorresponds to a value returned in column vectort.

All MATLAB®ODE solvers can solve systems of equations of the form y ' = f ( t , y ) , or problems that involve a mass matrix, M ( t , y ) y ' = f ( t , y ) . The solvers all use similar syntaxes. Theode23ssolver only can solve problems with a mass matrix if the mass matrix is constant.ode15sandode23tcan solve problems with a mass matrix that is singular, known as differential-algebraic equations (DAEs). Specify the mass matrix using theMassoption ofodeset.

example

[t,y] = ode23(odefun,tspan,y0,options)also uses the integration settings defined byoptions, which is an argument created using theodesetfunction. For example, use theAbsTolandRelToloptions to specify absolute and relative error tolerances, or theMassoption to provide a mass matrix.

[t,y,te,ye,ie] = ode23(odefun,tspan,y0,options)additionally finds where functions of(t,y), called event functions, are zero. In the output,teis the time of the event,yeis the solution at the time of the event, andieis the index of the triggered event.

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'Events'property to a function, such asmyEventFcnor@myEventFcn, and creating a corresponding function: [value,isterminal,direction] =myEventFcn(t,y). For more information, seeODE Event Location.

sol= ode23(___)returns a structure that you can use withdevalto evaluate the solution at any point on the interval[t0 tf]. You can use any of the input argument combinations in previous syntaxes.

Examples

collapse all

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 in the function.

Solve the ODE

y = 2 t .

Specify a time interval of[0 5]and the initial conditiony0 = 0.

tspan = [0 5]; y0 = 0; [t,y] = ode23(@(t,y) 2*t, tspan, y0);

Plot the solution.

plot(t,y,'-o')

Figure contains an axes object. The axes object contains an object of type line.

The van der Pol equation is a second-order ODE

$$y''_1 - \mu \left( 1 - y_1^2\right) y'_1+y_1=0,$$

where$\mu > 0$is a scalar parameter. Rewrite this equation as a system of first-order ODEs by making the substitution$y'_1 = y_2$. The resulting system of first-order ODEs is

$$
\begin{array}{cl}
y'_1 &= y_2\\
y'_2 &= \mu (1-y_1^2) y_2 - y_1.\end{array}
$$

The function filevdp1.mrepresents the van der Pol equation using$\mu = 1$. The variables$y_1$and$y_2$are the entriesy(1)andy(2)of a two-element vectordydt.

functiondydt = 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 theode23function on the time interval[0 20]with initial values[2 0]. The resulting output is a column vector of time pointstand a solution arrayy. Each row inycorresponds to a time returned in the corresponding row oft. The first column ofycorresponds to$y_1$, and the second column corresponds to$y_2$.

[t,y] = ode23(@vdp1,[0 20],[2; 0]);

Plot the solutions for$y_1$and$y_2$againstt.

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仅适用于使用两个输入argum的功能ents,tandy. 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

$$y'' = \frac{A}{B} t y.$$

Rewriting the equation as a first-order system yields

$$\begin{array}{cl} y'_1 &= y_2\\ y'_2 &= \frac{A}{B} t y_1.
\end{array}$$

odefcn.mrepresents this system of equations as a function that accepts four input arguments:t,y,A, andB.

functiondydt = odefcn(t,y,A,B) dydt = zeros(2,1); dydt(1) = y(2); dydt(2) = (A/B)*t.*y(1);

Solve the ODE usingode23. Specify the function handle such that it passes in the predefined values forAandBtoodefcn.

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.

plot(t,y(:,1),'-o',t,y(:,2),'-.')

Compared toode45, theode23solver is better at solving problems with crude error tolerances.

Compare the performance ofode45andode23by solving the moderately-stiff ODE

y = - λ y

for λ = 1 0 0 0 . This ODE is a test equation that becomes increasingly stiff as λ increases. Useodesetto turn on the display of solver statistics.

opts = odeset('Stats','on'); tspan = [0 2]; y0 = 1; lambda = 1e3; subplot(1,2,1) tic, ode45(@(t,y) -lambda*y, tspan, y0, opts), toc
615 successful steps 35 failed attempts 3901 function evaluations Elapsed time is 1.169297 seconds.
title('ode45'次要情节(1、2、2)抽搐,ode23 (@ (t、y)λ* y, tspan, y0, opts), toc
822 successful steps 2 failed attempts 2473 function evaluations Elapsed time is 0.568386 seconds.
title('ode23')

{

For this moderately stiff problem,ode23executes slightly faster thanode45and also has fewer failed steps. The step sizes taken byode45andode23for this problem are limited by the stability requirements of the equation rather than by accuracy. Since steps taken byode23are cheaper than withode45, theode23solver executes quicker even though it takes more steps.

Input Arguments

collapse all

Functions to solve, specified as a function handle that defines the functions to be integrated.

The functiondydt = odefun(t,y), for a scalartand a column vectory, must return a column vectordydtof data typesingleordoublethat corresponds to f ( t , y ) .odefunmust accept both input argumentstandy, even if one of the arguments is not used in the function.

For example, to solve y ' = 5 y 3 , use the function:

functiondydt = odefun(t,y) dydt = 5*y-3;end

For a system of equations, the output ofodefunis a vector. Each element in the vector is the solution to one equation. For example, to solve

y ' 1 = y 1 + 2 y 2 y ' 2 = 3 y 1 + 2 y 2

use the function:

functiondydt = odefun(t,y) dydt = zeros(2,1); dydt(1) = y(1)+2*y(2); dydt(2) = 3*y(1)+2*y(2);end

For information on how to provide additional parameters to the functionodefun, seeParameterizing Functions.

Example:@myFcn

Data Types:function_handle

Interval of integration, specified as a vector. At a minimum,tspanmust be a two-element vector[t0 tf]specifying the initial and final times. To obtain solutions at specific times betweent0andtf, use a longer vector of the form[t0,t1,t2,...,tf]. The elements intspanmust be all increasing or all decreasing.

The solver imposes the initial conditions given byy0at the initial timetspan(1), and then integrates fromtspan(1)totspan(end):

  • Iftspanhas two elements[t0 tf], then the solver returns the solution evaluated at each internal integration step within the interval.

  • Iftspanhas more than two elements[t0,t1,t2,...,tf], then the solver returns the solution evaluated at the given points. However, the solver does not step precisely to each point specified intspan. Instead, the solver uses its own internal steps to compute the solution, and then evaluates the solution at the requested points intspan. The solutions produced at the specified points are of the same order of accuracy as the solutions computed at each internal step.

    Specifying several intermediate points has little effect on the efficiency of computation, but can affect memory management for large systems.

The values oftspanare used by the solver to calculate suitable values forInitialStepandMaxStep:

  • Iftspancontains several intermediate points[t0,t1,t2,...,tf], then the specified points give an indication of the scale for the problem, which can affect the value ofInitialStepused by the solver. Therefore, the solution obtained by the solver might be different depending on whether you specifytspanas a two-element vector or as a vector with intermediate points.

  • The initial and final values intspanare used to calculate the maximum step sizeMaxStep. Therefore, changing the initial or final values intspancan cause the solver to use a different step sequence, which might change the solution.

Example:[1 10]

Example:[1 3 5 7 9 10]

Data Types:single|double

Initial conditions, specified as a vector.y0must be the same length as the vector output ofodefun, so thaty0contains an initial condition for each equation defined inodefun.

Data Types:single|double

Option structure, specified as a structure array. Use theodesetfunction to create or modify theoptionsstructure. SeeSummary of ODE Optionsfor a list of the options compatible with each solver.

Example:options = odeset('RelTol',1e-5,'Stats','on','OutputFcn',@odeplot)specifies a relative error tolerance of1e-5, turns on the display of solver statistics, and specifies the output function@odeplotto plot the solution as it is computed.

Data Types:struct

Output Arguments

collapse all

Evaluation points, returned as a column vector.

  • Iftspancontains two elements[t0 tf], thentcontains the internal evaluation points used to perform the integration.

  • Iftspancontains more than two elements, thentis the same astspan.

Solutions, returned as an array. Each row inycorresponds to the solution at the value returned in the corresponding row oft.

Time of events, returned as a column vector. The event times intecorrespond to the solutions returned inye, andiespecifies which event occurred.

Solution at time of events, returned as an array. The event times intecorrespond to the solutions returned inye, andiespecifies which event occurred.

Index of triggered event function, returned as a column vector. The event times intecorrespond to the solutions returned inye, andiespecifies which event occurred.

Structure for evaluation, returned as a structure array. Use this structure with thedevalfunction to evaluate the solution at any point in the interval[t0 tf]. Thesolstructure array always includes these fields:

Structure Field Description

sol.x

Row vector of the steps chosen by the solver.

sol.y

Solutions. Each columnsol.y(:,i)contains the solution at timesol.x(i).

sol.solver

Solver name.

Additionally, if you specify theEventsoption ofodesetand events are detected, thensolalso includes these fields:

Structure Field Description

sol.xe

Points when events occurred.sol.xe(end)contains the exact point of a terminal event, if any.

sol.ye

Solutions that correspond to events insol.xe.

sol.ie

Indices into the vector returned by the function specified in theEventsoption. The values indicate which event the solver detected.

Algorithms

ode23is an implementation of an explicit Runge-Kutta (2,3) pair of Bogacki and Shampine. It may be more efficient thanode45在原油公差和moderat的存在e stiffness.ode23is a single-step solver[1],[2].

References

[1] Bogacki, P. and L. F. Shampine, “A 3(2) pair of Runge-Kutta formulas,”Appl. Math. Letters, Vol. 2, 1989, pp. 321–325.

[2] Shampine, L. F. and M. W. Reichelt, “The MATLAB ODE Suite,”SIAM Journal on Scientific Computing, Vol. 18, 1997, pp. 1–22.

Extended Capabilities

Version History

Introduced before R2006a