How to change evaluation time of a Matlab Function in Simulink

25 views (last 30 days)
Hi everyone,
I had a simscape model whose simulation was relatively short. I added a block of a Matlab Function to the model to compare by an algorythm some of the signals to control the state of a switch. Although the algorythm is quite simple, the simulation time now is ridicously long. Do you now how can I reduce it? Is there any option to change the step time at which the function (ONLY THE FUNCTION) is evaluated?
Thanks in advance!

Answers (2)

Yoshi Minagawa
Yoshi Minagawa on 14 Jul 2021
Hi Fernando,
Although I didn't see your model and so I couldn't find the reason of the problem, I described the tips for this problem bellow. I hope this will help for you.
1. Set sample time of MATLAB function
In block parameters dialog(right click the block and select "Block Parameter") , you can set sample time for this block. It may be necessary to place Rate transition blocks before and after the range where the sample time was changed. (Please see the URL below for details)
2. Using local solver for simscape
Using Solver Configuration block you can use local solver for simscape. It enable you to use different solver to simulate simulink model and simscape model individually.
3. Profiling the reason of stack
To profile the reason of stack, you can use "Solver Profiler". Please see the picture how to start this app. (Please see the URL below for details)
Please be careful that there is not Algebra loop in your model. If there is, that can make slower the simulation speed. In another case, using stiff solva might improve this problem (ode23t or something) because the model may be stiff due to the mixture of simulink model and simscape model.
I hope this will help.

Jonas on 30 Aug 2021
If you put a MATLAB function block in your model, it will evaluate the MATLAB function code at each sample time. (depends on your model's architecture, but I am assuming so) This can indeed lengthen your simulation.
If you do not need the execution of the MATLAB function block each sample time, you can put the MATLAB function block inside a Triggered Subsystem, and then you can add a Step block with a certain step time to trigger the execution and calculation of the MATLAB function block just once. You could trigger the Triggered Subsystem block again to make the MATLAB function block calculate again, but then you need something more advanced than a step block, maybe a Pulse Generator if you like.
Hope it helps or gives inspiration.


Find more on General Applications 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!