function xpr = CSTR_SS_model(t,X,U)
%
% CSTRmod - This is a nonlinear model of a CSTR with two feeds and one
%           product.
%           Model assumptions: Two components. 
%           The model has two states (the composition of A and holdup)
%
% Inputs:   t     - time in [min]
%           X     - state, the first state is the composition of light component A (X(1))
%                   and the second state is the holdup in the reactor (X(2)).
%           U(1)  - product rate F
%           U(2)  - recycle/distillate D
%           U(3)  - feed rate F0
%           U(4)  - feed composition, zF0
%           U(5)  - recycle composition, xD.
%
% Output:   xprime - vector with time derivative of the two states

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Splitting the states
zFA=X(1);                  % Liquid hold up of A in reactor
Mr=X(2);                   % Liquid hold up in reactor

MrA=zFA*Mr;

% Inputs and disturbances
F   = U(1);                 % Product rate
D   = U(2);                 % Recycle/distillate

F0  = U(3);                 % Feed rate
zF0 = U(4);                 % Feed composition
xD  = U(5);                 % Recycle composition
k1  = U(6);                 % Reaction rate constant

% The model
% Dynamic component balances
dMrAdt = F0*zF0+D*xD-F*zFA-k1*MrA;
dMrdt  = F0+D-F;

xpr = [dMrAdt; dMrdt];

end
