Once you have selected the proper method (continuous, discrete, or phasor), solver type, and parameters, there are additional steps you can take to optimize your simulation speed. Here are the best practices for getting your models to run as fast as possible.
1. Discretize Your Circuit and Controllers
Discretizing your electric circuit and your control system is highly recommended. To optimize further, you can even use a larger sample time for the control system, provided that it is a multiple of the smallest sample time used in the electrical circuit.
2. Optimize Scopes and Data Logging
Visualization tools consume memory and processing power. Reducing the number of open scopes and the number of points saved in the scope also helps in reducing the simulation time.
3. Use Initial States (Skip the Transient)
If you are simulating repeatedly from a particular operating point, you should specify a vector of initial states. This allows you to skip the initialization transient every time you run the model.
- First, simulate your model and save the initial states as a vector in the MATLAB® workspace.
- In Simulink®, navigate to the Modeling tab and select Model Settings > Model Settings.
- In the left pane of the Configuration Parameters dialog box, select Data Import/Export.
- Finally, select the Initial state parameter and enter the name of the workspace variable that contains the vector of initial states.
4. Enable Simulink Accelerator Mode
Using the Simulink Accelerator mode is one of the easiest ways to speed things up. Typically you can expect performance improvements by factors of two to 10. The performance gain obtained with the Accelerator varies with the size and complexity of your model.
- How it works: The Accelerator mode speeds up the execution of Simulink models by replacing the interpreted M code running beneath the Simulink blocks with compiled code as your model executes.
- Requirements: The Accelerator mode uses portions of Simulink Coder™ to generate this code on the fly. Although the Accelerator mode uses this technology, Simulink Coder is not required to run it. If you do not have your own C compiler installed, you can use the LCC compiler provided with your MATLAB installation.
5. The Ultimate Speed: Standalone C Code
To take full advantage of the performance enhancements made possible by converting your models to code, you must use Simulink Coder software to generate standalone C code. You can then compile and run this code and, with Simulink Real-Time™ software, also run it on a target PC operating the Simulink Real-Time real-time kernel.