Save variable from a for loop

Hello everyone, I have a problem saving the variable TWR as a value, although I assigned it in the last step. Maybe someone can correct the code for me. Thanks!
R=[0.15,-0.1, 0.08, 0.02, -0.05];
%K1=10000;
%qt=19;
Lh=-0.1;
c=0;
Matrix=zeros(100,1);
for i=0.01:0.01:1
c1=c+i;
HPR=(1+c1*(-(R/Lh)));
TWR=prod(HPR(1:end,1:end));
Matrix(:,c)=TWR;
end
plot(Matrix,c1)

Respuestas (1)

Ameer Hamza
Ameer Hamza el 16 de Abr. de 2020
Editada: Ameer Hamza el 16 de Abr. de 2020

0 votos

try this
R=[0.15,-0.1, 0.08, 0.02, -0.05];
%K1=10000;
%qt=19;
Lh=-0.1;
c=1; % index must start from 1
Matrix=zeros(100,1);
for i=0.01:0.01:1
c1=c+i;
HPR=(1+c1*(-(R/Lh)));
TWR=prod(HPR(1:end,1:end));
Matrix(c)=TWR;
c = c+1; % index should increment by 1
end
plot(Matrix)

4 comentarios

Sandro Hantke
Sandro Hantke el 16 de Abr. de 2020
Thank you Ameer, now it works!
Ameer Hamza
Ameer Hamza el 16 de Abr. de 2020
Glad to be of help.
Stephen23
Stephen23 el 16 de Abr. de 2020
It is usually simpler and more robust to iterate over indices (rather than iterting over data values):
V = 0.01:0.01:1;
N = numel(V);
M = nan(1,N);
...
for k = 1:N
... do whatever with the data:
V(k)
... store data in output array:
M(k) = ...
end
Sandro Hantke
Sandro Hantke el 16 de Abr. de 2020
Thanks Stephen for your comment. I will keep that in mind!

La pregunta está cerrada.

Etiquetas

Preguntada:

el 16 de Abr. de 2020

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by