MATLAB: Operands to the || and && operators must be convertible to logical scalar values.

operandsoperators must be logical scalars

I am trying to run a file with given input. the function scripts and
the main files to be run are enclosed belo.I keep geeting errors that says"Operands to the and && operators must be convertible to logical scalar values"
function [gamma,a]=ACTIVITY_WILSON(MW,rhoL,BIP,T,Z)
%This program calculates the activity coefficients (gamma) and the
%activities (a) of each component of a mixture of c components using the
%Wilson model.
%INPUT PARAMETERS: MW: vector (1xc) reporting the molecular weights of the
%c components; rhoL, vector (1xc) reporting the liquid density of the c
%pure components at temperature T; BIP is a matrix cxc reporting the energy
%interaction parameters (BIP(i,j)=lambda_ij-lambda_ii, J/mol). The energy
%interaction parameters are temperature independent; T: temperature of the
%system; x vector (1xc) reporting the mole fractions of the components of
%the mixture.
%OUTPUT PARAMETERS: gamma: vector 1xc reporting the activity
%coefficients of the components of the mixture; a: vector 1xc reporting the
%activities of the components of the mixture.
%Unless otherwise stated, all input/output parameters are expressed
%according to MKS.
R=8.314;
c=length(Z);
%Molar volumes of the pure liquid components composing the mixture
Vl=1./((rhoL*1000)./MW);
%Lambda terms (dimensionless) of the Wilson formula
for i=1:c
for j=1:c
Lambda(i,j)=(Vl(j)/Vl(i))*exp(-BIP(i,j)/(R*T));
end
end
for i=1:c
for j=1:c
A=sum(Z.*Lambda(j,:));
C(j)=Z(j)*Lambda(j,i)/A;
end
lngamma(i)=1-log(sum(Z.*Lambda(i,:)))-sum(C);
gamma(i)=exp(lngamma(i));
a(i)=gamma(i)*Z(i);
end
end
%%





function [PD,xDew]=PD_VLE_wilson (C,MW,rhoL,BIP,T,Z)
% this is a function that calculates the dewpoint of a mixture with the
% wilson correlation for generating activity coefficients for non ideal
%liquid phase
c=length(Z);
for i=1:c
Ps=exp((C(i,1))+(C(i,2)/T)+(C(i,3)*log(T))+(C(i,4)*T^C(i,5)));
end
% initial guess of Pdew using PD_VLE_ID(C,T,y)
[PD,xDew]=PDew_Raoult(C,Ps,Z,T); %initial guess of PD(1) and calculation of x1
[gamma,~]=ACTIVITY_WILSON(MW,rhoL,BIP,T,Z);% calculation of gamma at the initial guess of x1
iter=0;
while max(abs((PD*Z-Ps.*Z.*gamma)./(PD*Z)))>0.001 &&iter<1000 %isofugacity conditions and control on the number of iterations
PD=1/sum(Z./Ps.*gamma);
xDew=(PD*Z)./(Ps.*gamma);
[gamma,~]=ACTIVITY_WILSON(MW,rhoL,BIP,T,Z);% initial guess of gamma
iter =iter +1;
end
if iter<1000
PD=1/sum(Z./Ps.*gamma);
xDew=(PD*Z)./(Ps.*gamma);
else
PD=0;
xDew=0;
disp("bubble point not found");
end
end
%%
function[PD,xDew]=PDew_Raoult(C,Ps,y,T)
% compute dew pressure at defined T for a mixture with a user given vapour
% pressure using the Raoult Law
c=length(y);
for i=1:c
Ps=exp((C(i,1))+(C(i,2)/T)+(C(i,3).*log(T))+(C(i,4).*T.^C(i,5)));
PD=1/sum(y./Ps);
xDew=y*PD./Ps;
end
%%
function[PB,y]=Pbubble_Raoult(C,Ps,Z,T)
%this is a function that calculates the bubble point of a fluid with given
%vapour pressure and composition
i=length(Z);
Ps=exp((C(i,1))+(C(i,2)/T)+(C(i,3).*log(T))+(C(i,4).*T.^C(i,5)));
PB=Ps.*Z;
y=Ps.*Z/PB;
end
%%
function[PB,y]=PB_VLE_Wilson(Z,C,MW,rhoL,T,BIP)
% this is a function that calculates the bubble point using the wilson
% activity coeefficient model.
c=length(Z);
for i=1:c
Ps(i)=exp(C(i,1)+C(i,2)/T+C(i,3).*log(T)+C(i,4).*(T).^(C(i,5)));
[gamma,~]=ACTIVITY_WILSON(MW,rhoL,BIP,T,Z);
PB=sum(Ps(i).*Z.*gamma);
y=Ps(i).*gamma/PB;
disp('PB VLE Wilson')
disp(PB)
disp(y)
end
%%
function [ alphaV ] = RACHFORDRICE_BISECT( K,Z )
%This function applies the bisection method to the Rachford-Rice equation
%for alphaV in (0,1).
%Input/output are on molar basis.
a=0;
b=1;
%psi_0=sum(z.*(K-1))
%psi_1=sum((z.*(K-1))./K)
while b-a>0.000001
alphaV=(a+b)/2;
psi_alphaV=sum((Z.*(K-1))./((1-alphaV)+alphaV*K));
psi_b=sum((Z.*(K-1))./((1-b)+b*K));
if psi_alphaV*psi_b<0
a=alphaV;
else
b=alphaV;
end
end
end
%%
function[Xeq, Yeq, alphaV, Fl, Fv]=PTFLASH_VLE_Wilson(C,MW,rhoL,BIP,P,T,Z)
%this is a function that calculates PT flash for non -ideal systems where K
%cannot be explicit since it depends on X which is also a variable
x=length(Z);
for i=1:x
Ps(i)=exp(C(i,1)+C(i,2)/T+C(i,3).*log(T)+C(i,4).*(T).^(C(i,5)));
end
% validating the possibility of having VLE
[PB,y]=PB_VLE_Wilson(Z,C,MW,rhoL,T,BIP);
[PD,x]=PD_VLE_wilson (C,MW,rhoL,BIP,T,Z);
% checking if there is VLE
if P<=PD %% y here represents the mole fraction of componenets
Xeq=0;
Yeq=y;
alphaV=1;
Fl=0;
Fv=P*Z;
elseif P>=PB %% x here represents the mole fraction of components
Xeq=x;
Yeq=0;
alphaV=0;
[gamma,a]=ACTIVITY_WILSON(MW,rhoL,BIP,T,Xeq);
Fl=Ps.*Xeq.*gamma;
Fv=0;
else
Xeq=(x+y)/2 ; % first guess on x
[gamma,a]=ACTIVITY_WILSON(MW,rhoL,BIP,T,Xeq); %first guess on gamma
K=(Ps.*gamma)/P; % first guess on K
Psi_0=sum(Z.*(K-1));
Psi_1=sum(Z.*(K-1)./K);
if Psi_0*Psi_1>=0 % bad initial guess
Xeq=0;
Yeq=0;
alphaV=0;
Fl=0;
Fv=0;
disp("bad initial guess")
else %supposedly good initial guess
Fl=zeros(1,x); % trick to enter the while cycle at the begininng
Fv=Fl+1;
iter=0; % initialization of iteration count
while max(abs((Fv-Fl)./Fv))>0.00001 && iter<10000 && Psi_0*Psi_1<0
[ alphaV ] = RACHFORDRICE_BISECT( K,Z );
xeq=Z./((1-alphaV)+alphaV*K);
Yeq=K.*xeq;
[gamma,a]=ACTIVITY_WILSON(MW,rhoL,BIP,T,Z); %Gamma new guess
K=(Ps.*gamma)/P;
psi_0=sum(Z.*(K-1));
psi_1=sum(Z.*(K-1)./K);
Fv=P*Yeq;
Fl=Ps.Xeq.*gamma;
iter=iter+1;
end
end
end
end
%% input
T=343;
z=[0.1 0.9];
P=4000000;
MW=[60 100];
rhoL=[803 802];
BIP=[1972.2 3700.10;1972.2 3700.10];
C1=[88.134 153.23];
C2=[-8489.6 -155];
C3=[-9.0766 -19.848];
C4=[8.3303E-18 1.6426E-05];
C5=[6 2];
C = [C1' C2' C3' C4' C5'];
[Xeq, Yeq, alphaV, Fl, Fv]=PTFLASH_VLE_Wilson(C,MW,rhoL,BIP,P,T,Z)

Best Answer

  • Well when you get an error you do like this:
    >> dbstop if error
    Then you rerun your code, get a debug-break at the line where the error occurs, then you can check the offending
    call, size of the input parameters and variable and what this and that is. In this case one of the arguments to && will not be a scalar but some sort of array. That might be because the code you got wa written expecting some other size of the inputs, arrays sent in with different row-column arrangements or the like. Perhaps you have the newest version of matlab
    that does the implicit expansion then adding or subtracting a column array with a row-array - but now I'm speculating.
    HTH