function frac=fraco(lk,xdhk,xblk,in)
% fraco		- Mass balance function simulating a distillation column
% frac=fraco(lk,xdhk,xblk,in)
% Auxiliary function that gives the fraction of compounds sent to the
%	distillate in fractions 
%   Argument    Gives
%   lk       index of light key
%   xdhk     mol fraction of heavy key in distillate
%   xblk     mol fraction of light key in bottom
%   in       index of the feed stream in ns
% Heavy key must be next to the light key
% For addtional help and picture, see PICFRACO
% Example:  >> ti=fraco(2,0.03,0.05,3);
%           >> sep(ti,3,[2 4])
% OKB, TYLC

global ns
a=[xdhk,(1-xblk);(1-xdhk),xblk]';
nf=ns(in,:);
zs=nf/(sum(nf));
zt=zs(1:lk);
zl=sum(zt);
em=a\[(xdhk*zl);(xblk*(1-zl))];
ti=zeros(size(zs));
ti(1,1:length(zt))=zt;
tem=em';
ti(1,lk:(lk+1))=[(ti(1,lk:(lk+1)))-(tem.*[1 -1])];
zzs=zs>0;
frac=zzs.*(ti./zs);
test=isnan(frac);
if sum(test)~=0
   frac(find(test))=0;
end
