## Important Concepts and Choices in Physical Simulation

This section describes advanced concepts and trade-offs you might want to consider as you configure and test solvers and other simulation settings for your Simscape™ model. For a summary of recommended settings, see Making Optimal Solver Choices for Physical Simulation. For background information, consult How Simscape Models Represent Physical Systems and How Simscape Simulation Works.

### Variable-Step and Fixed-Step Solvers

*Variable-step solvers* are the usual choice for design, prototyping,
and exploratory simulation, and to precisely locate events during simulation. They are not
useful for real-time simulation and can be costly if there are many events.

A variable-step solver automatically adjusts its step size as it moves forward in time to adapt to how well it controls solution error. You control the accuracy and speed of the variable-step solution by adjusting the solver tolerance. With many variable-step solvers, you can also limit the minimum and maximum time step size.

*Fixed-step solvers* are recommended or required if you want to make
performance comparisons across platforms and operating systems, to generate a code version
of your model, and to bound or fix simulation cost. A typical application is real-time
simulation. For more information, see Real-Time Simulation.

With a fixed-step solver, you specify the time step size to control the accuracy and speed of your simulation. Fixed-step solvers do not adapt to improve accuracy or to locate events. These limitations can lead to significant simulation inaccuracies.

### Explicit and Implicit Solvers

The degree of stiffness and the presence of algebraic constraints in your model
influence the choice between an *explicit* or
*implicit* solver. Explicit and implicit solvers use different
numerical methods to simulate a system.

If the system is a nonstiff ODE system, choose an explicit solver. Explicit solvers require less computational effort than implicit solvers, if other simulation characteristics are fixed.

To find a solution for each time step, an explicit solver uses a formula based on the local gradient of the ODE system.

If the system is stiff, use an implicit solver. Though an explicit solver may require less computational effort, for stiff problems an implicit solver is more accurate and often essential to obtain a solution. Implicit solvers require per-step iterations within the simulated time steps. With some implicit solvers, you can limit or fix these iterations.

An implicit solver starts with the solution at the current step and iteratively solves for the solution at the next time step with an algebraic solver. An implicit algorithm does more work per simulation step, but can take fewer, larger steps.

If the system contains DAEs, even if it is not stiff, use an implicit solver. Such solvers are designed to simultaneously solve algebraic constraints and integrate differential equations.

### Full and Sparse Linear Algebra

When you simulate a system with more than one state, the solver manipulates the mathematical system with matrices. For a large number of states, sparse linear algebra methods applied to large matrices can make the simulation more efficient.

### Event Detection and Location

Events, in most cases, occur between simulated time steps.

Fixed-step solvers detect events after “stepping over” them, but cannot adaptively locate events in time. This can lead to large inaccuracies or failure to converge on a solution.

Variable-step solvers can both detect events and estimate the instants when they occur by adapting the timing and length of the time steps.

**Tip**

To estimate the timing of events or rapid changes in your simulation, use a variable-step solver.

If your simulation has to frequently adapt to events or rapid changes by changing its step size, much or all of the advantage of implicit solvers over explicit solvers is lost.

### Unbounded, Bounded, and Fixed-Cost Simulation

In certain cases, such as real-time simulation, you need to simulate with an execution time that is not only bounded, but practically fixed to a predictable value. Fixing execution time can also improve performance when simulating frequent events.

The real-time cost of a variable-step simulation is potentially unlimited. The solver can take an indefinite amount of real time to solve a system over a finite simulated time, because the number and size of the time steps are adapted to the system. You can configure a fixed-step solver to take a bounded amount of real time to complete a simulation, although the exact amount of real time might still be difficult to predict before simulation. Even a fixed-step solver can take multiple iterations to find a solution at each time step. Such iterations are variable and not generally limited in number; the solver iterates as much as it needs to.

Fixing execution time implies *fixed-cost* simulation, which both
fixes the time step and limits the number of per-step iterations. Fixed-cost simulation
prevents *execution overruns*, when the execution time is longer than the
simulation sample time. A bounded execution time without a known fixed cost might still
cause some steps to overrun the sample time.

The actual amount of computational effort required by a solver is based on a number of other factors as well, including model complexity and computer processor. For more information, see Real-Time Simulation.

### Global and Local Solvers

You can use different solvers on different parts of the system. For example, you might
want to use implicit solvers on stiff parts of a system and explicit solvers everywhere
else. Such *local solvers* make the simulation more efficient and reduce
computational cost.

Such multisolver simulations must coordinate the separate sequences of time steps of each solver and each subsystem so that the various solvers can pass simulation updates to one another on some or all of the shared time steps.