MATLAB System Block evaluated multiple times per time step
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am running a Simulink model with a fixed-step size of 1s and automatic solver settings, which is usually configured to ode3 by Simulink during the simulation. I noticed that the MATLAB System block is executing multiple times per time step (for e.g, it executes at 1, 1.5, 1.75, 2 seconds). If I specify the solver to be ode1, this issue will be resolved, however I plan to integrate the MATLAB System block in a more complicated pre-existing Simulink block where the solver settings will be different.
Is there a way to fix this so that the code inside 'stepImpl' function of MATLAB System block runs only once at major time steps (1,2,3 seconds)?
I found a similar post at below link, however the solution seemed to be specific to the MATLAB Function Block. There are no settings in Model Explorer to configure MATLAB System Block to use "Discrete" update method as suggested in the below post. (It only exists for MATLAB Function Block)
Kindly also let me know if this issue will be resolved automatically if I use (Level 2) s function.
(Basically, I am trying to use the example shown in https://web.casadi.org/blog/mpc-simulink/, however they used a Variable step solver, ode45 in the demo file, which I cannot for the scope of my project.)
0 commentaires
Réponses (1)
Shree Charan M L
le 19 Oct 2023
Hi Anshul,
I understand that you want to implement a MATLAB system block that executes a certain algorithm only at major time steps regardless of the solver configuration.
You may add a check on the current time in the “stepImpl” function of your MATLAB System block to achieve the required behaviour regardless of the solver. You can achieve this by following these steps:
1) Add a persistent variable to keep track of the last major time step and initialize it to the start time of your simulation.
properties(Access = private)
LastMajorTime = 0;
End
2) In the “stepImpl” function execute your algorithm only if the current time is greater than or equal to the “LastMajorTime”.
function stepImpl(obj)
currentTime = obj.getCurrentTime();
if currentTime >= obj.LastMajorTime
% run your algorithm here
% ...
% Update the last major time step
obj.LastMajorTime =obj.LastMajorTime + 1;
end
end
The step algorithm now only runs at the major time steps.
obj.LastMajorTime =obj.LastMajorTime + 1;
can be modified to suit to the required major times. For example, if the required major times are 0,2,4,6,8…
obj.LastMajorTime =obj.LastMajorTime + 2;
can be used.
Hope this helps and you're able to implement your algorithm only at major time steps.
0 commentaires
Voir également
Catégories
En savoir plus sur Simulation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!