function varargout = reactivedist_c(t,varargin)

parameters_c
switch nargin
    case 2 % Optimization
        X  = t;
        U  = X((NC*NT)+NT+1:end);
        Dd = varargin{1};
    case 3 % Steady state
        X  = t;
        U  = varargin{1};
        Dd = varargin{2};
      
end

% Splitting the states
x = X(1:(NC-1)*NT);                    % Liquid compositions from btm to top
M = X((NC-1)*NT+1:NC*NT);              % Liquid hold up from btm to top
TL = X((NC*NT)+1:(NC*NT)+NT);          % Temperatures from btm to top

% Rearrange elements of composition vector (x) for later use
Iu = reshape(1:(NC-1)*NT,NT,NC-1);
x  = x(Iu);                          

% Inputs
LT = U(1);          % Reflux
VB = U(2);          % Boilup

% Disturbances
F  = Dd(1);         % Feedrate
qF = Dd(2);         % Feed liquid fraction
zF = Dd(3:end);     % Feed compositions

% The distillation model
flag = 0; % This flag signals the inclusion of equations for liquid levels. If flag~=0, Simulink is being used and no liquid level is calculated in the code.
model_c

% Output
xprime = [dxdt; dMdt; f3];
x = x(:); 
% Extra constraint which was transferred into a soft constraint:
% GL_tot - 0.0025;

c = [-D + 0; -B + 0; -LT + 0; -VB + VBmin; VB - VBmax; EP_per(1,1) - 0.002; EP_per(1,2) - 0.002; EP_per(1,3) - 0.002; EP_per(1,4) - 0.008; EP_per(1,5) - 0.0002; GL_tot - 0.0025; -EP_per(1,6) + 0.965; -CGL_per(1,5) + 0.92; TL(1) - 423.15]; 
ceq = xprime;

switch nargin
    case 2 % Optimization
        varargout = {c,ceq};
    case 3 % Steady state
        varargout = {[],xprime};
end


