function [xprime D1 B1 L1 V1 xD1 xB1 e1 D2 B2 L2 V2 xD2 xB2 e2] = twocol_dyn(varargin)

%TWOCOL_DYN calculates derivatives for two distillation columns in series
%using the multicomA model, assuming feed to second column is bottom
%product from the first.

switch nargin
    case 3 % Steady state
        t = [];
        x  = varargin{1} ;
        u  = varargin{2} ;
        d = varargin{3} ;
    case 4 % Dynamic
        t  = varargin{1} ;
        x  = varargin{2};
        u  = varargin{3};
        d = varargin{4};
end

global config number

% x consists of all states of the multicomA model (plus integrated error in
% composition controller if we use composition control)

switch config
    case('XdXb')
        x1 = x(1:125)   ;
        x2 = x(126:250) ;
    case('LV')
        x1 = x(1:123)   ;
        x2 = x(124:246) ;
end

number = 1 ; % First column
[xprime_1 B1 D1 L1 V1 xB1 xD1 e1] = multicomA(t,x1,u(1:2),d) ;

d2 = [B1 1 xB1] ;
number = 2 ; % Second column
[xprime_2 B2 D2 L2 V2 xB2 xD2 e2] = multicomA(t,x2,u(3:4),d2) ;

xprime = [xprime_1 ; xprime_2] ;

xB1 = xB1(:) ;
xD1 = xD1(:) ;
xB2 = xB2(:) ;
xD2 = xD2(:) ;


