How to achieve i+1 indexing in simulink

4 次查看(过去 30 天)
I'm very confused on how to achieve the (i+1) indexing in Simulink. Below is the matlab script that I want to implement. Any help is appreciated!
n_iter = 5;
Z = [1 1 0 0 0];
T1 = ones(n_iter, 1).*300;
T2 = ones(n_iter, 1).*300;
S = 5;
W = 5;
for i = 1:(n_iter-1)
S = S * 10;
if Z(i+1) == 0
S = S * 2;
else
S = S;
end
W = W * 5;
if Z(i+1) == 0
W = W * 5.5;
else
W = W;
end
T1(i+1) = T1(i) + S;
T2(i+1) = T2(i) + W + T1(i+1);
end
  1 个评论
Daniel Bengtson
Daniel Bengtson 2023-9-6
Hi Hui,
Could you just shift your indexing such that you use i-1 and i instead? Its much more trivial to add a delay on a variable in simulink than read a future value.
n_iter = 5;
Z = [1 1 0 0 0];
T1 = ones(n_iter, 1).*300;
T2 = ones(n_iter, 1).*300;
S = 5;
W = 5;
for i = 2:(n_iter)
S = S * 10;
if Z(i) == 0
S = S * 2;
else
S = S;
end
W = W * 5;
if Z(i) == 0
W = W * 5.5;
else
W = W;
end
T1(i) = T1(i-1) + S;
T2(i) = T2(i-1) + W + T1(i);
end

请先登录,再进行评论。

回答(1 个)

Kshittiz
Kshittiz 2023-9-29
Hi Hui,
I understand you are facing issues while trying to achieve the ‘i+1’ indexing from your MATLAB script in Simulink.
To achieve the (i+1) indexing in Simulink, you can use the ‘memory’ block to store the previous values of variables.
To learn more about the ‘memory’ block, refer the following documentation: https://in.mathworks.com/help/simulink/slref/memory.html
I hope this will help you in fixing the issue.
Regards,
Kshittiz

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by