Best way to optimize iterative Simulink calls

2 views (last 30 days)
Eric Lemos
Eric Lemos on 1 Nov 2021
Commented: Paul on 9 Nov 2021
Hi guys, hope you all are doing well.
I have a script that simulates a Simulink model for various parameters of a PI controller with the sim() function. At each iteration only the parameters of this controller change, and nothing else in the model. It is a fixed step simulation (0.5s step) with duration of 6000s.
When I simulate the script with a fixed pair of PI parameters, it takes roughly ~48s to end the simulation, whereas in the the simple loop I made it takes way too long per call (something like 1min20s approximately). Is there a way to optimize those sequential calls? I've read through some MATLAB docs but nothing really insightful came up.
The loop I made is something like:
for i = 1:n_tests
% Parameters of the PI
Kp_PI = Kp_value(i); Ti_PI = Ti_value(i);
sim('Simulink_model', 6000)

Answers (1)

Paul on 1 Nov 2021
I have no idea why a run in the loop takes so much longer than a single run not in the loop. Might be worth reaching out to Tech Support.
A good start in the doc pages is Running Multiple Simulations. In particular you can pre-define an array of Simulink.SimulationInput objects, and the use either sim() or parsim(), if you have the Parallel Computing Toolbox.
For sure, the parsim() should be faster than calling sim() in a loop. Don't know if calling sim() with an array of Simulink.SimulationInput objects would be faster than the loop you wrote. Worth a look though.
Paul on 9 Nov 2021
According to this thread
out = sim('model');
out.SimulationMetadata.TimingInfo % shows the timing for all phases of the simulation

Sign in to comment.





Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!