Main Content

Speed Up Simulation

Several factors can slow simulation. Check your model for some of these conditions.

  • 该模型包括一个Interpreted MATLAB Functionblock. When a model includes anInterpreted MATLAB Functionblock, the MATLAB®execution engine is called at each time step, drastically slowing down the simulation. Use theMath Functionblock whenever possible.

  • Your model includes a MATLAB file S-function. MATLAB file S-functions also call the MATLAB execution engine at each time step. Consider converting the S-function either to a subsystem or to a C-MEX file S-function.

  • Your model includes a Memory block. Using a Memory block causes the variable-order solvers (ode15sandode113) to reset back to order 1 at each time step.

  • The maximum step size is too small. If you changed the maximum step size, try running the simulation again with the default value (auto).

  • Your accuracy requirements are too high. The default relative tolerance (0.1% accuracy) is usually sufficient. For models with states that go to zero, if the absolute tolerance parameter is too small, the simulation can take too many steps around the near-zero state values. See the discussion of this error inMaximum order.

  • The time scale is too long. Reduce the time interval.

  • Your model represents a stiff system, but you are using a nonstiff solver.Try usingode15s. For more information, see刚度系统.

  • Your model uses sample times that are not multiples of each other. Mixing sample times that are not multiples of each other causes the solver to take small enough steps to ensure sample time hits for all sample times.

  • The model contains an algebraic loop. The solutions to algebraic loops are iteratively computed at every time step. Therefore, they severely degrade performance. For more information, seeAlgebraic Loop Concepts.

You can check the model blocks for the following conditions:

  • Your model feeds a Random Number block into an Integrator block. For continuous systems, use theBand-Limited White Noiseblock in the Sources library.

  • Your model contains a scope viewer that displays too many data points. Try adjusting the viewer property settings that can affect performance. For more information, seeScope Viewer.

  • You need to simulate your model iteratively. You change tunable parameters between iterations but do not make structural changes to the model. Every iteration requires the model to compile again, thus increasing overall simulation time. Use fast restart to perform iterative simulations. In this workflow, the model compiles only once and iterative simulations are tied to a single compile phase. SeeHow Fast Restart Improves Iterative Simulationsfor more information.

Related Examples

More About