Main Content

ode78

Solve nonstiff differential equations — high order method

Since R2021b

Description

[t,y] = ode78(odefun,tspan,y0), where tspan = [t0 tf], integrates the system of differential equations y'=f(t,y) from t0 to tf with initial conditions y0. Each row in the solution array y corresponds to a value returned in column vector t.

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 use similar syntaxes. The ode23s solver can solve problems with a mass matrix only if the mass matrix is constant. ode15s and ode23t can solve problems with a mass matrix that is singular, known as differential-algebraic equations (DAEs). Specify the mass matrix using the Mass option of odeset.

example

[t,y] = ode78(odefun,tspan,y0,options) also uses the integration settings defined by options, which is an argument created using the odeset function. For example, set the AbsTol and RelTol options to specify absolute and relative error tolerances, or set the Mass option to provide a mass matrix.

[t,y,te,ye,ie] = ode78(odefun,tspan,y0,options) additionally finds where functions of (t,y), called event functions, are zero. In the output, te is the time of the event, ye is the solution at the time of the event, and ie is 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 is significant. Do this by setting the 'Events' option of odeset to a function, such as myEventFcn or @myEventFcn, and create a corresponding function: [value,isterminal,direction] = myEventFcn(t,y). For more information, see ODE Event Location.

sol = ode78(___) returns a structure that you can use with deval to 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=2t.

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

tspan = [0 5];
y0 = 0;
[t,y] = ode78(@(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

y1-μ(1-y12)y1+y1=0,

where μ>0 is a scalar parameter. Rewrite this equation as a system of first-order ODEs by making the substitution y1=y2. The resulting system of first-order ODEs is

y1=y2y2=μ(1-y12)y2-y1.

The function file vdp1.m represents the van der Pol equation using μ=1. The variables y1 and y2 are the entries y(1) and y(2) of a two-element vector dydt.

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 ode78 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 y1, and the second column corresponds to y2.

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

Plot the solutions for y1 and y2 against t.

plot(t,y(:,1),'-o',t,y(:,2),'-o')
title('Solution of van der Pol Equation (\mu = 1) with ODE78');
xlabel('Time t');
ylabel('Solution y');
legend('y_1','y_2')

Figure contains an axes object. The axes object with title Solution of van der Pol Equation ( mu blank = blank 1 ) with ODE78, xlabel Time t, ylabel Solution y contains 2 objects of type line. These objects represent y_1, y_2.

ode78 works only with functions that use two input arguments, t and y. However, you can pass extra parameters by defining them outside the function and passing them in when you specify the function handle.

Solve the ODE

y=ABty.

Rewriting the equation as a first-order system yields

y1=y2y2=ABty1.

odefcn, a local function at the end of this example, represents this system of equations as a function that accepts four input arguments: t, y, A, and B.

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

Solve the ODE using ode78. Specify the function handle so that it passes the predefined values for A and B to odefcn.

A = 1;
B = 2;
tspan = [0 5];
y0 = [0 0.01];
[t,y] = ode78(@(t,y) odefcn(t,y,A,B), tspan, y0);

Plot the results.

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

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

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

Compared to ode45, the ode113, ode78, and ode89 solvers are better at solving problems with stringent error tolerances. A common situation where these solvers excel is in orbital dynamics problems, where the solution curve is smooth and requires high accuracy in each step from the solver.

The two-body problem considers two interacting masses m1 and m2 orbiting a common plane. In this example, one of the masses is significantly larger than the other. With the heavy body at the origin, the equations of motion are

x=-x/r3y=-y/r3,

where

r=x2+y2.

To solve the problem, first convert to a system of four first-order ODEs using the substitutions

y1=xy2=xy3=yy4=y.

The substitutions produce the first-order system

y1=y2y2=-y1/r3y3=y4y4=-y3/r3.

twobodyode, a local function included at the end of this example, codes the system of equations for the two-body problem.

function dy = twobodyode(t,y)
% Two-body problem with one mass much larger than the other.
r = sqrt(y(1)^2 + y(3)^2);
dy = [y(2); 
     -y(1)/r^3;
      y(4);
     -y(3)/r^3];
end

Solve the system of ODEs using ode78. Specify stringent error tolerances of 1e-13 for RelTol and 1e-14 for AbsTol.

opts = odeset('Reltol',1e-13,'AbsTol',1e-14,'Stats','on');
tspan = [0 10*pi];
y0 = [2 0 0 0.5];
[t,y] = ode78(@twobodyode, tspan, y0, opts);
341 successful steps
0 failed attempts
5797 function evaluations
plot(t,y)
legend('x','x''','y','y''','Location','SouthEast')
title('Position and Velocity Components')

Figure contains an axes object. The axes object with title Position and Velocity Components contains 4 objects of type line. These objects represent x, x', y, y'.

figure
plot(y(:,1),y(:,3),'-o',0,0,'ro')
axis equal
title('Orbit of Smaller Mass')

Figure contains an axes object. The axes object with title Orbit of Smaller Mass contains 2 objects of type line. One or more of the lines displays its values using only markers

Compared to ode45, the ode78 solver is able to obtain the solution faster and with fewer steps and function evaluations.

function dy = twobodyode(t,y)
% Two-body problem with one mass much larger than the other.
r = sqrt(y(1)^2 + y(3)^2);
dy = [y(2); 
    -y(1)/r^3;
    y(4);
    -y(3)/r^3];
end

Input Arguments

collapse all

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

The function dydt = odefun(t,y), for a scalar t and a column vector y, must return a column vector dydt of data type single or double that corresponds to f(t,y). odefun must accept both input arguments t and y, even if one of the arguments is not used in the function.

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

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

For a system of equations, the output of odefun is a vector. Each element in the vector is the computed value of the right side of one equation. For example, consider the system of two equations

y'1=y1+2y2y'2=3y1+2y2

A function that calculates the value of the right side of each equation at each time step is

function dydt = 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 function odefun, see Parameterizing Functions.

Example: @myFcn

Data Types: function_handle

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

The solver imposes the initial conditions given by y0 at the initial time tspan(1), and then integrates from tspan(1) to tspan(end):

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

  • If tspan has 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 in tspan. Instead, the solver uses its own internal steps to compute the solution, and then evaluates the solution at the requested points in tspan. 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 of tspan are used by the solver to calculate suitable values for InitialStep and MaxStep:

  • If tspan contains 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 of InitialStep used by the solver. Therefore, the solution obtained by the solver might be different depending on whether you specify tspan as a two-element vector or as a vector with intermediate points.

  • The initial and final values in tspan are used to calculate the maximum step size MaxStep. Therefore, changing the initial or final values in tspan can 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. y0 must be the same length as the vector output of odefun, so that y0 contains an initial condition for each equation defined in odefun.

Data Types: single | double

Option structure, specified as a structure array. Use the odeset function to create or modify the options structure. See Summary of ODE Options for a list of the options compatible with each solver.

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

Data Types: struct

Output Arguments

collapse all

Evaluation points, returned as a column vector.

  • If tspan contains two elements [t0 tf], then t contains the internal evaluation points used to perform the integration.

  • If tspan contains more than two elements, then t is the same as tspan.

Solutions, returned as an array. Each row in y corresponds to the solution at the value returned in the corresponding row of t.

Time of events, returned as a column vector. The event times in te correspond to the solutions returned in ye, and ie specifies which event occurred.

Solution at time of events, returned as an array. The event times in te correspond to the solutions returned in ye, and ie specifies which event occurred.

Index of triggered event function, returned as a column vector. The event times in te correspond to the solutions returned in ye, and ie specifies which event occurred.

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

Structure FieldDescription

sol.x

Row vector of the steps chosen by the solver.

sol.y

Solutions. Each column sol.y(:,i) contains the solution at time sol.x(i).

sol.solver

Solver name.

Additionally, if you specify the Events option of odeset and events are detected, then sol also includes these fields:

Structure FieldDescription

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 in sol.xe.

sol.ie

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

Algorithms

ode78 is an implementation of Verner's "most efficient" Runge-Kutta 8(7) pair with a 7th-order continuous extension. The solution is advanced with the 8th-order result. The 7th-order continuous extension requires four additional evaluations of odefun, but only on steps that require interpolation.

References

[1] Verner, J. H. “Numerically Optimal Runge–Kutta Pairs with Interpolants.” Numerical Algorithms 53, no. 2–3 (March 2010): 383–396. https://doi.org/10.1007/s11075-009-9290-3.

Extended Capabilities

Version History

Introduced in R2021b