How Simulink handles calls to discrete blocks
Show older comments
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.
, gets called n times. Though, I notice that
. What I see is that
.
. What I see is that
.Could you help me understand why?
Answers (1)
Fangjun Jiang
on 28 Nov 2023
0 votes
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).
6 Comments
Paul
on 28 Nov 2023
"for which I have defined a discrete sample time"
But if the block has a discrete sample time, shouldn't it only be executed at that sample time?
Fangjun Jiang
on 29 Nov 2023
Good question! I have to give it some thoughts and maybe do some experiments later.
- Assume dy/dt=f(u,y,t)+d(u,y,t) where f(u,y,t) is a continuous function, and you define d(u,y,t) as a "discrete function", or at least, on the surface, you can define the 'sample time' of the implementation of the d(u,y,t) as a discrete sample time. When doing RK45 integration, how do you think we should evaluate dy/dt 4 or 6 times? The u and y in d(u,y,t) are continuous. I don't mean to count questioning but want to trigger some thoughts.
- In Simulink document, the discrete sample time means "executes the output method of a block". It might be helpful if you can provide more details of your MATLAB System block. Might there be a difference between output method and update method? https://www.mathworks.com/help/simulink/ug/types-of-sample-time.html
- What I might do, is to construct both f(u,y,t) and d(u,y,t) in a MATLAB Function block, where I can use a persistent variable to count how many times it is called. But this may not reflect your MATLAB System block.
Fangjun Jiang
on 29 Nov 2023
For the mean time, @Lorenzo, can you try different solver and sample time to see how many times it is called?
Lorenzo
on 29 Nov 2023
Paul
on 29 Nov 2023
According to the linked doc page in your comment:
------------------------------------
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 Signal Operations 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!