How to achieve i+1 indexing in simulink
2 次查看(过去 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
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
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Sources 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!