Solve nonstiff differential equations — mediumorder method
collapse all in page
Syntax
[t,y] =ode45(odefun,tspan,y0)
[t,y] =ode45(odefun,tspan,y0,options)
[t,y,te,ye,ie]= ode45(odefun,tspan,y0,options)
sol = ode45(___)
Description
example
[t,y] =ode45(odefun,tspan,y0)
,where tspan = [t0 tf]
, integrates the system ofdifferential equations from t0
to tf
withinitial conditions y0
. Each row in the solutionarray y
corresponds to a value returned in columnvector t
.
All MATLAB® ODE solvers can solve systems of equations ofthe form ,or problems that involve a mass matrix, .The solvers all use similar syntaxes. The ode23s
solveronly can solve problems with a mass matrix if the mass matrix is constant. ode15s
and ode23t
cansolve problems with a mass matrix that is singular, known as differential-algebraicequations (DAEs). Specify the mass matrix using the Mass
optionof odeset.
ode45
is a versatile ODE solver and is thefirst solver you should try for most problems. However, if the problemis stiff or requires high accuracy, then there are other ODE solversthat might be better suited to the problem. See Choose an ODE Solver formore information.
example
[t,y] =ode45(odefun,tspan,y0,options)
alsouses the integration settings defined by options
,which is an argument created using the odeset
function.For example, use the AbsTol
and RelTol
optionsto specify absolute and relative error tolerances, or the Mass
optionto provide a mass matrix.
[t,y,te,ye,ie]= ode45(odefun,tspan,y0,options)
additionallyfinds where functions of (t,y),called event functions, are zero. In the output, te
isthe time of the event, ye
is the solution at thetime of the event, and ie
is the index of the triggeredevent.
For each event function, specify whether the integration isto terminate at a zero and whether the direction of the zero crossingmatters. Do this by setting the 'Events'
propertyto a function, such as myEventFcn
or @myEventFcn
,and creating a corresponding function: [value
,isterminal
,direction
]= myEventFcn
(t
,y
).For more information, see ODE Event Location.
example
sol = ode45(___)
returnsa structure that you can use with deval
to evaluatethe solution at any point on the interval [t0 tf]
.You can use any of the input argument combinations in previous syntaxes.
Examples
collapse all
ODE with Single Solution Component
Open Live Script
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
Specify a time interval of [0 5]
and the initial condition y0 = 0
.
tspan = [0 5];y0 = 0;[t,y] = ode45(@(t,y) 2*t, tspan, y0);
Plot the solution.
plot(t,y,'-o')
Solve Nonstiff Equation
Open Live Script
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(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 ode45
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 corresponds to .
[t,y] = ode45(@vdp1,[0 20],[2; 0]);
Plot the solutions for and against t
.
plot(t,y(:,1),'-o',t,y(:,2),'-o')title('Solution of van der Pol Equation (\mu = 1) with ODE45');xlabel('Time t');ylabel('Solution y');legend('y_1','y_2')
Pass Extra Parameters to ODE Function
Open Live Script
ode45
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
Rewriting the equation as a first-order system yields
odefcn
, a local function included 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 ode45
. 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] = ode45(@(t,y) odefcn(t,y,A,B), tspan, y0);
Plot the results.
plot(t,y(:,1),'-o',t,y(:,2),'-.')
function dydt = odefcn(t,y,A,B) dydt = zeros(2,1); dydt(1) = y(2); dydt(2) = (A/B)*t.*y(1);end
Solve ODE with Multiple Initial Conditions
Open Live Script
For simple ODE systems with one equation, you can specify y0
as a vector containing multiple initial conditions. This technique creates a system of independent equations through scalar expansion, one for each initial value, and ode45
solves the system to produce results for each initial value.
Create an anonymous function to represent the equation . The function must accept two inputs for t
and y
.
yprime = @(t,y) -2*y + 2*cos(t).*sin(2*t);
Create a vector of different initial conditions in the range .
y0 = -5:5;
Solve the equation for each initial condition over the time interval using ode45
.
tspan = [0 3];[t,y] = ode45(yprime,tspan,y0);
Plot the results.
plot(t,y)grid onxlabel('t')ylabel('y')title('Solutions of y'' = -2y + 2 cos(t) sin(2t), y(0) = -5,-4,...,4,5','interpreter','latex')
This technique is useful for solving simple ODEs with several initial conditions. However, the technique also has some tradeoffs:
You cannot solve systems of equations with multiple initial conditions. The technique only works when solving one equation with multiple initial conditions.
The time step chosen by the solver at each step is based on the equation in the system that needs to take the smallest step. This means the solver can take small steps to satisfy the equation for one initial condition, but the other equations, if solved on their own, would use different step sizes. Despite this, solving for multiple initial conditions at the same time is generally faster than solving the equations separately using a
for
-loop.
For more information on this technique, see Solve System of ODEs with Multiple Initial Conditions.
ODE with Time-Dependent Terms
Open Script
Consider the following ODE with time-dependent parameters
The initial condition is . The function f(t)
is defined by the n-by-1 vector f
evaluated at times ft
. The function g(t)
is defined by the m-by-1 vector g
evaluated at times gt
.
Create the vectors f
and g
.
ft = linspace(0,5,25);f = ft.^2 - ft - 3;gt = linspace(1,6,25);g = 3*sin(gt-0.25);
Write a function named myode
that interpolates f
and g
to obtain the value of the time-dependent terms at the specified time. Save the function in your current folder to run the rest of the example.
The myode
function accepts extra input arguments to evaluate the ODE at each time step, but ode45
only uses the first two input arguments t
and y
.
function dydt = myode(t,y,ft,f,gt,g)f = interp1(ft,f,t); % Interpolate the data set (ft,f) at time tg = interp1(gt,g,t); % Interpolate the data set (gt,g) at time tdydt = -f.*y + g; % Evaluate ODE at time t
Solve the equation over the time interval [1 5]
using ode45
. Specify the function using a function handle so that ode45
uses only the first two input arguments of myode
. Also, loosen the error thresholds using odeset
.
tspan = [1 5];ic = 1;opts = odeset('RelTol',1e-2,'AbsTol',1e-4);[t,y] = ode45(@(t,y) myode(t,y,ft,f,gt,g), tspan, ic, opts);
Plot the solution, y
, as a function of the time points, t
.
plot(t,y)
Evaluate and Extend Solution Structure
Open Live Script
The van der Pol equation is a second order ODE
Solve the van der Pol equation with using ode45
. The function vdp1.m
ships with MATLAB® and encodes the equations. Specify a single output to return a structure containing information about the solution, such as the solver and evaluation points.
tspan = [0 20];y0 = [2 0];sol = ode45(@vdp1,tspan,y0)
sol = struct with fields: solver: 'ode45' extdata: [1x1 struct] x: [0 1.0048e-04 6.0285e-04 0.0031 0.0157 0.0785 0.2844 0.5407 0.8788 1.4032 1.8905 2.3778 2.7795 3.1285 3.4093 3.6657 3.9275 4.2944 4.9013 5.3506 5.7998 6.2075 6.5387 6.7519 6.9652 7.2247 7.5719 8.1226 8.6122 9.1017 9.5054 ... ] (1x60 double) y: [2x60 double] stats: [1x1 struct] idata: [1x1 struct]
Use linspace
to generate 250 points in the interval [0 20]
. Evaluate the solution at these points using deval
.
x = linspace(0,20,250);y = deval(sol,x);
Plot the first component of the solution.
plot(x,y(1,:))
Extend the solution to using odextend
and add the result to the original plot.
sol_new = odextend(sol,@vdp1,35);x = linspace(20,35,350);y = deval(sol_new,x);hold onplot(x,y(1,:),'r')
Input Arguments
collapse all
Output Arguments
collapse all
Algorithms
ode45
is based on an explicit Runge-Kutta(4,5) formula, the Dormand-Prince pair. It is a single-step solver– in computing y(tn)
,it needs only the solution at the immediately preceding time point, y(tn-1)
[1], [2].
References
[1] Dormand, J. R. and P. J. Prince, “Afamily of embedded Runge-Kutta formulae,” J. Comp.Appl. Math., Vol. 6, 1980, pp. 19–26.
[2] Shampine, L. F. and M. W. Reichelt, “TheMATLAB ODE Suite,” SIAM Journal on ScientificComputing, Vol. 18, 1997, pp. 1–22.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
All
odeset
optionarguments must be constant.Code generation does not support a constant mass matrixin the options structure. Provide a mass matrix as a function.
You must provide at least the two output arguments
T
andY
.Input types must be hom*ogeneous—alldouble or all single.
Variable-sizing support must be enabled. Code generationrequires dynamic memory allocation when
tspan
hastwo elements or you use event functions.
Version History
Introduced before R2006a
See Also
ode23 | ode78 | ode89 | ode113 | ode15s | odeset | odeget | deval | odextend
Topics
- Choose an ODE Solver
- Summary of ODE Options
- Solve Nonstiff ODEs
- Anonymous Functions
- Troubleshoot Common ODE Problems
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office