function flash(t,p,zs,erc,nmax)
% flash		- Performs a flash calculation with a table of results
% function flash(t,p,zs,erc,nmax)
% Argument List
% Argument   Gives
%    t      temperature in Tdeg.
%    p      pressure in kPa.
%    zs     vector of feed mol fractions.
%    erc    gives fzero the difference between successive values of x
%           allowed when convergence is deemed to be achieved.
%    nmax   is the maximum number of modified retrys by fzero before quitting.
%  If erc and nmax are not given, then the program uses:
%     erc=.0001 and nmax=10
%
% Example:% >> start301   to specify benzene, toluene, o-xylene 
%         >> flash(375,101,[.4 .4 .2])
%  will produce a table giving the flash V/F ratio
%  as well as the liquid and vapor compositions.
global cnms Tdeg
if nargin<=3
   erc=.000001;
   nmax=10;
end
szs=sum(zs);
if abs(szs-1)>.5*erc
   disp('The zs do not sum to 1, normalizing by dividing by the sum.')
   zs=zs/szs;
end
ks=kval(t,p);
omks=1-ks;
P1=[zs;omks];
fx='sum(c(1,:).*c(2,:)./(1-x.*c(2,:)))';
x=0;
c=P1;
f1=eval(fx);
x=1;
f2=eval(fx);
if f1*f2>=0
   disp('There is no V/F for this flash.')
   return
end
vl=0;
vr=1;
fl=f1;
fr=f2;
nmax1=10;
for k=1:nmax
    vof=ssec2([vl vr],fx,c,erc,nmax1);
 %vof=fzero(fx,[vl vr],erc,[],P1);
 xs=zs./(1-vof.*P1(2,:));
 ys=xs.*ks;
 if abs(sum(xs)-1)+abs(sum(ys)-1)<=erc
  fprintf(['Temperature= %6.2f',Tdeg,' Pressure= %6.2f kPa V/F=%6.4f\n'],t,p,vof)
  disp('Compound      Feed      Liquid    Vapor     K=y/x')
  m=[zs;xs;ys;ks]';
  cm=charmx(m,10,5);
  for k=1:length(zs)
    disp([cnms(k,:),cm(k,:)])
  end
  break
 else
  x=(vl+vr)/2;
  fm=eval(fx);
  if fm*fl>0
    vl=x;
  else
    vr=x;
  end
 end
end
