# Tips for Simulation Performance5

Posted by Guy Rouleau,

By Guy Rouleau

One of the questions I hear the most often from Simulink users is What can I do to make my model run faster? Here is a list of tricks that can help.

Displays and Scopes

When you have a display or a scope in your model, Simulink needs to update it. To speed up the execution of the model, display only the necessary. This can mean:

• Limit the amount of visible displays and open scopes.
• For open scopes and displays, set the decimation to a reasonable value.
• Load the model in memory using load_system instead of open_system and simulate it using the sim command, then post process/display the outputs.

Interpreted blocks

When a model includes a MATLAB Fcn block or MATLAB file S-function, the MATLAB interpreter is called at each time step. Try replacing these blocks by the Fcn block, the Math Function block, the Embedded MATLAB Function block (without extrinsic functions) or a C-MEX S-function.

Algebraic loops

I saw this TV commercial recently. I think this comment about pick-up lines could also apply for algebraic loops:

"There is a time and a place for them. The time is never... you can figure out the place on your own."

Seriously, sometimes algebraic loops are unavoidable, but they always slow down models.

Solver Options

Solver settings influence simulation performance, especially for variable-step solvers. Look at the time steps taken by your model. I usually do it by logging the simulation time (tout) and displaying it using this command:

semilogy(tout(1:end-1),diff(tout))
Based on what you see, it is possible to determine which setting to change. This can include solver choice, max step size, relative and absolute tolerances and zero crossings detection, etc.

How solver settings influence simulation performance is a complex topic too long for this post. I recommend looking at the documentation page titled Choosing a solver for more details. Leave a comment below if you would like to see a future post on improving simulation performance through solver settings.

Accelerator and Rapid Accelerator modes

Accelerator mode converts the block methods into a C-MEX S-function. Since the block methods are compiled instead of interpreted, the model usually runs faster. The Rapid Accelerator mode creates a standalone executable from your model. The executable is launched in a different process that will run in a separate thread on a different processor if you have a multi-core machine.

Here are a few tips when using the Accelerator and Rapid Accelerator mode:

• Avoid blocks that do not support code generation. They run in interpreted mode and can slowdown your model.
• Try switching the Compiler Optimization Level from faster build to faster run.
• For maximum speed, start your simulation using the sim command when using these modes.

Look at the documentation page titled Comparing Performance. This will give you a good idea of when these modes should be used.

Parallel Computing Toolbox

To run multiple simulations in parallel, calling sim from within parfor is a good idea. This allows you to fully take advantage of all the processors in your machine when performing parameter sweeps and Monte Carlo analysis.

In your MATLAB installation, look for the demo titled Rapid Accelerator Simulations Using PARFOR for an example.

Where to look for more?

The Simulink documentation contains a chapter titled Improving Simulation Performance and Accuracy. I strongly recommend going through this chapter to create simulations running at optimal speed.

How do you ensure that your simulation is running at optimal speed? Do you have tricks not mentioned here? Post a comment here.

Guy Rouleau replied on : 1 of 5

One more additional resource I forgot to mention, Tech-note 1806 – Simulink Performance & Memory Management Guide:

http://www.mathworks.com/support/tech-notes/1800/1806.html

29th,October 2010
Chennai-62.
Dear sir,
I am very keen interest on knowing in Simulink programmes of Subsynchronous resonance reducing by Thyristor controlled series capacitor simulation. please help in this regard.
Mahaboob Basha Qureshi
Asst. Professor in EEE dept.

Ramu Paladugula replied on : 3 of 5

Thanks Guy,

I read this article when I was trying to simulate a large model running 334 times slower than real-time. I used some of your tips and it ran 25 times slower than real-time. I did not change the functionality of the model in anyway. Thank you very much!

I make changes to my model save and close it. When I use the load_system and sim command after that MATLAB crashed.
I restart MATLAB and the model simulates normally.
Can you please explain why this occurs?

Guy Rouleau replied on : 4 of 5

Hi Ramu,

I am happy to know the tricks have been useful for you!

If you experience a crash, please contact technical support and provide as many details as possible to allow us to reproduce and fix this issue.

Thanks

Guy

KE replied on : 5 of 5

Very useful. I have been seeking this information but it is spread out in the documentation. Could you have a future post on dynamic stability, i.e. sensible choices for model setup that prevent problems?

What is 9 + 10?

Preview: hide

These postings are the author's and don't necessarily represent the opinions of MathWorks.