ODE Solver Selection in MATLAB

www.spiroprojects.com

There are 7 ordinary differential equation initial value problem solvers in MATLAB:
  • ode45
  • ode23
  • ode113
  • ode15s
  • ode23s
  • ode23t
  • ode23tb
(note that ode15i is left out of this discussion because it solves its own class of initial value problems: fully implicit ODEs of the form f(t,y,y)=0)
To choose between the solvers, it's first necessary to understand why one solver might be better than another for a given problem.
The ODE solvers in MATLAB all work on initial value problems of the form,

y=f(t,y)
where y=dy/dt. There is also a more general form,

M(t,y)y=f(t,y)
where M(t,y) is referred to as the mass matrix.
Starting with the initial conditions y0, and a period of time over which the answer is to be obtained (t0,tf), the solution is obtained iteratively by using the results of previous steps according to the solver's algorithm. At the first such step, the initial conditions provide the necessary information that allows the integration to proceed. The final result is that the ODE solver returns a vector of time steps t0,t1,...,tf as well as the corresponding solution at each time step y0,y1,...,yf.
Theoretically, this numerical solution technique is possible because of the connection between differential equations and integrals provided by the fundamental theorem of calculus:

y(t+h)=y(t)+t+htf(s,y(s))ds
The problem of calculating y(t+h) becomes a question of how to approximate the integral on the right hand side. This is where different solvers come in. Each different solver evaluates the integral using different numerical techniques, and each solver makes trade-offs between efficiency and accuracy.

Example: Euler's Method

Euler's method is a simple ODE solver, but it provides an illustration of the trade-offs between efficiency and accuracy in an ODE solver algorithm. Suppose you want to solve

y=f(t,y)=2t
over the time span [0,3] using the initial condition y0=0. Each step of Euler's method is computed with

yn+1tn+1=yn+hf(tn,yn)=tn+h
Using h=1, the solution requires just three steps:

y1y2y3=y0+f(t0,y0)=0=y1+f(t1,y1)=2=y2+f(t2,y2)=6
... But is it accurate?
Not really. The exact solution to this equation is

y(t)=t2
Reducing the step size h can improve the accuracy of the answer a bit, but it also requires more steps to achieve the solution. To see this, the below code solves this problem using Euler's method and compares the answer to the analytic solution for several different values of h.


 

Improving on Euler's Method

Using smaller and smaller step sizes turns out to not be a good idea, since the algorithm loses efficiency. For any reasonable problem such a solver would be very slow. Also, Euler's method has a few inherent problems. Since the slope of y is evaluated only once at the beginning of each interval, this solver only produces exact answers for constant functions. There is also no way to estimate the error, so the solver needs to use fixed step sizes.So, one way to improve on Euler's method is to evaluate y more often in each step. This provides intermediate slopes that give a better idea of what the function is doing within each interval, allowing the solver to produce exact answers for higher order problems. For example, if you add an evaluation of the slope halfway across each interval to Euler's method, then the result is called the midpoint rule, which produces exact integrations for linear functions:
s1s2yn+1tn+1=f(tn,yn)=f(tn+h2,yn+h2s1)=yn+hs2=tn+h
If you evaluate the slope four times in each interval, you get the classical Runge-Kutta algorithm (a.k.a. RK4), which is a piece of the ode45 algorithm. This algorithm produces exact integrations for cubic functions (and if f is only a function of t, then s2=s3 and this is the same as Simpson's rule for quadrature):
s1s2s3s4yn+1tn+1=f(tn,yn)=f(tn+h2,yn+h2s1)=f(tn+h2,yn+h2s2)=f(tn+h,yn+hs3)=yn+h6(s1+2s2+2s3+s4)=tn+h
Runge-Kutta algorithms are all single-step solvers, since each step only depends on the result of the previous step. ode45, ode23, ode23s, ode23t, and ode23tb all employ single-step algorithms. Multi-step algorithms, such as those employed by ode113 and ode15s, use the results of several past steps.
Sophisticated ODE solvers, like the ones in MATLAB, also estimate the error in each step to determine how big the next step size should be. This is another improvement over the fixed step sizes used above, since a solver that does more work per step is able to compensate by taking steps of varying size. The error estimate used to determine the step size is typically obtained by comparing the results of two different methods. MATLAB's ODE solvers follow a naming convention that reveals information about which methods they use. ode45 compares the results of a 4th-order Runge-Kutta method and a 5th-order Runge-Kutta method to determine the error. Similarly, ode23 uses a 2nd-order and 3rd-order Runge-Kutta comparison. So, in general, the smaller the number odeNN, the looser the solver's error tolerance is.
It should be no surprise, then, that ode45 obtains a very accurate answer for the equation we solved before with Euler's method. ode45 is MATLAB's general purpose ODE solver, and it is the first solver you should use for most problems.



y = @(t) t.^2; x = linspace(0,3); figure plot(x,y(x)) xlabel('t'), ylabel('y(t)') hold on [t,y] = ode45(@(t,y) 2*t, [0 3], 0); plot(t,y,'o') xlabel('t'), ylabel('y(t)') title('Solution of y''=2t using ode45')
 

Stiff Differential Equations

For some ODE problems, the step size taken by the solver is forced down to an unreasonably small level in comparison to the interval of integration, even in a region where the solution curve is smooth. These step sizes can be so small that traversing a short time interval might require millions of evaluations. This can lead to the solver failing the integration, but even if it succeeds it will take a very long time to do so.
Equations that cause this behavior in ODE solvers are said to be stiff. This is a nod to the fact that the equations are stubborn and not easily evaluated with numerical techniques. The problem that stiff ODEs pose is that explicit solvers (such as ode45) are untenably slow in achieving a solution. This is why ode45 is classified as a nonstiff solver along with ode23 and ode113. These solvers all struggle to integrate stiff equations.
Equation stiffness resists a precise definition, because there are several factors that cause it. Stiffness results from a combination of the specific equations, the ODE solver being used, the initial conditions, and the error tolerance used by the solver. The following statements about stiffness, attributed to Lambert [6], are exhibited by many examples of stiff ODEs, but counterexamples also exist, so they are not true definitions of stiffness:
  1. A linear constant coefficient system is stiff if all of its eigenvalues have negative real part and the stiffness ratio [of the largest and smallest eigenvalues] is large.
  2. Stiffness occurs when the mathematical problem is stable, and yet stability requirements, rather than those of accuracy, severely constrain the step length.
  3. Stiffness occurs when some components of the solution decay much more rapidly than others.
A common theme among these statements is that stiffness can result from a difference in scaling somewhere in the problem. This difference in scale (for example, if the Jacobian J=fn/yi has a large ratio of negative eigenvalues) constrains the step size that the solver can take in performing the integration. Tiny step sizes become necessary in order to preserve any notion of error tolerance or stability in the solution.
For example, equations describing chemical reactions frequently display stiffness, since it is common for components of the solution to vary on drastically different time scales (reactions occurring at the same time that are both very slow and very fast).
However, there are solvers specifically designed to work on stiff ODEs. Solvers that are designed for stiff problems typically do more work per step, and the pay-off is that they are able to take much larger steps and enjoy improved numerical stability compared to the nonstiff solvers. Stiff solvers are implicit, because the computation of y requires the use of linear algebra to solve systems of linear equations. The Jacobian is used to estimate the local behavior of the ODE as the integration proceeds, so supplying the analytical Jacobian can improve the performance of MATLAB's stiff ODE solvers.
This is just a cursory treatment of stiffness, because it is a complex topic. See Ordinary Differential Equations: Stiffness for a more in-depth look.
To summarize, the nonstiff solvers in MATLAB are:
  • ode45
  • ode23
  • ode113
The stiff solvers are (when ode45 is slow):
  • ode15s
  • ode23s
  • ode23t
  • ode23tb
It should be noted that nonstiff solvers do work on stiff problems, it is just that they are exceptionally slow. Similarly, solvers designed for stiff problems can work on nonstiff problems, but since they do more work per step they are less efficient than their nonstiff counterparts when that extra work isn't necessary. So equation stiffness is a matter of solver efficiency, and the goal is to strike the right balance between accuracy of the solution and work done in each step by the solver.

Solver Recommendations

The following recommendations are adapted from the MATLAB Mathematics documentation :
  • ode45 is MATLAB's general purpose single-step ODE solver. This should be the first solver you use for most problems.
For nonstiff problems:
  • ode23 is another single-step solver that can be more efficient than ode45 if the problem permits a crude error tolerance. This looser error tolerance can also accommodate some mildly stiff problems.
  • ode113 is a multi-step solver, and is preferred over ode45 if the function is expensive to evaluate, or for smooth problems where high precision is required. For example, ode113 excels with orbital dynamics and celestial mechanics problems.
For stiff problems (where ode45 is slow):
  • ode15s is a multi-step solver that is MATLAB's general purpose solver for stiff problems. Use ode15s if ode45 fails or struggles to complete the integration in a reasonable amount of time. ode15s is also the primary solver for DAEs, which are identified as ODEs with a singular mass matrix.
  • For stiff problems with crude error tolerances, ode23s, ode23t, and ode23tb provide more efficient alternatives to ode15s since they are single-step solvers. The efficiency of ode23s can be significantly improved by providing the Jacobian, since ode23s evaluates the Jacobian in each step.
  • ode23s only works on ODEs with a mass matrix if the mass matrix is constant (not time- or state-dependent).
  • ode15s and ode23t are the only solvers that solve DAEs of index 1.
Here is a graphic that captures the basic recommendations. In most cases, the only choice in solver you will need to make is to use ode15s instead of ode45.
 






 
 

Example 1: Damped Pendulum

The equation of motion for a damped pendulum is,

θ¨=bmθ˙mgL(m2b)sinθ
where g is the gravitational constant, m the mass of the bob, L the length of the string, and b is a damping coefficient. The goal is to solve for θ, the angle that the pendulum deviates from the vertical, and θ, the rate at which the angle changes.
Some natural initial conditions would be θ0=π/4 and θ0=0, indicating that you lift the pendulum up to a 45 degree angle before letting go, and it has no initial angular velocity. Due to the damping coefficient, you would expect the pendulum to slowly lose momentum and go back down to rest.
The file pendulumODE.m reformulates the problem as a coupled system of first-order ODEs:

y1y2=y2=bmy2mgL(m2b)sin(y1)
then solves using ode45, ode15s, ode23, and ode113. The solutions for y1=θ are plotted, and the file returns the stats for each solver. As is always the case when displaying execution times, "the timings displayed can vary".




 
 
 
The solvers all perform well, but the damped pendulum is a good example of a nonstiff problem where ode45 performs nicely. In this case ode15s needs to do extra work in order to achieve an inferior solution.

Example 2: van der Pol Oscillator

The van der Pol Oscillator equation becomes stiff in certain intervals when the nonlinear parameter μ is large:

y¨Î¼(1y2)y˙+y=0
The nonlinearity of this equation is contained entirely in the term that involves μ: notice that if μ=0, the equation reduces to that of a simple harmonic oscillator, which has regular periodic behavior.
Attempting to solve this equation using ode45 is met with severe resistance, requiring millions of evaluations and 30+ minutes of execution (I stopped execution after 35 minutes). Since the problem is clearly stiff, this example compares the stiff solvers.
The file vanderpolODE.m finds the solution for μ=1000 using ode15s, ode23s, ode23t, and ode23tb. The function file vdp1000.m ships with MATLAB and encodes this equation as a coupled system of first-order ODEs:

y1y2=y2=μ(1y21)y2y1
The Jacobian is supplied to assist the solvers, and its use is reflected in the number of partial derivative evaluations.




 
 
 
 
The plots are of the solutions for y1. For this problem, ode23s executes quickest and with the least number of failed steps. The supplied Jacobian greatly assists ode23s in evaluating the partial derivatives in each step. ode23tb also solves the problem with the fewest number of steps, outperforming ode15s. This problem is a good example of a stiff problem with a crude tolerance where ode23s and ode23tb can out perform ode15s.
 But practically speaking, all of the stiff solvers perform well on this
 problem and offer significant time savings when compared to ode45. 

 

 


Previous
Next Post »