Choosing A Solver
Choosing A Solver
On this page What Is a Solver? Choosing a Solver Type Choosing a Fixed-Step Solver Choosing a Variable-Step Solver Choosing a Jacobian Method for an Implicit Solver
What Is a Solver?
A solver is a component of the Simulink software. The Simulink product provides an extensive library of solvers, each of which determines the time of the next simulation step and applies a numerical method to solve the set of ordinary differential equations that represent the model. In the process of solving this initial value problem, the solver also satisfies the accuracy requirements that you specify. To help you choose the solver best suited for your application, Choosing a Solver Type provides background on the different types of solvers while Choosing a Fixed-Step Solver and Choosing a Variable-Step Solver provide guidance on choosing a specific fixed-step or variable-step solver, respectively. The following table summarizes the types of solvers in the Simulink library and provides links to specific categories. All of these solvers can work with the algebraic loop solver. Discrete Fixed-Step Explicit Not Applicable Continuous Explicit Fixed-Step Continuous Solvers Implicit Fixed-Step Continuous Solvers Explicit Continuous Variable-Step Solvers Implicit Continuous Variable-Step Solvers Variable-Order Not Applicable Not Applicable Variable-Order Solvers Variable-Order Solvers
Implicit Not Applicable Variable-Step Explicit Implicit Note 1. 2. Choosing a Variable-Step Solver
The fixed-step discrete solvers do not solve for discrete states; each block calculates its discrete states independent of the solver. For more information, see Discrete versus Continuous Solvers Every solver in the Simulink library can perform on models that contain algebraic loops.
For information on tailoring the selected solver to your model, see Improving Simulation Accuracy. Back to Top
The choice between the two types depends on how you plan to deploy your model and the model dynamics. If you plan to generate code from your model and run the code on a real-time computer system, choose a fixed-step solver to simulate the model because you cannot map the variable-step size to the real-time clock. If you do not plan to deploy your model as generated code, the choice between a variable-step and a fixedstep solver depends on the dynamics of your model. A variable-step solver might shorten the simulation time of your model significantly. A variable-step solver allows this savings because, for a given level of accuracy, the solver can dynamically adjust the step size as necessary and thus reduce the number of steps. Whereas the fixed-step solver must use a single step size throughout the simulation based upon the accuracy requirements. To satisfy these requirements throughout the simulation, the fixed-step solver might require a very small step. The following model shows how a variable-step solver can shorten simulation time for a multirate discrete model.
This model generates outputs at two different rates: every 0.5 s and every 0.75 s. To capture both outputs, the fixed-step solver must take a time step every 0.25 s (the fundamental sample time for the model).
The Simulink solver library provides both one-step and multistep solvers. The one-step solvers estimate y(tn) using the solution at the immediately preceding time point, y(tn-1), and the values of the derivative at a number of points between tn and tn-1. These points are minor steps. The multistep solvers use the results at several preceding time steps to compute the current solution. Simulink provides one explicit multistep solver, ode113, and one implicit multistep solver, ode15s. Both are variable-step solvers. Variable-Order Solvers Two variable-order solvers, ode15s and ode113, are part of the solver library. These solvers use multiple orders to solve the system of equations. Specifically, the implicit, variable-step ode15s solver uses firstorder through fifth-order equations while the explicit, variable-step ode113 solver uses first-order through thirteenth-order. For ode15s, you can limit the highest order applied via the Maximum Order parameter. For more information, see Maximum Order. Back to Top
where x is the state, Dx is a solver-dependent function that estimates the state derivative, h is the step size, and n indicates the current time step. Simulink provides a set of explicit fixed-step continuous solvers. The solvers differ in the specific numerical integration technique that they use to compute the state derivatives of the model. The following table lists each solver and the integration technique it uses. Solver Integration Technique Order of Accuracy First Second Third
Fourth-Order Runge-Kutta (RK4) Formula Fourth Dormand-Prince (RK5) Formula Dormand-Prince RK8(7) Formula Fifth Eighth
The table lists the solvers in order of the computational complexity of the integration methods they use, from the least complex (ode1) to the most complex (ode8). None of these solvers has an error control mechanism. Therefore, the accuracy and the duration of a simulation depends directly on the size of the steps taken by the solver. As you decrease the step size, the results become more accurate, but the simulation takes longer. Also, for any given step size, the more computationally complex the solver is, the more accurate are the simulation results. If you specify a fixed-step solver type for a model, then by default, Simulink selects the ode3 solver, which can handle both continuous and discrete states with moderate computational effort. As with the discrete solver, if the model has discrete rates (sample times), then Simulink sets the step size to the fundamental sample time of the model by default. If the model has no discrete rates, Simulink automatically uses the result of dividing the simulation total duration by 50. Consequently, the solver takes a step at each simulation time at which Simulink must update the discrete states of the model at its specified sample rates. However, it does not guarantee that the default solver accurately computes the continuous states of a model. Therefore, you might need to choose another solver, a different fixed step size, or both to achieve acceptable accuracy and an acceptable simulation time. Implicit Fixed-Step Continuous Solvers. An implicit fixed-step solver computes the state at the next time step as an implicit function of the state at the current time step and the state derivative at the next time step. In other words:
Simulink provides one implicit fixed-step solver : ode14x. This solver uses a combination of Newton's method and extrapolation from the current value to compute the value of a state at the next time step. You can specify the number of Newton's method iterations and the extrapolation order that the solver uses to compute the next value of a model state (see Fixed-step size (fundamental sample time)). The more iterations and the higher the extrapolation order that you select, the greater the accuracy you obtain. However, you simultaneously create a greater computational burden per step size. Process for Choosing a Fixed-Step Continuous Solver Any of the fixed-step continuous solvers in the Simulink product can simulate a model to any desired level of accuracy, given a small enough step size. Unfortunately, it generally is not possible, or at least not practical, to decide a priori which combination of solver and step size will yield acceptable results for the continuous states in the shortest time. Determining the best solver for a particular model generally requires experimentation.
Following is the most efficient way to choose the best fixed-step solver for your model experimentally. 1. 2. Choose error tolerances. For more information, see Specifying Error Tolerances for Variable-Step Solvers . Use one of the variable-step solvers to simulate your model to the level of accuracy that you desire. Start with ode45. If your model runs slowly, your problem might be stiff and need an implicit solver. The results of this step give a good approximation of the correct simulation results and the appropriate fixed step size. Use ode1 to simulate your model at the default step size for your model. Compare the simulation results for ode1 with the simulation for the variable-step solver. If the results are the same for the specified level of accuracy, you have found the best fixed-step solver for your model, namely ode1. You can draw this conclusion because ode1 is the simplest of the fixed-step solvers and hence yields the shortest simulation time for the current step size. If ode1 does not give satisfactory results, repeat the preceding steps with each of the other fixed-step solvers until you find the one that gives accurate results with the least computational effort. The most efficient way to perform this task is to use a binary search technique: a. b. c. d. Try ode3. If ode3 gives accurate results, try ode2. If ode2 gives accurate results, it is the best solver for your model; otherwise, ode3 is the best. If ode3 does not give accurate results, try ode5. If ode5 gives accurate results, try ode4. If ode4 gives accurate results, select it as the solver for your model; otherwise, select ode5. If ode5 does not give accurate results, reduce the simulation step size and repeat the preceding process. Continue in this way until you find a solver that solves your model accurately with the least computational effort.
3.
4.
Back to Top
ODE Solver
One-Step Method X X
Multistep Method
Method Runge-Kutta, Dormand-Prince (4,5) pair Runge-Kutta (2,3) pair of Bogacki & Shampine PECE Implementation of AdamsBashforth-Moutlon
In general, the ode45 solver is the best to apply as a first try for most problems. For this reason, ode45 is the default solver for models with continuous states. This Runge-Kutta (4,5) solver is a fifth-order method that performs a fourth-order estimate of the error. This solver also uses a fourth-order "free" interpolant, which allows for event location and smoother plots. The ode45 is more accurate and faster than ode23. If the ode45 is slow computationally, your problem may be stiff and thus in need of an implicit solver.
ode23
The ode23 can be more efficient than the ode45 solver at crude error tolerances and in the presence of mild stiffness. This solver provides accurate solutions for "free" by applying a cubic Hermite interpolation to the values and slopes computed at the ends of a step. For problems with stringent error tolerances or for computationally intensive problems, the Adams-Bashforth-Moulton PECE solver can be more efficient than ode45.
ode113
Implicit Continuous Variable-Step Solvers If your problem is stiff, try using one of the implicit variable-step solvers: ode15s, ode23s, ode23t, or ode23tb. ODE Solver One-Step Method Multistep Method X X X X Order of Accuracy Variable, Low to Medium Low Low Low X X Solver Reset Method X Max. Order X Method
Numerical Differentiation Formulas (NDFs) Second-order, modified Rosenbrock formula Trapezoidal rule using a "free" interpolant TR-BDF2
Solver Reset Method. For three of the stiff solvers ode15s, ode23t, and ode23tb a drop-down menu for the Solver reset method appears on the Solver Configuration pane. This parameter controls how the solver treats a reset caused, for example, by a zero-crossing detection. The options allowed are Fast and Robust. The former setting specifies that the solver does not recompute the Jacobian for a solver reset, whereas the latter setting specifies that the solver does. Consequently, the Fast setting is faster computationally but might use a small step size for certain cases. To test for such cases, run the simulation with each setting and compare the results. If there is no difference, you can safely use the Fast setting and save time. If the results differ significantly, try reducing the step size for the fast simulation. Maximum Order. For the ode15s solver, you can choose the maximum order of the numerical differentiation formulas (NDFs) that the solver applies. Since the ode15s uses first- through fifth-order
formulas, the Maximum order parameter allows you to choose 1 through 5. For a stiff problem, you may want to start with order 2. Tips for Choosing a Variable-Step Implicit Solver. The following table provides tips relating to the application of variable-step implicit solvers. For a demonstration comparing the behavior of these solvers, see sldemo_solvers. ODE Solver ode15s Tips on When to Use
ode15s is a variable-order solver based on the numerical differentiation formulas (NDFs). NDFs
are related to, but are more efficient than the backward differentiation formulas (BDFs), which are also known as Gear's method. The ode15s solver numerically generates the Jacobian matrices. If you suspect that a problem is stiff, or if ode45 failed or was highly inefficient, try ode15s. As a rule, start by limiting the maximum order of the NDFs to 2.
ode23s
ode23s is based on a modified Rosenbrock formula of order 2. Because it is a one-step solver, it can be more efficient than ode15s at crude tolerances. Like ode15s, ode23s numerically
generates the Jacobian matrix for you. However, it can solve certain kinds of stiff problems for which ode15s is not effective.
ode23t
The ode23t solver is an implementation of the trapezoidal rule using a "free" interpolant. Use this solver if your model is only moderately stiff and you need a solution without numerical damping. (Energy is not dissipated when you model oscillatory motion.)
ode23tb
ode23tb is an implementation of TR-BDF2, an implicit Runge-Kutta formula with two stages. The
first stage is a trapezoidal rule step while the second stage uses a backward differentiation formula of order 2. By construction, the method uses the same iteration matrix in evaluating both stages. Like ode23s, this solver can be more efficient than ode15s at crude tolerances.
Note For a stiff problem, solutions can change on a time scale that is very small as compared to the interval of integration, while the solution of interest changes on a much longer time scale. Methods that are not designed for stiff problems are ineffective on intervals where the solution changes slowly because these methods use time steps small enough to resolve the fastest possible change. For more information, see Shampine, L. F., Numerical Solution of Ordinary Differential Equations, Chapman & Hall, 1994. Support for Zero-Crossing Detection Both the variable-step discrete and continuous solvers use zero-crossing detection (see Zero-Crossing Detection) to handle continuous signals. Specifying Error Tolerances for Variable-Step Solvers Local Error. The variable-step solvers use standard control techniques to monitor the local error at each time step. During each time step, the solvers compute the state values at the end of the step and determine the local errorthe estimated error of these state values. They then compare the local error to the acceptable error, which is a function of both the relative tolerance (rtol) and the absolute tolerance (atol). If the local error is greater than the acceptable error for any one state, the solver reduces the step size and tries again. The Relative tolerance measures the error relative to the size of each state. The relative tolerance represents a percentage of the state value. The default, 1e-3, means that the computed state is accurate to within 0.1%. Absolute tolerance is a threshold error value. This tolerance represents the acceptable error as the value of the measured state approaches zero. The solvers require the error for the ith state, ei, to satisfy:
The following figure shows a plot of a state and the regions in which the relative tolerance and the absolute tolerance determine the acceptable error.
Absolute Tolerances. Your model has a global absolute tolerance that you can set on the Solver pane of the Configuration Parameters dialog box. This tolerance applies to all states in the model. You can specify auto or a real scalar. If you specify auto (the default), Simulink initially sets the absolute tolerance for each state to 1e-6. As the simulation progresses, the absolute tolerance for each state resets to the maximum value that the state has assumed so far, times the relative tolerance for that state. Thus, if a state changes from 0 to 1 and reltol is 1e-3, then by the end of the simulation, abstol becomes 1e-3 also. If a state goes from 0 to 1000, then abstol changes to 1. If the computed setting is not suitable, you can determine an appropriate setting yourself. You might have to run a simulation more than once to determine an appropriate value for the absolute tolerance. Several blocks allow you to specify absolute tolerance values for solving the model states that they compute or that determine their output: Integrator Second-Order Integrator, Second-Order Integrator Limited Variable Transport Delay Transfer Fcn State-Space Zero-Pole The absolute tolerance values that you specify for these blocks override the global settings in the Configuration Parameters dialog box. You might want to override the global setting if, for example, the global setting does not provide sufficient error control for all of your model states because they vary widely in magnitude. The block absolute tolerance can be set to:
auto
1 (same as auto)
real scalar real vector (having a dimension equal to the number of corresponding continuous states in the block)
Tips. If you do choose to set the absolute tolerance, keep in mind that too low of a value causes the solver to take too many steps in the vicinity of near-zero state values. As a result, the simulation is slower. On the other hand, if you choose too high of an absolute tolerance, your results can be inaccurate as one or more continuous states in your model approach zero. Once the simulation is complete, you can verify the accuracy of your results by reducing the absolute tolerance and running the simulation again. If the results of these two simulations are satisfactorily close, then you can feel confident about their accuracy. Back to Top
For implicit solvers, Simulink must compute the solver Jacobian, which is a submatrix of the Jacobian matrix associated with the continuous representation of a Simulink model. In general, this continuous representation is of the form:
Sparsity of Jacobian. For many physical systems, the solver Jacobian Jx is sparse, meaning that many of the elements of Jx are zero. Consider the following system of equations:
From this system, you can derive a sparsity pattern that reflects the structure of the equations. The pattern, a Boolean matrix, has a 1 for each thereby attain: that appears explicitly on the right-hand side of an equation. You
As discussed in Full and Sparse Perturbation Methods and Full and Sparse Analytical Methods respectively, the Sparse Perturbation Method and the Sparse Analytical Method may be able to take advantage of this sparsity pattern to reduce the number of computations necessary and thereby improve performance. Solver Jacobian Methods When you choose an implicit solver from the Solver pane of the Configuration Parameters dialog box, a parameter called Solver Jacobian method and a drop-down menu appear. This menu has five options for computing the solver Jacobian: auto, Sparse perturbation, Full perturbation, Sparse analytical, and Full analytical.
Note If you set Automatic solver parameter selection to warning or error in the Solver Diagnostics pane, and you choose a different solver method than Simulink, you might receive a warning or an error. Limitations. The solver Jacobian methods have the following limitations associated with them. If you select an analytical Jacobian method, but one or more blocks in the model do not have an analytical Jacobian, then Simulink applies a perturbation method. If you select sparse perturbation and your model contains data store blocks, Simulink applies the full perturbation method. Heuristic 'auto' Method
The default setting for the Solver Jacobian method is auto. Selecting this choice causes Simulink to perform a heuristic to determine which of the remaining four methods best suits your model. This algorithm is depicted in the following flowchart.
Because the sparse methods are beneficial for models having a large number of states, the heuristic chooses a sparse method if more than 50 states exist in your model. The logic also leads to a sparse method if you specify ode23s because, unlike other implicit solvers, ode23s generates a new Jacobian at every time step. A sparse analytical or a sparse perturbation method is, therefore, highly advantageous. The heuristic also ensures that the analytical methods are used only if every block in your model can generate an analytical Jacobian. Full and Sparse Perturbation Methods The full perturbation method was the standard numerical method that Simulink used to solve a system. For this method, Simulink solves the full set of perturbation equations and uses LAPACK for linear algebraic operations. This method is costly from a computational standpoint, but it remains the recommended method for establishing baseline results. The sparse perturbation method attempts to improve the run-time performance by taking mathematical advantage of the sparse Jacobian pattern. Returning to the sample system of three equations and three states,
It is, therefore, necessary to perturb each of the three states three times and to evaluate the derivative function three times. For a system with n states, this method perturbs the states n times. By applying the sparsity pattern and perturbing states x1 and x 2 together, this matrix reduces to:
The solver can now solve columns 1 and 2 in one sweep. While the sparse perturbation method saves significant computation, it also adds overhead to compilation. It might even slow down the simulation if the system does not have a large number of continuous states. A tipping point exists for which you obtain increased performance by applying this method. In general, systems having a large number of continuous states are usually sparse and benefit from the sparse method. The sparse perturbation method, like the sparse analytical method, uses UMFPACK to perform linear algebraic operations. Also, the sparse perturbation method supports both RSim and Rapid Accelerator mode. Full and Sparse Analytical Methods The full and sparse analytical methods attempt to improve performance by calculating the Jacobian using analytical equations rather than the perturbation equations. The sparse analytical method, also uses the sparsity information to accelerate the linear algebraic operations required to solve the ordinary differential equations. Sparsity Pattern For details on how to access and interpret the sparsity pattern in MATLAB, see sldemo_metro. Support for Code Generation While the sparse perturbation method supports RSim, the sparse analytical method does not. Consequently, regardless of which sparse method you select, any generated code uses the sparse perturbation method.