Issue with calling a for loop before the sim command.

4 views (last 30 days)
When I use a for loop just before calling the sim(model) command to simulate a Simulink model, I receive this error:
Error using ERROR_BUCLE_RARISIMO (line 139)
Derivative of state '1' in block 'PhasorPVESSSMModel/SG/SM/Electrical model/Synchronous Machine Continuous
Model/phi/Integrator' at time 0.0083 is not finite. The simulation will be stopped. There may be a
singularity in the solution. If not, try reducing the step size (either by reducing the fixed step size
or by tightening the error tolerances)
However, if I simulate before calling the for loop, the error does not appear.
My initial idea was to include the sim command within the loop to simulate the model for different parameters, but I always encountered this error. Initially, I thought it was an issue with the parameters I passed to the model, but now I've noticed that simply calling the for loop doesn't work.
I have tried everything, and I'm not sure what could be causing this failure. Can anyone help me? Thank you very much.

Answers (1)

Sugandhi on 8 Jun 2023
Edited: Sugandhi on 8 Jun 2023
I understand that you are receiving error while simulating simulink model using for loop and sim(model).
The error message suggests that the model simulation has encountered a singularity, which may be caused by dividing by zero or approaching an infinite value in any one of the blocks.
It's possible that when you include the `sim` command within the loop, the simulation may be progressing too quickly and not allowing enough time for the model to stabilize before the next iteration. This might result in the model diverging from the expected behaviour and causing the singularity.
One of the possible workarounds could be:
To add a pause within the loop to allow enough time for the model to stabilize before the next iteration. You can adjust the duration of the pause as needed to ensure that the model has enough time to reach a steady state before the next iteration begins.
Here's an example code that includes a pause of 1 second between model simulations in a for loop:
for i = 1:n
% Prepare input signals and set parameter values
% Simulate the model
simOut = sim(model);
% Process simulation results
pause(1); % adding 1 second pause before next iteration
You might also try reducing the simulation step size or tightening the error tolerances by adjusting the simulation parameters. This helps to identify the source of the singularity and mitigate its effects.
For more information, kindly go through the following links:


Find more on Trimming and Linearization in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!