Persistent variables counting differently than integrator
古いコメントを表示
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
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
2024 年 1 月 16 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Simulink についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!