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!