MATLAB: How many times does a Matlab Function in Simulink run in one time step

embedded matlab functionrun times in a stepsimulink

Hello,
I have a Matlab Function in a Simulink model. Because the Matlab Funtion is time-confusing, I just want it to run one time in a step. But it seems to run multiple (>5) times in a step. I was wondering whether the number of times a Matlab function runs in a step depends on the solver (ode1,ode4, etc) selected. Can anyone provide some insights into this question? Thank you.
Regards Pan

Best Answer

After struggling for one day, I found two main causes for my problem. The first one is that there was an algebraic loop in my model due to a missing setting in a S function, which made Simulink run the model MULTIPLE times in ONE time step to solve the algebraic loop. The second one is that the [Update Method] of my Matlab Function was set to be [inherited]. The problem disappeared after (1) removing the algebraic loop and (2) changing the [Update Method] to [discrete] and setting the [Sample Time] equal to the (fixed) step size of the model.
Hope this may help people who confront similar problems.