Clear Filters
Clear Filters

Simulation at different frequencies and solver management.

6 views (last 30 days)
Hi,
I've been using Simulink for several years now, and I still don't understand the solver parameters:
I often have to simulate mechanical systems (non-linear), where the excitation frequency varies a lot.
The two most typical cases are :
  • Case 1: Performing a series of simulations at a fixed excitation frequency with parsim, for frequencies ranging from 1 to 10 kHz.
  • Case 2: Simulation of a system with a sweep from 1Hz to 10kHz.
In both case 1 and case 2, the problem is that the various solvers don't seem to take into account the acceleration of the system dynamics. I end up with a huge oversampling at low frequencies and therefore very long simulations, or an obvious undersampling at high frequencies.
Here are the different techniques I used:
  • For case 2: use my own point list with the option "Produce specified output only" in the "data import/export" part, and set the solver with maxStepSize, MinStepSize and RelativeTolerence super large so that it only produces my specified points. This method looks really ugly, but it works for case 2. I'm surprised I haven't found a method that's a bit more "designed for".
  • For case 1, however, I haven't found a solution. I did try this method with a fixed time step: https://fr.mathworks.com/matlabcentral/answers/871323-missing-parameter-to-change-fixed-step-size-via-code ,but with parsim, a frequency variation leads to the error "The model Blablabla has unsaved changes. The model must be saved before running parallel simulations.” What's more, this method seems to me uglier than really understanding once and for all how matlab solvers work.
Let's take an extremely simple example, which may help me to understand: A SinWave block connected to a scope.
With the following parameters :
Type : Variable-step
Solver : Auto
Max step size : Auto
Relative tolerance : 1E-3
Min step size : auto
Absolute tolerence : Auto
For a frequency of 1 Hz, and StopeTime = 10s: the result is just right (At 100 Hz, the result is not acceptable).
For a frequency of 0.1 Hz, and StopeTime = 10s: the result is over-sampled:
What parameters should I adapt so that my sampling follows the dynamics of the system, whatever the frequency in this super-basic case?
And how does "Relative tolerance" work, what "state" is relative tolerance based on?
(In the meantime, if anyone has a super ugly solution for varying the sampling frequency in fixed time steps with parsim, I'd love to hear about it :) )
Sorry for this long message, and thank you in advance for your answers 😊
Adrien

Answers (1)

Sayan
Sayan on 12 Apr 2024
Hi Drenay,
I too face the issue while changing the sampling time using "parsim". However, a workaround to this is using "sim" to simulate the model for multiple simulation inputs with different sampling times. I am adding a code snippet of the same herewith.
% I am taking a range of 1 kHz to 10 kHz
samplingFrequencies = 1000:1000:10000;
samplingTimes = 1 ./ samplingFrequencies;
numSimulations = numel(samplingTimes);
simIn(1:numSimulations) = Simulink.SimulationInput('Your_model_name');
for i = 1:numSimulations
% Set the Fixed-step value for each simulation
simIn(i) = simIn(i).setModelParameter('FixedStep', 'samplingTimes(i)');
end
simOut=sim(simIn);
Now, to understand which solver best suits your system, you need to understand the Variable step and Fixed step solvers and how they work.
  • Fixed-step solvers solve the model at regular time intervals from the beginning to the end of the simulation. The size of the interval is known as the step size. You can specify the step size or let the solver choose the step size. Generally, decreasing the step size increases the accuracy of the results while increasing the time required to simulate the system.
  • Variable-step solvers vary the step size during the simulation. They reduce the step size to increase accuracy when a model's states are changing rapidly and increase the step size to avoid taking unnecessary steps when the model's states are changing slowly. Computing the step size adds to the computational overhead at each step but can reduce the total number of steps, and hence the simulation time required to maintain a specified level of accuracy for models with rapidly changing or piecewise continuous states.
Both the solvers have a time-accuracy tradeoff. If your system contains irregular dynamics, i.e., you need the solver to take small steps during rapid changes and big steps during changes at constant slope, Variable step solver would be ideal. However, you need to define the "Max step size" to ensure the solver does not take bigger than the required step size for the system.
The relative tolerance indicates the tolerance allowed for the computational accuracy of the variable-step solver as a percentage of each state value. The default value 1e-3 indicates that the computed state value at each time step must be accurate within 0.1%. You can reduce this value to increase the accuracy of the system, but it will also increase the simulation time.
To know more about the solvers you can go through this documentation.
A comparison between Fixed step and Variable step solver is documented here.
Hope this answers you query.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!