# How Simulink handles calls to discrete blocks

2 views (last 30 days)
Lorenzo on 28 Nov 2023
Commented: Paul on 29 Nov 2023
I have a Simulink model which includes a Matlab System Block for which I have defined a discrete sample time following this guide.
Indeed, if in Simulink I enable the sample time information (from Debug/Information Overlays panel), I see that the output signals are characterized by that sample time.
Instead, as inputs to this block I have signals with different sample times or multirate.
I am simulating with a ode45 Variable-Step solver.
Let's denote with the simulation end time. The initial time is 0.
If I run the Simulink profiler I see that this Matlab System Block, characerized by a sample time , gets called n times.
Though, I notice that . What I see is that .
Could you help me understand why?

Fangjun Jiang on 28 Nov 2023
That is right. Read this. For every integration step, the derivative function needs to be called between 4 times (for RK4) and 6 times (for RK5).
Lorenzo on 29 Nov 2023
So, when I am modeling a discrete system, I am not using differential equations, but finite difference ones.
This means that, for example for a linear system, I have:
So, conversely to continuous systems, there is no derivative. The system state evolves only at discrete time steps. This means that whatever solver I use, I think it should call the block only at multiple integers of the sample time, because there is no integration to perform (which seems to be aligned with the link you shared https://it.mathworks.com/help/simulink/ug/types-of-sample-time.html).
In the meanwhile, I gave a quick read to other help pages (such as https://it.mathworks.com/help/simulink/ug/managing-sample-times-in-systems.html), which might explain why discrete systems could be called more times.
But:
1. I need to carefully evaluate if this is my case.
2. Still, I do not understand why multiple calls are necessary (if not done at multiple steps of the discrete sample time), given that the system state only changes at discrete steps. Also because calling a block could be time consuming (as it is in my case since within the discrete block I am invoking some python code).
Hope it helps to clarify my point.
Paul on 29 Nov 2023
------------------------------------
Discrete Sample Times
Given a block with a discrete sample time, Simulink® software executes the block output or update method at times .... (emphasis added)
---------------------------------
I realize that a few lines later that same doc page says " executes the output method." Nevertheless, a block with discrete sample time is only executed (update and output methods) at the integer multiples of the sample time (pluss offset if nonzero).
"dy/dt=f(u,y,t)+d(u,y,t) " with u and y continuous.
In this case, dy/dt is computed at the major and minor time steps of the ODE solver, but d(u,y,t) is only executed as its discrete sameple time hits. If using a variable step solver, Simulink ensures that each sample time hit occurs on a major time step. If using a fixed step solver, the step size has to be the sample time of the d(u,y,t) block divided by an ineger >= 1. In either case, the d(u,y,t) block is executed on the major step prior to the start of the integration, i.e., d(u,y,t) is computed at t_n and held constant over all of the dy/dt calculations that are needed to step from t_n to t_n + dt. More precisely, even if t_n + dt = t_(n+1) the d(u,y,t) block is not executed on the final minor time step of the integration. At least that's my understanding based on Simulation Loop Phase
What did you see on that linked doc page that "might explain why discrete systems could be called more times."?
Based on your comment above, it sounds like your model is strictly discrete-time, and therefore should be using a discrete-time solver. But your original question mentions using ode45, which is a continuous-time solver? However, even if the solver is set to ode45, Simulink will automatically change it the to the variabie-step discrete-time solver if there are no continuous states. Can you clarify whether or not the model has continuous states? Is the solver is fixed- or variable-step? If the former, what is the step size? In either case, what's the sample time of of the Matlab System Block?

### Categories

Find more on Configure Simulation Conditions in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!