Simulink Model simulation: huge time differences between simulation "arrangements"
16 views (last 30 days)
Show older comments
Hi,
this is my first post here and I am a MATLAB newbie...
I "inherited" a certain nonlinear system described via a Simulink "schematic", consisting essentially of interlinked:
- Matlab Function blocks, implementing a static nonlinearity (using exp,abs,phase...)
- First order filters described in the s-domain
So far, with ChatGPT's help I should not be ashamed to say, I managed to create a staircase input for my model within a *.m script, and through it, launch a time-domain simulation via:
%% --- Model & Common Inputs ---
model = 'my_simulink_model';
[..]
ds1 = Simulink.SimulationData.Dataset;
ds1 = ds1.addElement(Pin_ts1,'Pin');
[..]
simIn1 = Simulink.SimulationInput(model);
simIn1 = simIn1.setExternalInput(ds1);
simIn1 = simIn1.setModelParameter('SolverType','Variable-step',...
'Solver',solver,...
'RelTol',RelTol,...
'AbsTol',AbsTol,...
'StopTime',num2str(tf1));
simOut1 = sim(simIn1);
This way, by setting solver type (it's a nonlinear "stiff" system, I'm using ode15), variable-step and the rest, I managed to get a satisfyingly fast and accurate simulation.
I then ran into issues when I wanted to extract a linearized version of the system at a certain timepoint (I'm not asking about that here, I'll do separately in case) and I could not find a working answer from ChatGPT, so I decided I needed to understand the tool better...
Since what I could find about linearization in help is all GUI/Simulink based (I do have the Control Design Toolbox license, btw, I bought exactly for this purpose), I then decided to learn to simulate from the Simulink interface directly...
I then instantiated my model in another Simulink canvas, created the various constant inputs to the system, and added a time-varying staircase input which, at least in my understanding, should be equivalent to the one I created programmatically.
The code to create the staircase is:
t_step1 = 50e-3;
I_max = 9.8e-3;
I_min = 0;
N1 = 50;
I_HTR_sweep1 = linspace(I_max, I_min, N1);
tf1 = t_step1 * N1;
t_edges1 = 0:t_step1:tf1;
t_plot1 = reshape([t_edges1(1:end-1); t_edges1(2:end)],1,[]);
I_plot1 = repelem(I_HTR_sweep1, 2)';
% Timeseries
I_HTR_ts1 = timeseries(I_plot1, t_plot1);
I don't know whether this is MATLAB coding at its finest, but it does make sense to me and it results in a descending staircase of t_step1 seconds plateau for each step.
In the Simulink/GUI approach, I feed to the same model port a "repeating sequence stair" block, whose vector expression I've filled with:
linspace(9.8e-3,0,50)
and "sample time" with a variable I created on purpose, named t_step and set at 50e-3.
I also changed the setting in the "model setup" tab, which is responsible for solver options and such - To the values I have programmatically set before.
However - now the simulation takes AGES to complete! Actually, it takes so long that I got bored before I could see the first step happening.
Now, since the model is absolutely the same, I am assuming that something BIG is missing from my Simulink/GUI setup which isn't or is set automatically "right" when the model is simulated from a script.
Do you have any suggestions and/or hints on what to look for that could cause such vastly different behaviour?
Despite the fact that the time step is variable in both cases, the GUI based simulator seems to take very, very tiny steps. Which is bizarre, because the time constants of the system are much larger, so I wonder where am I driving it nuts?
2 Comments
Sam Chak
on 21 Aug 2025
Hi @Michele
I plotted the staircase signal from your code. However, I am unsure whether the ode15s solver in Simulink can handle this "stiff" situation. The slope of the perfectly right-angled staircase at each time interval of 0.05 is infinite (∞). ODE solvers like ode15s can approximate the Jacobian matrix internally using the finite difference method. However, from the t_plot1 data, I anticipate that there will be many division-by-zero events.
t_step1 = 50e-3;
I_max = 9.8e-3;
I_min = 0;
N1 = 50;
I_HTR_sweep1 = linspace(I_max, I_min, N1);
tf1 = t_step1 * N1;
t_edges1 = 0:t_step1:tf1;
t_plot1 = reshape([t_edges1(1:end-1); t_edges1(2:end)],1,[]);
I_plot1 = repelem(I_HTR_sweep1, 2)';
t_plot1(1:8)
plot(t_plot1, I_plot1, '.'), grid on,
title('Descending staircase signal')
xlabel('t'), ylabel('I')
Have you tried something that is mathematically simpler?
x = linspace(0, 2.5, 2501);
amp = 0.01; % amplitude
y = amp*(1 - floor(x/0.05)/48);
plot(x, y), grid on
ylim([0, 0.01])
Accepted Answer
More Answers (1)
Ritika Thusoo
on 22 Aug 2025
Hi,
You can check a couple of things to understand why simulation takes ages for you.
First, check the sample time that you have selected. Simulink® allows you to specify a block sample time directly as a numerical value or symbolically by defining a sample time vector. You can read about this from the following documentation page:
Next, you can check the type of Solver that is better suited for your system considering that you’re also working with a linearised model at one point. Check the following documentation page to read more about the solver:
In order to decide which solver to select for the GUI Simulation you can check out the following documentation:
Furthermore, you can check zero crossing detection in your system which might cause additional time in simulation. You can read about this on the following documentation page:
See Also
Categories
Find more on RF Blockset Models for Transceivers 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!
