Using for loop within a for loop in MATLAB

1 view (last 30 days)
GAGANDEEP KAUR
GAGANDEEP KAUR on 9 Dec 2020
Answered: Jan on 10 Dec 2020
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 Comments
Jan
Jan on 9 Dec 2020
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 on 9 Dec 2020
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)

Sign in to comment.

Answers (1)

Jan
Jan on 10 Dec 2020
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".

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!