## Improving Speed and Accuracy

### Why Speed and Accuracy Matter for Real-Time Simulation

Speed and accuracy are the determining factors for making your model real-time capable. Your model is real-time capable if it satisfies both of these conditions when you simulate it on your particular target hardware:

There are no overruns.

The simulation results meet your criteria for accuracy.

Speed is objective. The real-time clock determines whether your model is fast enough for real-time simulation. For each step that your solver takes, your real-time hardware system tracks the time that it takes to complete these processing tasks:

Execute the simulation.

Process input and output.

Perform general computer tasks.

An overrun occurs when, for any time step, the time that it takes your system to complete the processing tasks exceeds the real-time limit for the tasks. If your target machine reports any overruns when you use it to simulate your model, your model is not fast enough for real-time simulation.

Your Simscape™ model is accurate if it produces results that agree with the empirical and theoretical data that are the basis for your model. Accuracy is more subjective when the foundation and simulation data are similar, but are not in absolute agreement. To determine if your model is accurate enough for real-time simulation when the data do not match perfectly, consider these questions:

Is the model representing the phenomena that you want it to measure?

Is it representing those phenomena correctly?

If you plan to use your model to test your controller design, is the model accurate enough to produce results that you can rely on for system qualification?

The only way to test whether your model is real-time capable is to run it on your actual real-time target hardware using fixed-step, fixed-cost solvers. You can, however, estimate whether the model is both fast and accurate enough for real-time simulation by analyzing the results from desktop simulation. To estimate whether your model is real-time capable, see Determine Step Size and Estimate Computation Costs.

If the analysis from the desktop simulation indicates that your model likely is not real-time capable, increase model speed or accuracy before deploying your model to your real-time target machine. Increasing the speed of your simulation tends to decrease the accuracy, and conversely increasing accuracy decreases speed. To make your model real-time capable, maintain a balance between speed and accuracy.

### Balancing Speed and Accuracy

Simulation speed and accuracy correlate to your choices for:

Model fidelity and scope

Real-time hardware computing power

Solver sample time (step size) and number of iterations

To try to increase simulation speed, potentially at the expense of accuracy:

Decrease model fidelity or scope.

Increase sample time.

Decrease the number of solver iterations.

To try to increase simulation accuracy, potentially at the expense of speed:

Increase model fidelity or scope.

Decrease sample time.

Increase the number of solver iterations.

To try to increase both accuracy and speed, or either one without sacrificing the other, increase computing power. To increase computing power, use a faster real-time processor or compute in parallel.

The type of solver that you specify also affects simulation
speed and accuracy. For fixed-step simulation, Simscape local
solvers are faster and as accurate as Simulink^{®} global solvers.
Implicit solvers are faster, but less accurate than explicit solvers.
However, the numerical stiffness of the network is also a determinant
for deciding whether to use an implicit solver or an explicit solver.
Explicit solvers yield more accurate results for numerically stiff
networks.

For more information on how model complexity affects speed and accuracy, see Eliminating Effects That Require Intensive Computation. For more information, on how solver configurations affect speed and accuracy, see Optimizing Local and Global Solver Configurations.

It is possible that there is no combination of model complexity and solver settings that can make your model real-time capable. If the simulation does not run in real time on the target machine, or if the accuracy is unacceptable, consider these options for increasing speed and accuracy:

### Eliminating Effects That Require Intensive Computation

If your desktop simulation analysis indicates that your model likely is not fast enough for real-time simulation, eliminate effects that require intensive computation. Identify elements in your model that cause costly effects, such as discontinuities and rapid changes, that tend to slow down simulations.

Elements that cause discontinuities include:

Hard stops or backlash

Stick-slip friction

Switches or clutches

Elements with small time constants that cause rapid changes include:

Small masses attached to stiff springs with minimal damping

Electrical circuits with low capacitance, inductance, and resistance

Hydraulic circuits with small compressible volumes

To eliminate or modify the elements that are responsible for the effects that slow down your simulation, use these approaches:

Replace nonlinear components with linearized versions.

Replace complex equations with lookup tables for their solution.

Replace complicated components with simplified models.

Smooth discontinuous functions (step changes) with filters, delays, and other techniques.

### Optimizing Local and Global Solver Configurations

You can also influence the speed and accuracy of your simulation by way of your solver specifications. The level of accuracy that your real-time target machine delivers does not necessarily correlate to a specific step size across all networks in a single model. A real-time target machine can give accurate results for a simple network in your model but inaccurate results for a more complex network. Take advantage of the ability to specify different solver configurations for each network in your Simscape model. To help make your model real-time capable, configure your fixed-step global solver and each local solver individually.

For information on solver options and determining the solvers that help to make your Simscape model real-time capable, see Solvers for Real-Time Simulation.

### Upgrading Target Hardware

Different targets give varying levels of accuracy when using the same step size to simulate the same model. You can speed up or increase the accuracy of the real-time simulation by using a faster real-time target computer.

### Simulating Parts of the System in Parallel

Another approach for increasing speed while maintaining accuracy is to configure your model to evaluate multiple physical networks in parallel. You can partition your model if the networks are not dependent upon one another. Work with and experiment with your model, the generated code, and the real-time target machine to use this approach.

## Related Examples

- Determine Step Size
- Estimate Computation Costs
- Reduce Computation Costs
- Reduce Fast Dynamics
- Reduce Numerical Stiffness
- Reduce Zero Crossings
- Partition a Model