Solve Nonstiff ODEs
This page contains two examples of solving nonstiff ordinary differential equations usingode45
。MATLAB® has several solvers for nonstiff ODEs.
ode45
ode23
ode78
ode89
ode113
For most nonstiff problems,ode45
performs best. However,ode23
is recommended for problems that permit a slightly cruder error tolerance or in the presence of moderate stiffness. Likewise,ode113
can be more efficient thanode45
for problems with more stringent error tolerances or when the ODE function is computationally expensive to evaluate.ode78
andode89
are high-order solvers that excel with long integrations where accuracy is crucial for stability.
If the nonstiff solvers take a long time to solve the problem or consistently fail the integration, then the problem might bestiff。SeeSolve Stiff ODEsfor more information.
Example: Nonstiff van der Pol Equation
The van der Pol equation is a second order ODE
whereis 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 system of ODEs must be coded into a function file that the ODE solver can use. The general functional signature of an ODE function is
dydt = odefun(t,y)
That is, the function must accept botht
andy
as inputs, even if it does not uset
for any computations.
函数文件vdp1.m
codes the van der Pol equation using。的变量andare represented byy(1)
andy(2)
, and the two-element column vectordydt
contains the expressions forand。
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 theode45
function on the time interval[0 20]
with initial values[2 0]
。The output is a column vector of time pointst
and a solution arrayy
。Each row iny
corresponds to a time returned in the corresponding row oft
。The first column ofy
corresponds to, and the second column to。
[t,y] = ode45(@vdp1,[0 20],[2; 0]);
Plot the solutions forandagainstt
。
plot(t,y(:,1),'-o',t,y(:,2),'-o') title('Solution of van der Pol Equation (\mu = 1) using ODE45'); xlabel('Time t'); ylabel('Solution y'); legend('y_1','y_2')
Thevdpode
function solves the same problem, but it accepts a user-specified value for。The van der Pol equations become stiff asincreases. For example, with the valueyou need to use a stiff solver such asode15s
to solve the system.
Example: Nonstiff Euler Equations
The Euler equations for a rigid body without external forces are a standard test problem for ODE solvers intended for nonstiff problems.
The equations are
函数文件rigidode
defines and solves this first-order system of equations over the time interval[0 12]
, using the vector of initial conditions[0; 1; 1]
corresponding to the initial values of,, and。当地的函数f(t,y)
encodes the system of equations.
rigidode
callsode45
with no output arguments, so the solver uses the default output functionodeplot
to automatically plot the solution points after each step.
functionrigidode%RIGIDODE Euler equations of a rigid body without external forces.% A standard test problem for non-stiff solvers proposed by Krogh. The% analytical solutions are Jacobian elliptic functions, accessible in% MATLAB. The interval here is about 1.5 periods; it is that for which% solutions are plotted on p. 243 of Shampine and Gordon.%% L. F. Shampine and M. K. Gordon, Computer Solution of Ordinary% Differential Equations, W.H. Freeman & Co., 1975.%% See also ODE45, ODE23, ODE113, FUNCTION_HANDLE.% Mark W. Reichelt and Lawrence F. Shampine, 3-23-94, 4-19-94% Copyright 1984-2014 The MathWorks, Inc.tspan = [0 12]; y0 = [0; 1; 1];% solve the problem using ODE45figure; ode45(@f,tspan,y0);% --------------------------------------------------------------------------functiondydt = f(t,y) dydt = [ y(2)*y(3) -y(1)*y(3) -0.51*y(1)*y(2) ];
Solve the nonstiff Euler equations by calling therigidode
function.
rigidode title('Solution of Rigid Body w/o External Forces using ODE45') legend('y_1','y_2','y_3','Location','Best')
See Also
ode45
|ode23
|ode78
|ode89
|ode113