Documentation

## Fixed Step Solvers in Simulink

Fixed-step solvers solve the model at regular time intervals from the beginning to the end of the simulation. The size of the interval is known as the step size. You can specify the step size or let the solver choose the step size. Generally, a smaller the step size increases the accuracy of the results but also increases the time required to simulate the system.

### Fixed-Step Discrete Solver

The fixed-step discrete solver computes the time of the next simulation step by adding a fixed step size to the current time. The accuracy and the length of time of the resulting simulation depends on the size of the steps taken by the simulation: the smaller the step size, the more accurate the results are but the longer the simulation takes. By default, Simulink® chooses the step size 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 the step size to the fundamental sample time of the model. Otherwise, if no discrete rates exist, Simulink sets the size to the result of dividing the difference between the simulation start and stop times by 50.

### Fixed-Step Continuous Solvers

The fixed-step continuous solvers, like the fixed-step discrete solver, compute the next simulation time by adding a fixed-size time step to the current time. For each of these steps, the continuous solvers use numerical integration to compute the values of the continuous states for the model. These values are calculated using the continuous states at the previous time step and the state derivatives at intermediate points (minor steps) between the current and the previous time step.

### Note

Simulink uses the fixed-step discrete solver for a model that contains no states or only discrete states, even if you specify a fixed-step continuous solver for the model.

Simulink provides two types of fixed-step continuous solvers — explicit and implicit.

The difference between these two types lies in the speed and the stability. An implicit solver requires more computation per step than an explicit solver but is more stable. Therefore, the implicit fixed-step solver that Simulink provides is more adept at solving a stiff system than the fixed-step explicit solvers. For a comparison of explicit and implicit solvers, see Explicit Versus Implicit Continuous Solvers.

#### Fixed-Step Continuous Explicit Solvers

Explicit solvers compute the value of a state at the next time step as an explicit function of the current values of both the state and the state derivative. A fixed-step explicit solver is expressed mathematically as:

`$x\left(n+1\right)=x\left(n\right)+h\ast Dx\left(n\right)$`

where

• x is the state.

• Dx is a solver-dependent function that estimates the state derivative.

• h is the step size.

• n indicates the current time step.

Simulink provides a set of fixed-step continuous explicit solvers. The solvers differ in the specific numerical integration technique that they use to compute the state derivatives of the model. This table lists each solver and the integration technique it uses. The table lists the solvers in the order of the computational complexity of the integration methods they use, from the least complex (`ode1`) to the most complex (`ode8`).

SolverIntegration TechniqueOrder of Accuracy

`ode1`

Euler's method

First

`ode2`

Heun's method

Second

`ode3`

Bogacki-Shampine formula

Third

`ode4`

Fourth-order Runge-Kutta (RK4) formula

Fourth

`ode5`

Dormand-Prince (RK5) formula

Fifth

`ode8`

Dormand-Prince RK8(7) formula

Eighth

None of these solvers have an error control mechanism. Therefore, the accuracy and the duration of a simulation depend 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 higher the order of the solver, the more accurate the simulation results.

If you specify a fixed-step solver type for a model, then by default, Simulink selects the `FixedStepAuto` solver. Auto solver then selects an appropriate fixed-step solver that 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 may need to choose another solver, a different fixed step size, or both to achieve acceptable accuracy and an acceptable simulation time.

#### Fixed-Step Continuous Implicit Solvers

An implicit 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, as described by the following expression.

`$x\left(n+1\right)-x\left(n\right)-h\ast Dx\left(n+1\right)=0$`

Simulink provides one fixed-step implicit 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.

### Choose a Fixed-Step Solver

This example shows an algorithmic method of selecting an appropriate fixed-step solver for your model. For simulation workflows in Simulink™, the default setting for the Solver parameter in the Model Configuration Parameters is `auto`. The heuristics used by Simulink to select a variable-step solver is shown in the figure below.

When to Use a Fixed-Step Solver

One common case to use a fixed-step solver is for workflows where you plan to generate code from your model and run the code on a real-time system.

With a variable-step solver, the step size can vary from step to step, depending on the model dynamics. In particular, a variable-step solver increases or reduces the step size to meet the error tolerances that you specify and as such, the variable step sizes cannot be mapped to the real-time clock of a target system.

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 is not possible or practical to decide without trial, the combination of solver and step size that will yield acceptable results for the continuous states in the shortest time. Determining the best solver for a particular model generally requires experimentation.

The example model represents the flight control algorithm for the longitudinal flight of an aircraft.

```Warning: Method 'setUserActionInProgress' is not defined for class 'slreq.app.MainManager' or is removed from MATLAB's search path. ```

```Warning: Method 'setUserActionInProgress' is not defined for class 'slreq.app.MainManager' or is removed from MATLAB's search path. ```

Establish Baseline Results Using a Variable-Step Solver

Before you begin simulation, determine acceptable error tolerances for your variable-step solver. The model is currently set up with the default values of absolute and relative tolerances of `1e-6` and `1e-4` respectively.

If these values are acceptable, continue with the example. Otherwise, you can change them to your specification using the Model Configuration Parameters.

Select a variable-step solver from the list of solvers in the Solver dropdown in the Solver pane of the Model Configuration Parameters to simulate the model. The default setting in Simulink for the Solver parameter is `VariableStepAuto`. Simulink selects a solver and the maximum step size of the simulation based on the contents and dynamics of the model.

Simulate the model using the `auto` solver, or pick another solver. Additionally, enable the Save states, Save time, and Save outputs parameters in the Data Import/Export pane of the Model Configuration Parameters. Set the logging format for your model to `Dataset` to allow the Simulation Data Inspector to log the signals.

The simulation results from this run will be designated the baseline results for this task. The model contains 13 signals, but this example focuses on only a few signals plotted below.

Profile the model using the Solver Profiler to find an appropriate step size for the candidate fixed-step simulations of the model. See Solver Profiler for information on how to launch and use the tool. For command-line usage, see `solverprofiler.profileModel`.

Note the maximum and average step sizes returned by the Solver Profiler.

```Warning: Solver Profiler temporarily changed model parameter 'ReturnWorkspaceOutputs' value from 'off' to 'on' for profiling, the parameter value will be automatically restored after profiling is complete. Warning: Solver Profiler temporarily changed model parameter 'SaveState' value from 'on' to 'off' for profiling, the parameter value will be automatically restored after profiling is complete. solver: 'ode45' tStart: 0 tStop: 60 absTol: 1.0000e-06 relTol: 1.0000e-04 hMax: 0.1000 hAverage: 0.0447 steps: 1342 profileTime: 0.0305 zcNumber: 0 resetNumber: 600 jacobianNumber: 0 exceptionNumber: 193 ```

Run Fixed-Step Simulations of the Model

Once you obtain the results of the variable-step simulation of the model, simulate it using one or more of the fixed-step solvers. In this example, the model is simulated using all the non-stiff fixed-step solvers: `ode1`, `ode2`, `ode3`, `ode4`, `ode5`, and `ode8.` You can also select a specific solver from the Solver dropdown in the Model Configuration Parameters to run against the variable-step baseline.

Considerations for Selecting a Fixed Step Size

The optimal step size for a fixed-step simulation of your model strikes a balance between speed and accuracy, given constraints such as code-generation objectives, physics or dynamics of the model, and modeling patterns used. For example, code generation would dictate the step size must be greater than or equal to the clock speed of the processor (the reciprocal of the CPU frequency). For pure simulation objectives, the step size must be less than the discrete sample times specified by individual blocks in the model. For models with periodic signals, the step size must be such that the signal is sampled at twice its highest frequency; this is known as the Nyquist frequency.

For this specific example, set the fixed-step size of the solver to 0.1 (the maximum step size detected by the Solver Profiler). This takes into account the discrete sample time 0.1 of the Dryden Wind-Gust block, as well as the periodic nature of the stick movements and the aircraft response.

Make sure that the model states, outputs, and simulation time are enabled for logging and that the logging format is set to `Dataset` in the Model Configuration Parameters.

Simulate the model by selecting any one or all the non-stiff fixed-step solvers from the Solver dropdown of the Model Configuration Parameters when the solver Type is set to `Fixed-step`.

A `Simulink.sdi.Run` object is created for the fixed-step solver simulation(s) and stored in the `fsRuns` struct in the base workspace.

Compare Fixed-Step Simulations with the Variable-Step Baseline

Use the Simulation Data Inspector to visualize and inspect logged signals in your model. You can also compare signals across simulations, or runs, using the Compare feature. For more information on using the Simulation Data Inspector, see Simulation Data Inspector. For more information on how to compare simulations using the Simulation Data Inspector, see Compare Simulation Data.

To compare signals, switch to the Compare tab in the Simulation Data Inspector. Set the Baseline run to the variable-step simulation and select a fixed-step simulation from the Compare to dropdown. Set the Global Abs Tolerance, Global Rel Tolerance, and Global Time Tolerance based on your requirements.

For this example, Global Abs Tolerance is set to `0.065`, Global Rel Tolerance is set to 0.005, and Global Time Tolerancce is set to `0.1`.

The comparison plots display the results for the lowest order fixed-step solver simulation where all signals fell within tolerance, when compared to the baseline variable-step simulation. For the selected solver, comparison results of a few of the signals are plotted below.

The lowest order with all signals within tolerance is determined to be `ode4`. Consider the results for the ode1 fixed-step solver, where the comparison results showed 11 signals out of tolerance. Observe that there are 11 signals out of tolerance when the signal comparison parameters are set as: * Signal Abs Tolerance: 0.065 * Signal Rel Tolerance: 0.065 * Signal Time Tolerance: 0.1