Persistent variables counting differently than integrator

조회 수: 6 (최근 30일)
Nurk
Nurk 2024년 1월 15일
댓글: Nurk 2024년 1월 16일
Hello,
I am working with the MATLAB Function block in Simulink to model tank levels through persistent variables. My Code is basically looking like the following:
function y = fcn(u)
persistent total_u
if isempty(total_u)
total_u = 0;
end
total_u = total_u + u;
y = total_u;
Now I noticed some values for my MATLAB functions not adding up. The following shows a MATLAB function with the above code and comparing it to an integrator with the simulation having a stop time of 10 and a variable step size.
I basically need to achieve a result using my MATLAB functions like the integrator gives, so that each value per second is counted up once per second. I cant explain myself how the MATLAB function is counting up to 56. Im guessing it has something to do with the step size and how often Functions are called but I am hoping somebody can explain this more detailed to me.
If I switch in this little example to a fixed step size of 1 with a stop time of 10 seconds i get the following result:
This already looks better but i still cant explain myself how the MATLAB function counts to 11 even tho I only have 10 Timesteps and an initial value of total_u = 0;
I hope this explains my problem well enough. Thank you already for any answers!
  댓글 수: 2
Walter Roberson
Walter Roberson 2024년 1월 15일
Do you have 10 timesteps, or is your starting time 0 and your ending time 10 which is 11 time steps?
Nurk
Nurk 2024년 1월 16일
Thanks! I had indeed 11 time steps.

댓글을 달려면 로그인하십시오.

채택된 답변

Fangjun Jiang
Fangjun Jiang 2024년 1월 16일
편집: Fangjun Jiang 2024년 1월 16일
The MATLAB Function block is executed at every simulation step. The counting algorithm does not give a weight to the time elasped thus the output is dependent on how many times it is executed. Log the simulation time vector, also log the value of y at every time step. Compare the value at each time step or simply plot(t,y), you will see the issue clearly.
To explain the second instance where y ends at 11, it is due to the fact that the value of y, at t=0, is y=1, because at t=0, the whole model or the whole dynamic equation still needs to be held true. Thus, the function is evaluated, the value of u (Constant 1) is propagated, and it yields y=1.
To make y=0 at t=0, just make a simple change as below. The end value of y in the second instance will be 10, matching the output of the integrator.
function y = fcn(u)
persistent total_u
if isempty(total_u)
total_u = 0;
else
total_u = total_u + u;
end
y = total_u;
  댓글 수: 1
Nurk
Nurk 2024년 1월 16일
Thank you for the explanation! I fixed it now by switching to a fixed stepsize of 1 and now my persistent variables give te same count as the integrator block.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by