Borrar filtros
Borrar filtros

How to achieve i+1 indexing in simulink

4 visualizaciones (últimos 30 días)
Hui Li
Hui Li el 6 de Sept. de 2023
Respondida: Kshittiz el 29 de Sept. de 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 comentario
Daniel Bengtson
Daniel Bengtson el 6 de Sept. de 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

Iniciar sesión para comentar.

Respuestas (1)

Kshittiz
Kshittiz el 29 de Sept. de 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

Categorías

Más información sobre Simulink Functions en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by