## Solvers for Real-Time Simulation

To run your model on a real-time target machine, configure your model for fixed-step, fixed-cost simulation. The type of fixed-step solver, step size, and number of iterations that you specify affect the speed and accuracy of your real-time simulation.

Each distinct Simscape™ physical network in your model has
its own Simscape Solver Configuration block. You
can set the solver choice differently for each physical network. If
you do not check the local solver option for a physical network, then
that network uses the Simulink^{®} global solver that you specify.

When choosing a fixed-step solver type, the main factors to consider for each network in your model are:

Whether the network is discrete or continuous

The computational cost of the solver

The numerical stiffness of the network

The following table summarizes the types of fixed-step solvers in the Simulink and Simscape libraries.

Realm | Type | Numerical Method | Solver |
---|---|---|---|

Simulink global solver | Continuous | Explicit | ode1 (Euler's method) |

ode2 (Huen’s method) | |||

ode3 (Bogacki-Shampine) | |||

ode4 (Fourth-Order Runge-Kutta, RK4) | |||

ode5 (Dormand-Prince,RK5) | |||

ode8 (Dormand-Prince, RK8) | |||

Implicit | ode14x (extrapolation) | ||

ode1be (Backward Euler) | |||

Discrete | Not applicable | Discrete (no continuous states) | |

Simscape local network | Continuous | Implicit | Backward Euler |

Trapezoidal Rule | |||

Partitioning |

### Choosing Between Discrete and Continuous Solvers

To perform real-time simulation on a discrete model, for example, for the design of a digital controller, specify the Simulink global discrete solver. If the network that contains the controller has any continuous states, discretize the network. For an example that shows how to discretize the controller for the hydraulic actuator, see Hydraulic Actuator Configured for HIL Testing.

**Note**

A physical network using a local solver appears to the global Simulink solver as if it has discrete states.

If your controller model does contain continuous states, for example, if you are modeling an analog controller, use a Simulink global continuous solver.

### Computational Cost for Continuous Solvers

Computation cost is the number of calculations per time step that a processor performs. Real-time readiness varies inversely with computation cost. The lower the computational cost of a model is, the more likely it is that a real-time simulation of the model proceeds without overruns and generates sufficiently accurate results.

The figure shows the normalized computational cost of most global and local continuous fixed-step solvers. The data comes from a series of fixed-step, fixed-cost simulations using the different solver types. The model is nonlinear and contains one physical network. Although the solver type varies, the simulations use the same step size and a similar setting for the total number of solver iterations. They do so because the step size and number of iterations also affect the computational cost of a simulation.

For a given accuracy, explicit global solvers generally have a lower computational cost than implicit global solvers. Local (Simscape only) solvers are less costly than global solvers.

### How Numerical Stiffness Affects Solver Choice

To determine whether to use an explicit or implicit fixed-step solver for simulating your model in real time, consider these two factors:

The numerical stiffness of the system

The computational cost of the solver

To determine if your system is stiff or nonstiff, simulate with different fixed-step solver configurations and compare results from each to the reference results. If the step size is too large, stiff systems can produce oscillations because they contain dynamics that vary both quickly and slowly. For more information, see Stiffness of System and Determine System Stiffness.

Explicit solvers are faster than implicit solvers, but they provide less accurate solutions for numerically stiff systems because they tend to damp out oscillations. Implicit solvers can better capture the oscillations that occur in stiff systems because they are more robust than explicit solvers. However, implicit solvers deliver better accuracy at the expense of speed.

If your controller model is continuous and numerically stiff, use the implicit solver ode14x. If ode14x does not allow your model to simulate fast enough for real-time simulation, at the expense of accuracy, you can:

Improve simulation speed by increasing the step size or decreasing the number of iterations.

Use the ode1be Backward Euler solver.

Reduce the stiffness of your model and specify an explicit solver instead of ode14x.

To determine the explicit solver that is the best choice for your less stiff or numerically nonstiff, continuous controller model, perform bounded simulation using each of the explicit continuous solvers. Configure each solver to use the same step size and a similar number of solver iterations. Compare the simulation results and choose the solver that provides the best combination of accuracy and speed.

To increase the accuracy of the results that your explicit solver provides, at the expense of speed, decrease the step size or increase the number of iterations. For more information on configuring your model for fixed-step, fixed-cost simulation, and evaluating the results of bounded simulation, see Choose Step Size and Number of Iterations.

### Using Simscape Local Fixed-Step Solvers

You can usually further minimize computational cost by using a Simscape local solver for each independent physical network in your model. For similar levels of accuracy, local solvers have a lower computational cost than Simulink global solvers.

Simscape allows you to specify a different solver configuration for each independent physical system (subsystem) in your model. You can use an implicit fixed-step solver on the stiff local networks and an explicit fixed-step solver on the nonstiff local networks. Optimizing solvers for each network minimizes the overall number of computations done per time step and makes it more likely that the model can run in real time without generating an overrun.

Choose between three Simscape fixed-step solvers for real-time simulation.

Backward Euler

Trapezoidal Rule

Partitioning

The Backward Euler solver is more robust, and therefore more stable than the Trapezoidal Rule solver. It tends to damp oscillations. The Trapezoidal Rule solver is more accurate, but less stable than the Backward Euler solver. It tends to capture oscillations, like the sinusoid AC waveforms that are common to electrical systems. The Partitioning solver is also more robust than the Trapezoidal Rule solver, however, it cannot simulate certain models. For more information, see Increase Simulation Speed Using the Partitioning Solver. Regardless of the local solver you choose, the simulation uses the Backward Euler whenever numerical stability is at risk:

At the start of simulation.

After an instantaneous change, when the corresponding block undergoes an internal discrete change.