Borrar filtros
Borrar filtros

How I could solve the for loop problem?

1 visualización (últimos 30 días)
M.Rameswari Sudha
M.Rameswari Sudha el 12 de Jul. de 2022
Respondida: M.Rameswari Sudha el 12 de Jul. de 2022
I couldn't solve the for loop program. I couldn't find where i make mistake. I couldn't get the answer for TC, TCC
clc
close all
k=50;
c=20;
h=2;
H=2;
w0=20;
w1=0.1;
c0=1;
lambdai = [220 250 280 310 400];
b=[50 70 100 120 130];
M = zeros(length(lambdai));
for i=1:M
C1(i) = lambdai;
rho1(i)= (1-(lambdai(i)./mu));
B1(i)=((b(i).*lambdai(i))./lambda);
q1(i)=sqrt((2.*k.*lambdai(i).*(h+c0.*w1.*the2+b(i)))./((H+c0.*w1.*the2).*rho(i).*b(i)));
v1(i)=-((H+c0.*w1.*the2).*rho(i).*q(i))./((H+c0.*w1.*the2+b(i)));
i=i+1;
end
C
rho
B1
q1
v1
N = zeros(length(q1),length(rho),length(v1));
for j=1:N
TCC(i)=c.*lambdai(i)+((k.*lambdai(i))./q1(i))+((H.*(rho(i).*q1(i)+v1(i)).^2)./(2.*rho(i).*q1(i)))+((b(i).v1(i).^2)./(2.*rho(i).*q1(i)))+c0.*(w0+w1.*((rho(i).*q1(i)+v1(i)).^2)./(2.*rho(i).*q1(i))).* the2
TC(i)=c.*lambdai(i)+c0.*w0.*the2+ sqrt((2.*k.*lambdai(i).*b(i)).*rho(i).*(H+c0.*w1.*the2)./((H+c0.*w1.*the2.*b(i))))
j=j+1;
end
TCC
TC
C1 = sum(C(:));
Q=sum(q(:));
rhoo=sum(rho(:));
V=sum(v(:));
B=sum(B1(:));
TCP= c.*lambda+((k.*lambda)./Q) + (H+c0.*w1.*the2).*((rhoo.*Q+V).^2)./(2.*rhoo.*Q)+((B.*V.^2)./(2.*rhoo.*Q))+c0.*w0.*the2
TCPP=c.*lambda+c0.*w0.*the2+sqrt((2.*k.*lambda.*B.*rhoo.*(H+c0.*w1.*the2))./(H+B+c0.*w1.*the2))
Z=TCPP-TCC
R=(Z./TCC).*100
  4 comentarios
KSSV
KSSV el 12 de Jul. de 2022
It is because:
M = zeros(length(lambdai));
for i=1:M
What does it mean? M is a matrix, you have used it in loop indexing. So, this loop does not get executed.
M.Rameswari Sudha
M.Rameswari Sudha el 12 de Jul. de 2022
I struggle with get the answer for TC and TCC. Eventhough, I change M to another variable, I didn't get the answer.

Iniciar sesión para comentar.

Respuesta aceptada

Mathieu NOE
Mathieu NOE el 12 de Jul. de 2022
hello
tried to fix quite a large amount of bugs
still don't understand why you need a second for loop as obviously it could be done in one single for loop
clc
clearvars
close all
k=50;
c=20;
h=2;
H=2;
w0=20;
w1=0.1;
c0=1;
lambdai = [220 250 280 310 400];
b=[50 70 100 120 130];
M = zeros(length(lambdai));
% missing init variables
mu = 1000 ; % check this !!
lambda = 1000 ; % check this !!
the2 = 1000 ; % check this !!
% for j=1:M % NO ! M is array of zeroes ! this blocks the for loop operation
for i=1:numel(lambdai) % <= here
C1(i) = lambdai(i); % <= here
rho1(i)= (1-(lambdai(i)./mu));
B1(i)=((b(i).*lambdai(i))./lambda);
q1(i)=sqrt((2.*k.*lambdai(i).*(h+c0.*w1.*the2+b(i)))./((H+c0.*w1.*the2).*rho1(i).*b(i))); % rho or rho1 (computed above) ??
v1(i)=-((H+c0.*w1.*the2).*rho1(i).*q1(i))./((H+c0.*w1.*the2+b(i))); % q or q1 (computed above) ??
% i=i+1; % not needed
end
% C % C or C1 (computed above) ??
% rho % rho or rho1 (computed above) ??
C1 % OK
rho1 % OK
q1 % OK
v1 % OK
% N = zeros(length(q1),length(rho),length(v1));% rho or rho1 (computed above) ??
%for j=1:N % ??
for i=1:numel(lambdai) % <= here
% TCC(i)=c.*lambdai(i)+((k.*lambdai(i))./q1(i))+((H.*(rho(i).*q1(i)+v1(i)).^2)./(2.*rho(i).*q1(i)))+((b(i).v1(i).^2)./(2.*rho(i).*q1(i)))+c0.*(w0+w1.*((rho(i).*q1(i)+v1(i)).^2)./(2.*rho(i).*q1(i))).* the2;
TCC(i)=c.*lambdai(i)+((k.*lambdai(i))./q1(i))+...
((H.*(rho1(i).*q1(i)+v1(i)).^2)./(2.*rho1(i).*q1(i)))+...
((b(i).*v1(i).^2)./(2.*rho1(i).*q1(i)))+... % missing product sign : (b(i).v1(i).^2) => (b(i).*v1(i).^2)
c0.*(w0+w1.*((rho1(i).*q1(i)+v1(i)).^2)./(2.*rho1(i).*q1(i))).* the2; % replaced rho by rho1
TC(i)=c.*lambdai(i)+c0.*w0.*the2+...
sqrt((2.*k.*lambdai(i).*b(i)).*rho1(i).*(H+c0.*w1.*the2)./((H+c0.*w1.*the2.*b(i))));% replaced rho by rho1
% j=j+1; % not needed
end
TCC
TC
return
% C1 = sum(C(:)); % C or C1 (computed above) ??
C1 = sum(C1(:));
% Q=sum(q(:)); % q or q1 (computed above) ??
Q=sum(q1(:)); % q or q1 (computed above) ??
% rhoo=sum(rho(:)); % rho or rho1 (computed above) ??
% V=sum(v(:)); % v or v1 (computed above) ??
rhoo=sum(rho1(:));
V=sum(v1(:));
B=sum(B1(:));
TCP= c.*lambda+((k.*lambda)./Q) + (H+c0.*w1.*the2).*((rhoo.*Q+V).^2)./(2.*rhoo.*Q)+((B.*V.^2)./(2.*rhoo.*Q))+c0.*w0.*the2
TCPP=c.*lambda+c0.*w0.*the2+sqrt((2.*k.*lambda.*B.*rhoo.*(H+c0.*w1.*the2))./(H+B+c0.*w1.*the2))
Z=TCPP-TCC
R=(Z./TCC).*100
  1 comentario
M.Rameswari Sudha
M.Rameswari Sudha el 12 de Jul. de 2022
I got the answer. I was able to find the error where I left off. Thank you

Iniciar sesión para comentar.

Más respuestas (1)

M.Rameswari Sudha
M.Rameswari Sudha el 12 de Jul. de 2022
"still don't understand why you need a second for loop as obviously it could be done in one single for loop " - I will try to bring in a single loop.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2009b

Community Treasure Hunt

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

Start Hunting!

Translated by