Filter löschen
Filter löschen

How to achieve i+1 indexing in simulink

4 Ansichten (letzte 30 Tage)
Hui Li
Hui Li am 6 Sep. 2023
Beantwortet: Kshittiz am 29 Sep. 2023
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 Kommentar
Daniel Bengtson
Daniel Bengtson am 6 Sep. 2023
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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Kshittiz
Kshittiz am 29 Sep. 2023
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

Kategorien

Mehr zu Simulink Functions finden Sie in Help Center und File Exchange

Produkte


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by