mercredi 27 janvier 2021

Execute for loop with if condition in MATLAB

Actually I am getting problem in re-entering the for loop. Need to enter p loop where ym is to calculated again with new set of parameters calculated and repeat the all code below until condition is not satisfied. After the condition get satisfied, then it should enter O loop(of ym) and same calculations to be done. If I could get some solution as it is not giving desired results Probably it is not re-entering the same loop to calculate ym again but starts executing outer loop after only one cycle.

     %%To calculate system pressure and vapor phase composition
e=0.10
f_sat=  [ 32.2587857596876  34.0501961493468 35.8746854026426
          2.00528670528962   2.33558884889119 2.70783015514365
         0.157445079883318   0.187083254240374 0.221158510598425];
Gamma_t =[3.19702443124609  3.31850686110491  3.44415347633114
          2.54070700562388  2.66546741032482  2.79709939502664
          2.08314683871606  2.20838701999553  2.34012176373733 ]; 
      Vm= [68.0045214121783  70.0736822139801  72.5465660407398
          18.1158800129555  18.2324752448415  18.3515283669796
          51.5543766322469  51.7464896478157  51.9409677300663];
Pc= [831;220.58; 116.54];
Tc=[394.15 ;399.15 ;404.15];
xm=[0.09630 0.0964 0.0965;
    0.5096 0.5092 0.5087;
    0.3941 0.3944 0.3948];
T=[394.15 399.15 404.15 ];
N=length(T);
M=length(xm);
Phi(1:3,1:3)=1;
 R=0.08314;
ym=zeros(N,M);P_0=zeros(N,1);P_calI=zeros(N,M);
a0=zeros(1,M);aHI=zeros(1,N);aH2O=zeros(1,N);aI2=zeros(1,N);
 a=zeros(N,1);b=zeros(N,1);A=zeros(N,1); B=zeros(N,1);
for   o=1:N
    for p=1:M   
     P_calI(p,o)= (xm(p,o)*Gamma_t(p,o)*f_sat(p,o))/Phi(p,o)
    end 
    P_0(o)=sum(P_calI(:,o))
end

for o=1:N
   for p=1
   ym(p,o)= (xm(p,o)*Gamma_t(p,o)*f_sat(p,o))/(Phi(p,o)*P_0(o))
  ym(p+1,o)= (xm(p+1,o)*Gamma_t(p+1,o)*f_sat(p+1,o))/(Phi(p+1,o)*P_0(o))
   ym(p+2,o)=(xm(p+2,o)*Gamma_t(p+2,o)*f_sat(p+2,o))/(Phi(p+2,o)*P_0(o)) 
  a0(p)=(0.42748*(((R^2)*(Tc(p)^2.5))/Pc(p)));
  a0(p+1)=(0.42748*(((R^2)*(Tc(p+1)^2.5))/Pc(p+1)));
  a0(p+2)=(0.42748*(((R^2)*(Tc(p+2)^2.5))/Pc(p+2)));
    b(p,o)=(0.08664*((R*Tc(p))/Pc(p)))*(ym(p,o))+(0.08664* 
 ((R*Tc(p+1))/Pc(p+1)))*(ym(p+1,o))+(0.08664*((R*Tc(p+2))/Pc(p+2)))*(ym(p+2,o));
     A(p,o)=(((0.4278*Tc(p)^2.5)/(Pc(p)*T(o)^2.5))^(1/2))*(ym(p,o))+(((0.4278*Tc(p+1)^2.5)/(Pc(p+1)*T(o)^2.5))^(1/2))*(ym(p+1,o))+(((0.4278*Tc(p+2)^2.5)/(Pc(p+2)*T(o)^2.5))^(1/2))*(ym(p+2,o));
     B(p,o)=((0.08664*Tc(p))/(Pc(p)*T(o))*(ym(p,o)))+((0.08664*Tc(p+1))/(Pc(p+1)*T(o))*(ym(p+1,o)))+((0.08664*Tc(p+2))/(Pc(p+2)*T(o))*(ym(p+2,o)));
      Asq=((A).^2)
         for i=1
    aHI(o)=(((a0(i)*a0(i+1))^0.5)*ym(i,o)*ym(i+1,o))+(((a0(i)*a0(i+2))^0.5)*ym(i,o)*ym(i+2,o));  %applying mixing rules
    aH2O(o)=(((a0(i+1)*a0(i))^0.5)*ym(i+1,o)*ym(i,o))+(((a0(i+1)*a0(i+2))^0.5)*ym(i+1,o)*ym(i+2,o));
    aI2(o)=(((a0(i+2)*a0(i))^0.5)*ym(i+2,o)*ym(i,o))+(((a0(i+2)*a0(i+1))^0.5)*ym(i+2,o)*ym(i+1,o));
         end
     format long g
        a(o)=(aHI(o)+aH2O(o)+aI2(o));
         a(o)=(a(o))';
         Phi=zeros(N,M);Z=zeros(N,M);h=zeros(N,M); P_1=zeros(N,1);P_cal=zeros(N,M);
        for i=1:M 
        h(i,o)=b(o)./Vm(i,o);
        Z(i,o)=(1./1-h(i,o))-((Asq(o)/B(o)).*(h(i,o)./(1+h(i,o))));
        Phi(i,o)=exp(Z(i,o)-1-log(Z(i,o)-(B(o).*P_calI(i,o)))-(Asq(o)/B(o)).*(log(1+((B(o).*P_calI(i,o))./Z(i,o)))));
        P_cal(i,o)= (xm(i,o)*Gamma_t(i,o)*f_sat(i,o))/Phi(i,o)
        end
        P_1(o)=sum(P_cal(:,o))
      if ((P_1(o)-P_0(o)/P_0(o))<e)
       break
      end
          P_0=P_1
        P_calI=P_cal
       end   
     end

Aucun commentaire:

Enregistrer un commentaire