On this page… |
---|
What Is a Solver? Choosing a Solver Type Choosing a Fixed-Step Solver Choosing a Variable-Step Solver |
A solver is a component of the Simulink software that determinesthe next simulation time step. In making this determination, the solversatisfies the target accuracy requirements that you specify. The Simulink productprovides an extensive set of solvers, each adept at choosing the nexttime step for specific types of applications. The following sectionsexplain how to choose the solver best suited to your application.For information on tailoring the selected solver to your model, see Improving Simulation Accuracy.
Back to Top
Solvers are divided into two types: fixed-step and variable-step.Both types of solvers compute the next simulation time as the sumof the current simulation time and a quantity known as the step size.With a fixed-step solver, the step size remains constant throughoutthe simulation. In contrast, with a variable-step solver, the stepsize can vary from step to step, depending on the model dynamics.In particular, a variable-step solver reduces the step size when themodel states are changing rapidly to maintain accuracy. Similarly,the variable-step solver increases the step size when the model statesare changing slowly and thus avoids unnecessary steps. The Type control on the Simulink Solver configuration pane allows you to selecteither of these two types of solvers (see Solver Pane).
The choice between the two types depends on how you plan todeploy your model and the model dynamics. If you plan to generatecode from your model and run the code on a real-time computer system,choose a fixed-step solver to simulate the model. The rational forthis decision is that real-time computer systems operate at fixed-sizesignal sample rates. A variable-step solver may cause the simulationto miss error conditions that can occur on a real-time computer system.
If you do not plan to deploy your model as generated code, thechoice between a variable-step and a fixed-step solver depends onthe dynamics of your model. A variable-step solver can shorten thetime required to simulate your model significantly. A variable-stepsolver allows such savings because, for a given level of accuracy,the variable-step solver can dynamically adjust the step size as necessaryand thus reduce the number of steps required. Whereas the fixed-stepsolver must use a single step size throughout the simulation basedupon the accuracy requirements. In order to satisfy such requirementsthroughout the simulation, the solver might require a very small step.
The following model illustrates how a variable-step solver canshorten simulation time for a multirate discrete model.
This model generates outputs at two different rates, every0.5 s and every 0.75 s. To capture both outputs, the fixed-step solvermust take a time step every 0.25 s (the fundamental sampletime for the model).
[0.0 0.25 0.5 0.75 1.0 1.25 ...]
By contrast, the variable-step solver needs to take a step onlywhen the model actually generates an output.
[0.0 0.5 0.75 1.0 1.5 2.0 2.25 ...]
This significantly reduces the number of time steps requiredto simulate the model.
The variable-step discrete solver uses zero-crossing detection(see Zero-Crossing Detection) to handle continuous signals.Simulink uses this solver by default if you specify a continuous solverand if your model has no continuous states.
Back to Top
When you set the Type controlof the Solver configuration paneto fixed-step, the adjacent Solver controlallows you to choose one of the fixed-step solvers provided. The setof fixed-step solvers comprises two types of solvers: discrete andcontinuous. Both of these types rely upon the model blocks to computethe values of any discrete states. Blocks that define discrete statescompute the values of those states at each time step. Unlike the discretesolvers, the continuous solvers compute the continuous states definedby blocks through numerical integration. Therefore, the very firststep is to decide whether you need to use a discrete or a continuoussolver.
If your model has no states or discrete states only, choosethe fixed-step discrete solver. If your model has continuous states,you must choose either one of the explicit fixed-step continuous solvers(ode1, ode2, ode3, ode4, ode5, ode8), Explicit Fixed-Step Continuous Solversor the implicit fixed-stepcontinuous solver (ode14x), Implicit Fixed-Step Continuous Solvers. Compared to the explicitfixed-step solvers, the ode14x implicit solver provides for high accuracyand stability but is computationally expensive.
The fixed-step discrete solver computes the time of the nextsimulation step by adding a fixed step size to the current time. Theaccuracy and the length of time of the resulting simulation dependson the size of the steps taken by the simulation: the smaller thestep size, the more accurate the results are but the longer the simulationtakes. You can allow the Simulink software to choose the sizeof the step size (the default) or you can choose the step size yourself.If you choose the default setting of auto,and if the model has discrete sample times, then Simulink sets thestep size to the fundamental sample time of the model. Otherwise,if no discrete rates exist, Simulink sets the size to the result ofdividing the difference between the simulation start and stop timesby 50.
Note If you attempt to use the fixed-step discrete solver to updateor simulate a model that has continuous states, an error message appears.Thus, updating or simulating a model is a quick way to determine whetherit has continuous states. |
The fixed-step continuous solvers, like the fixed-step discretesolver, compute the next simulation time by adding a fixed-size timestep to the current time. For each of these steps, the continuoussolvers employ numerical integration to compute the values of thecontinuous states for the model. These values are calculated usingthe continuous states at the previous time step and the state derivativesat intermediate points (minor steps) between the current and the previoustime step. The fixed-step continuous solvers can therefore handlemodels that contain both continuous and discrete states.
Note In theory, a fixed-step continuous solver can handle modelsthat contain no continuous states. However, that would impose an unnecessarycomputational burden on the simulation. Consequently, Simulink usesthe fixed-step discrete solver for a model that contains no statesor only discrete states, even if you specify a fixed-step continuoussolver for the model. |
The two distinct types of fixed-step continuous solvers providedare: explicit and implicit solvers. Explicit solvers (see Explicit Fixed-Step Continuous Solvers) compute the valueof a state at the next time step as an explicit function of the currentvalues of both the state and the state derivative. Expressed mathematically:
X(n+1) = X(n) + h * DX(n)
where X is the state, DX isthe state derivative, and h is the step size, and n indicatesthe current time step. An implicit solver (see Implicit Fixed-Step Continuous Solvers)computes the state at the next time step as an implicit function ofthe state at the current time step and the state derivative at thenext time step. In other words,
X(n+1) - X(n) - h*DX(n+1) = 0
This type of solver requires more computation per step thanan explicit solver but is also more accurate for a given step size.This solver thus can be faster than explicit fixed-step solvers forcertain types of stiff systems.
Explicit Fixed-Step Continuous Solvers. Simulinkprovides a set of explicit fixed-step continuous solvers. The solversdiffer in the specific numerical integration technique that they useto compute the state derivatives of the model. The following tablelists each of the available solvers and the integration techniqueit uses.
Solver | Integration Technique |
---|---|
ode1 |
Euler's Method |
ode2 |
Heun's Method |
ode3 |
Bogacki-Shampine Formula |
ode4 |
Fourth-Order Runge-Kutta (RK4) Formula |
ode5 |
Dormand-Prince (RK5) Formula |
ode8 |
Dormand-Prince RK8(7) Formula |
The integration techniques used by the fixed-step continuoussolvers trade accuracy for computational effort. The table lists thesolvers in order of the computational complexity of the integrationmethods they use, from the least complex (ode1)to the most complex (ode8).
As with the fixed-step discrete solver, the accuracy and theduration of a simulation driven by a fixed-step continuous solverdepends on the size of the steps taken by the solver: as you decreasethe step size, the results become more accurate but the simulationtakes longer. Also, for any given step size, the more computationallycomplex the solver is, the more accurate are the simulation results.
If you specify a fixed-step solver type for a model, then bydefault, Simulink selects the ode3 solver whichis capable of handling both continuous and discrete states with moderatecomputational effort. As with the discrete solver, if the model hasdiscrete rates (sample times), then Simulink sets the step size tothe fundamental sample time of the model by default. If the modelhas no discrete rates, Simulink automatically uses the result of dividingthe simulation total duration by 50. Consequently, the solver takesa step at each simulation time at which Simulink must update the discretestates of the model at its specified sample rates. However, it doesnot guarantee that the default solver accurately computes the continuousstates of a model or that the model cannot be simulated in less timewith a less complex solver. Depending on the dynamics of your model,you might need to choose another solver and/or a different sampletime to achieve both acceptable accuracy and an acceptable simulationtime.
Implicit Fixed-Step Continuous Solvers. This categoryprovides one solver : ode14x. This solver usesa combination of Newton's method and extrapolation from the currentvalue to compute the value of a model state at the next time step.You can specify the number of Newton's method iterations and the extrapolationorder that the solver uses to compute the next value of a model state(see Fixed-step size (fundamental sample time)). The moreiterations and the higher the extrapolation order that you select,the greater the accuracy you obtain. However, you simultaneously createa greater computational burden per step size.
Any of the fixed-step continuous solvers in the Simulink productcan simulate a model to any desired level of accuracy, given a smallenough step size. Unfortunately, it generally is not possible, orat least not practical, to decide a priori whichcombination of solver and step size will yield acceptable resultsfor the continuous states in the shortest time. Determining the bestsolver for a particular model thus generally requires experimentation.
Here is the most efficient way to choose the best fixed-stepsolver for your model experimentally. First, use one of the variable-stepsolvers to simulate your model to the level of accuracy that you desire.These results will give you a good approximation of the correct simulationresults. Next, use ode1 to simulate your modelat the default step size for your model. Compare the results of simulatingyour model with ode1 with the results of simulatingwith the variable-step solver. If the results are the same for thespecified level of accuracy, you have found the best fixed-step solverfor your model, namely ode1. You can draw thisconclusion because the ode1 is the simplest ofthe fixed-step solvers and hence yields the shortest simulation timefor the current step size.
If ode1 does not give accurate results, repeatthe preceding steps with each of the other fixed-step solvers untilyou find the one that gives accurate results with the least computationaleffort. The most efficient way to perform this task is to use a binarysearch technique. First, try ode3. If it givesaccurate results, try ode2. If ode2 givesaccurate results, it is the best solver for your model; otherwise, ode3 isthe best. If ode3 does not give accurate results,try ode5. If ode5 gives accurateresults, try ode4. If ode4 givesaccurate results, select it as the solver for your model; otherwise,select ode5.
If ode5 does not give accurate results, reducethe simulation step size and repeat the preceding process. Continuein this way until you find a solver that solves your model accuratelywith the least computational effort.
Back to Top
When you set the Type controlof the Solver configuration paneto variable-step, the Solver controlallows you to choose one of the variable-step solvers. As with fixed-stepsolvers, the set of variable-step solvers comprises a discrete solverand a subset of continuous solvers. However, unlike the fixed-stepsolvers, the step size varies dynamically with the rate of changeof the model states. The choice between the two types of solvers dependson whether the blocks in your model define states and, if so, thetype of states that they define. If your model defines no states ordefines only discrete states, select the discrete solver. In fact,if a model has no states or only discrete states, Simulink uses thediscrete solver to simulate the model even if you specify a continuoussolver. If the model has continuous states, the continuous solversuse numerical integration to compute the values of the continuousstates at the next time step.
The variable-step solvers in the Simulink product dynamicallyvary the step size during the simulation. They reduce the step sizeto increase the accuracy when the model states are changing rapidlyand increase the step size to avoid unnecessary steps when the modelstates are changing slowly. Computing the step size at each time stepadds to the computational overhead but can reduce the total numberof steps, and hence the simulation time required to maintain a specifiedlevel of accuracy. This capability is particularly important for modelswith rapidly changing or piecewise continuous states.
Following are the variable-step continuous solvers provided.They comprise both one-step and multistep solvers.
ode45 is based on an explicit Runge-Kutta (4,5) formula, the Dormand-Prince pair. Itis a one-step solver; that is, in computing y(tn), it needs only the solutionat the immediately preceding time point, y(tn-1). In general, ode45 isthe best solver to apply as a first try for most problems. For thisreason, ode45 is the default solver used for modelswith continuous states.
ode23 is also based on an explicit Runge-Kutta(2,3) pair of Bogacki and Shampine. It can be more efficient than ode45 atcrude tolerances and in the presence of mild stiffness. ode23 isa one-step solver.
ode113 is a variable-order Adams-Bashforth-Moulton PECE solver.It can be more efficient than ode45 at stringenttolerances. ode113 is a multistep solver;that is, it normally needs the solutions at several preceding timesteps to compute the current solution.
ode15s is a variable-order solver based on the numericaldifferentiation formulas (NDFs). NDFs are related to, but are moreefficient than the backward differentiation formulas (BDFs), alsoknown as Gear's method. Theode15s solver is a multistepsolver that numerically generates the Jacobian matrices. If you suspectthat a problem is stiff, or if ode45 failed orwas highly inefficient, try ode15s.
ode23s is based on a modified Rosenbrock formula of ordertwo. Because it is a one-step solver, it can be more efficient than ode15s atcrude tolerances. Like ode15s, ode23s numericallygenerates the Jacobian for you. However, it can solve certain kindsof stiff problems for which ode15s is not effective.
ode23t is an implementation ofthe trapezoidal rule using a "free" interpolant. Usethis solver if the problem is only moderately stiff and you need asolution without numerical damping.
ode23tb is an implementation ofTR-BDF2, an implicit Runge-Kutta formula with a first stage that isa trapezoidal rule step and a second stage that is a backward differentiationformula of order two. By construction, the method uses the same iterationmatrix in evaluating both stages. Like ode23s,this solver can be more efficient than ode15s atcrude tolerances.
Note Fora stiff problem, solutions can change on a timescale that is very small as compared to the interval of integration,while the solution of interest changes on a much longer time scale.Methods not designed for stiff problems are ineffective on intervalswhere the solution changes slowly because they use time steps smallenough to resolve the fastest possible change. For more information, see Shampine, L. F., NumericalSolution of Ordinary Differential Equations, Chapman &Hall, 1994. |
The solvers use standard local error control techniques to monitorthe error at each time step. During each time step, the solvers computethe state values at the end of the step and determine the localerror—the 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 localerror is greater than the acceptable error for any state,the solver reduces the step size and tries again.
The relative tolerance measuresthe error relative to the size of each state. The relative tolerancerepresents a percentage of the state value. The default, 1e-3, meansthat the computed state is accurate to within 0.1%.
Absolute tolerance isa threshold error value. This tolerance represents the acceptableerror as the value of the measured state approaches zero.
The solvers require the error for the ithstate, ei,to satisfy:
The following figure shows a plot of a stateand the regions in which the relative tolerance and the absolute tolerancedetermine the acceptable error.
If you specify auto (the default), Simulinkinitially sets the absolute tolerance for each state to 1e-6. As thesimulation progresses, the absolute tolerance for each state is resetto the maximum value that the state has assumed, thus far, times therelative tolerance for that state. Thus, if a state changes from 0to 1 and the reltol is 1e-3, then by the end ofthe simulation the abstol is set to 1e-3 also.If a state goes from 0 to 1000, then the abstol isset to 1.
If the computed setting is not suitable, you can determine anappropriate setting yourself. You might have to run a simulation morethan once to determine an appropriate value for the absolute tolerance.
The Integrator, Transfer Fcn, State-Space,and Zero-Pole blocks allow youto specify absolute tolerance values for solving the model statesthat they compute or that determine their output. The absolute tolerancevalues that you specify for these blocks override the global settingsin the Configuration Parameters dialog box. You might want to overridethe global setting in this way. For example, if the global settingdoes not provide sufficient error control for all of your model statesbecause they vary widely in magnitude, then you might want to setthe value yourself.