function [sys,x0]=gensec(T,X,u,flag,Nt,alphas,M0,yselect)
%
%Generalized distillation column section model
%assumptions	- no liquid or vapour dynamics, constant relative volatility, 
%total condenser
%Input:
%		T				- time
%		X				- (nc-1)compositions for Nt trays
%		u(1)			- Liquid feed flow
% 		u(2:nc)		- Compositions for components 1 - (nc-1) in liq. feed
%		u(nc+1)		- Vapour feed flow
%		u(nc+2:2*nc)	- Compositions for components 1 - (nc-1) in vap. feed
%		Nt				- number of trays
% 		alphas		- relative volatilities for the nc components relateted to heaviest comp.
%		M0				- Tray holdup
x0=[];
nc=length(alphas);
L=u(1);
V=u(nc+1);
cx=reshape(X,Nt,(nc-1));

if (flag==1)					%state derivatives xdot=f(x,u)
   x=u(2:nc)';
   y=u(nc+2:2*nc)';
   %computing vapour cmpositions cy using routine 'equilib.m'
   cy=equilib(cx,alphas);
   %
   %computing composition derivatives
   %
   xdot=zeros(Nt,nc-1);
   %Top tray
   xdot(1,:)=(L*(x-cx(1,:))+V*(cy(2,:)-cy(1,:)))/M0;
   %Middle tray
   II=[2:Nt-1];
   xdot(II,:)=(L*(cx(II-1,:)-cx(II,:))+V*(cy(II+1,:)-cy(II,:)))/M0;
   %Bottom tray
   xdot(Nt,:)=(L*(cx(Nt-1,:)-cx(Nt,:))+V*(y-cy(Nt,:)))/M0;
   sys=xdot(:);
elseif (flag==3)			%measurements y=f(x,u)
   xut=cx(Nt,:);
   yut=equilib(cx(1,:),alphas);
   if (isempty(yselect)|yselect==0)
      sys=[L,xut,V,yut]';
   elseif (yselect==1)
      sys=[[L,xut,V,yut]';x]
   end
end

     
      
   



	
   

   
