## Real-Time Simulation Workflow

The figure shows the real-time simulation workflow. The connectors are exit points for returning to the real-time model preparation workflow.

The figure shows the real-time model preparation workflow. The connector is an entry point for returning to the real-time model preparation workflow from other real-time workflows (for example, the real-time simulation workflow or the hardware-in-the-loop simulation workflow).

Before performing this workflow, prepare your model for real-time simulation using the Real-Time Model Preparation Workflow. The real-time model preparation workflow shows you how to obtain reference results, determine the maximum step size, and modify your model to simulate quickly and produce accurate results.

Use the real-time simulation workflow to increase the likelihood that your model is real-time capable. Your model is real-time capable if it meets both of these criteria when you simulate it on your real-time computer:

The results match your expectations, based on empirical data or theoretical models.

The model simulates without incurring an overrun.

The real-time simulation workflow uses bounded, that is fixed-step, fixed-cost, simulation. Fixed-step, fixed-cost simulation sets an upper boundary on computational cost by limiting both the step size and the number of iterations that the solver uses.

### Make Your Model Real-Time Viable

#### Perform Fixed-Step, Fixed-Cost Simulation

Run your model on a desktop computer using fixed-step, fixed-cost configurations for the global solver and local solvers. For more information on specifying fixed-step, fixed-cost solver configurations for real-time simulation, see Choose Step Size and Number of Iterations and Fixed-Cost Simulation for Real-Time Viability.

#### Evaluate Model Accuracy

Compare the results from the simulation on the target computer to your reference results. Are the reference and modified model results the same? If not, are they similar enough that the empirical or theoretical data also supports the results from the simulation of the modified model? Is the modified model representing the phenomena that you want it to measure? Is it representing those phenomena correctly? If you plan on using your model to test your controller design, is the model accurate enough to produce results that you can rely on for system qualification? The answers to these questions help you to decide if your real-time results are accurate enough.

#### Improve Accuracy by Adjusting Solver Settings

If your fixed-step, fixed-cost simulation results do not match your reference results, try to improve accuracy by adjusting solver configurations. Increasing the number of iterations or decreasing the step size can improve accuracy.

For an implicit global solver (ode14x, ode1be), increase the number of Newton’s iterations. For a Backward Euler or Trapezoidal Rule local solver, increase the number of nonlinear iterations.

For the global solver, and for any local solvers, decrease the step size. Configure the step size for each local solver as an integer multiple of the step size you specify for the global solver.

#### Return to the Real-Time Model Preparation Workflow

If changing solver configurations does not improve or speed enough, try to make your model real-time capable by returning to the real-time model preparation workflow.

Adjust the fidelity or scope of your model, and then step through the other processes and decisions in the real-time model preparation workflow. Iterate on adjusting, simulating, and analyzing your model until it is fast and accurate enough for you to attempt the real-time simulation workflow again. For information, see Real-Time Model Preparation Workflow.

#### Evaluate Overrun Risk

In terms of speed, the only method for definitively determining that your model is real-time capable is to test for overruns during simulation on your target hardware. You can, however, use fixed-step, fixed-cost simulation to estimate the likelihood that your solver executes quickly enough for real-time simulation. For information on estimating simulation time, see Estimate Computation Costs.

#### Improve Simulation Speed by Adjusting Solver Settings

If your computational cost estimate indicates that your model executes too slowly to avoid an overrun on a real-time target machine, try to increase simulation speed by adjusting solver configurations. Decreasing the number of iterations or increasing the step size can improve accuracy.

For an implicit global solver (ode14x, ode1be), decrease the number of Newton’s iterations. For either a Backward Euler or Trapezoidal Rule local solver, decrease the number of nonlinear iterations.

For the global solver, and for any local solvers, increase the step size. Configure the step size for each local solver as an integer multiple of the step size you specify for the global solver.

#### Model Is Real-Time Viable

When fixed-step, fixed-cost simulation results indicate that your model is likely real-time capable, you can attempt real-time simulation on the target hardware. For information on how you can use real-time simulation to test your controller hardware, see Basics of Hardware-In-The-Loop simulation.

#### Return to the Real-Time Simulation Workflow

The connector is an entry point for returning to the real-time simulation workflow from another workflow (for example, the hardware-in-the-loop simulation workflow).

### Insufficient Computational Capability for Real-Time Viability

It is possible that your real-time target machine lacks the computational capability for running your model in real time. If, after multiple iterations of the workflow, there is no combination of model complexity and solver settings that makes your model real-time viable, consider these options for increasing processing power: