필터 지우기
필터 지우기

Using for loop within a for loop in MATLAB

조회 수: 2 (최근 30일)
GAGANDEEP KAUR
GAGANDEEP KAUR 2020년 12월 9일
답변: Jan 2020년 12월 10일
I need to apply For loop within a loop. As of generally, we apply a for loop(Say j as i used) and for that value another loop( say i here) runs all the statements but here I need to reuse that first loop( j ) again within the statements. If I could get some idea, how to use it repeatedly.
%%Calculate molar volume of solvent mixture
%xm=composition of species in solution
%Vc=critical volume of species
%vm=molar volume of species
%Tr=reduced temperature of species
%T=temperature of solution mixture
%V= molar volume of micture
Vc= [121.90 55.9 155];
Zc= [0.287 0.229 0.265];
Tc= [423.85 647.1 819.15];
xm= [0.1 0.2 0.2];
T=[394.15 399.15 404.15];
V=[0 0 0];
N=length(T);
vm=[0 0 0];
v=[0 0 0];
Tr=[0 0 0];
for j=1:N
if (T(j)<=423.85)
for i=1:1
Tr(i)=T(j)/Tc(i);
vm(i)=Vc(i)*(Zc(i)^((1-Tr(i))^0.286));
v(i)=xm(i)*vm(i);
end
elseif (423.85<T(j))&&(T(j)<=647.1)
for i=1
T= 423.85; %Constant critical temperature of HI
Tr(i)=T/Tc(i);
vm(i)=Vc(i)*(Zc(i)^((1-Tr(i))^0.286));
v(i)=xm(i)*vm(i);
for i=2:3
T=T(j)
Tr(i)=T(j)/Tc(i);
vm(i)=Vc(i)*(Zc(i)^((1-Tr(i))^0.286));
v(i)=xm(i)*vm(i);
end
end
elseif (647.1<T(j))&&(T(j)<=819.15)
for i=1
T=423.85;
Tr(i)=T/Tc(i);
vm(i)=Vc(i)*(Zc(i)^((1-Tr(i))^0.286));
v(i)=xm(i)*vm(i);
for i=2
T=647.1; % critical temperature of H2O
Tr(i)=T/Tc(i);
vm(i)=Vc(i)*(Zc(i)^((1-Tr(i))^0.286));
v(i)=xm(i)*vm(i);
for i=3
T=T(j);
Tr(i)=T(j)/Tc(i);
vm(i)=Vc(i)*(Zc(i)^((1-Tr(i))^0.286));
v(i)=xm(i)*vm(i);
end
end
end
end
Tr
vm
v
V(i)=sum(v(i))/sum(xm(i));
end
disp(V)
  댓글 수: 2
Jan
Jan 2020년 12월 9일
The question is not clear to me yet. What does " reuse that first loop( j ) again within the statements" mean?
What is the purpose of loops over one value? Replace "for i=1" by "i=1;". Nested loops with the same loop counter are confusing only. It is too hard to guess, what you want the code to do.
GAGANDEEP KAUR
GAGANDEEP KAUR 2020년 12월 9일
Sorry for inconvinence. Actually I need to calculte the value of 'V' at every temperature given as input and for every temperature data have to use three set of values of other parameters(say Vc, Zc, Tc etc.) in loop.
For one temperature value code is written like this now I want to extend it to an array of temperature inputs
%%Calculate molar volume of solvent mixture
%xm=composition of species in solution
%Vc=critical volume of species
%vm=molar volume of species
%Tr=reduced temperature of species
%T=temperature of solution mixture
%V= molar volume of micture
Vc= [121.90 55.9 155];
Zc= [0.287 0.229 0.265];
Tc= [423.85 647.1 819.15];
xm= [0.1 0.2 0.2];
vm=[0 0 0];
v=[0 0 0];
Tr=[0 0 0];
T=473;..............................................................
if (T<=423.85)
for i=1:3
Tr(i)=T/Tc(i);
vm(i)=Vc(i)*(Zc(i)^((1-Tr(i))^0.286));
v(i)=xm(i)*vm(i);
end
elseif (423.85<T)&&(T<=647.1)
for i=1
T= 423.85;
Tr(i)=T/Tc(i);
vm(i)=Vc(i)*(Zc(i)^((1-Tr(i))^0.286));
v(i)=xm(i)*vm(i);
for i=2:3
Tr(i)=T/Tc(i);
vm(i)=Vc(i)*(Zc(i)^((1-Tr(i))^0.286));
v(i)=xm(i)*vm(i);
end
end
elseif (647.1<T)&&(T<=819.15)
for i=1
T=423.85;
Tr(i)=T/Tc(i);
vm(i)=Vc(i)*(Zc(i)^((1-Tr(i))^0.286));
v(i)=xm(i)*vm(i);
for i=2
T=647.1;
Tr(i)=T/Tc(i);
vm(i)=Vc(i)*(Zc(i)^((1-Tr(i))^0.286));
v(i)=xm(i)*vm(i);
for i=3
Tr(i)=T/Tc(i);
vm(i)=Vc(i)*(Zc(i)^((1-Tr(i))^0.286));
v(i)=xm(i)*vm(i);
end
end
end
end
Tr
vm
v
V=sum(v)/sum(xm);
disp(V)

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2020년 12월 10일
Cleanup the strange nested loops over scalars:
for i=1
T= 423.85;
Tr(i)=T/Tc(i);
vm(i)=Vc(i)*(Zc(i)^((1-Tr(i))^0.286));
v(i)=xm(i)*vm(i);
for i=2:3
Tr(i)=T/Tc(i);
vm(i)=Vc(i)*(Zc(i)^((1-Tr(i))^0.286));
v(i)=xm(i)*vm(i);
end
end
This is a waste of time only, because you calculate exactly the same in both loops. So simply write:
T = 423.85;
for i = 1:3
Tr(i) = T/Tc(i);
vm(i) = Vc(i)*(Zc(i)^((1-Tr(i))^0.286));
v(i) = xm(i)*vm(i);
end
Or even without a loop:
Tr = T ./ Tc;
vm = Vc .* (Zc .^ ((1 - Tr) .^ 0.286));
v = xm .* vm;
The complete shown code can be simplified to:
Tv = [min(T, 423.85), min(T, 647.1), min(T, 647.1)];
Tr = T ./ Tc;
vm = Vc .* (Zc .^ ((1 - Tr) .^ 0.286));
v = xm .* vm;
V = sum(v) / sum(xm);
disp(V)
No loops, no different branches, no "for i=1".

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by