function [c ,ceq ] = nlcon(x)
%NLCON Nonlinear equality and inequality constraints

% Parameter 
global p;

% Column parameters
U1=[x(p.NT+1:p.NT+6); p.qF; p.NT; p.NF; p.alpha];
% Column state variables
X1=x(1:p.NT);
% CSTR parameters
U2=[x(p.NT+5) x(p.NT+3) x(p.NT+8) p.zF0 x(p.NT) p.k1]';

% CSTR state variables
X2=x(p.NT+6:p.NT+7);

% Nonlinear inequality constraints C(x)<0
c=[];
% Nonlinear equality constraints C(x)=0
ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2)];                     % 1 unconstrained DOF

%%
%Keeping CV constant at its optimal value for case 1
load yopt_nom_case1.mat     % Optimal values of the measurements for case I

% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(p.NT+5)-(A1(1)/60)*1];           % keeping F constant
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(p.NT+1)-(A1(2)/60)*1.0];         % keeping L constant
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(p.NT+3)-(A1(3)/60)*1.0];         % keeping D constant
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(p.NT+1)-(A1(4))*1.0*x(p.NT+3)];  % keeping L/D constant
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(p.NT+1)-A1(5)*1.0*x(p.NT+5)];    % keeping L/F constant
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(p.NT+1)-A1(6)*1.0*x(p.NT+2)];    % keeping L/V constant
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(22)-A1(7)*0.8];                  % keeping xD constant
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(p.NT+5)-A1(8)*1.0*x(p.NT+8)];    % keeping F/F0 constant
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(p.NT+3)-A1(9)*1.0*x(p.NT+8)];    % keeping D/F0 constant 

%%
%Keeping CV constant at its optimal value for case 2
load yopt_nom_case2.mat     % Optimal values of the measurements for case II

% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(p.NT+5)-(A4(1)/60)*1.0];         % keeping F constant
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(p.NT+1)-(A4(2)/60)*1.0];         % keeping L constant
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(p.NT+3)-(A4(3)/60)*1.0];         % keeping D constant
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(p.NT+1)-(A4(4))*1.0*x(p.NT+3)];  % keeping L/D constant
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(p.NT+1)-(A4(5))*1.0*x(p.NT+5)];  % keeping L/F constant
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(p.NT+1)-(A4(6))*1.0*x(p.NT+2)];  % keeping L/V constant
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(22)-(A4(7))*1.0];                % keeping xD constant
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(p.NT+5)-(A4(8))*1.0*x(p.NT+8)];  % keeping F/F0 constant
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); x(p.NT+3)-(A4(9))*1.0*x(p.NT+8)];  % keeping D/F0 constant 

%% Null space method case 1
load H11.mat            % The H-matrix with the measurement combinations    
% y=[x(1);
%     x(6);
%     x(10);
%     x(14);
%     x(18);
%     x(22);    
%     x(23)*60;
%     x(24)*60;
%     x(25)*60;
%     x(26)*60;
%     x(27)*60;
%     x(30)*60];        % Measurement vector y
%
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); H11*y-0.0272];


%% Null space method case 2

load H2.mat             % The H-matrix with the measurement combinations
% y=[x(1);
%     x(6);
%     x(10);
%     x(14);
%     x(18);
%     x(22);    
%     x(23)*60;
%     x(24)*60;
%     x(25)*60;
%     x(26)*60;
%     x(27)*60;
%     x(30)*60];        % Measurement vector y
% 
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); H2*y-0.8315];

%% Exact local method
% Case 1
load Hel.mat            % The H-matrix with the measurement combinations

% y=[x(1);
%     x(6);
%     x(10);
%     x(14);
%     x(18);
%     x(22);    
%     x(23)*60;
%     x(24)*60;
%     x(25)*60;
%     x(26)*60;
%     x(27)*60;
%     x(30)*60];        % Measurement vector y
% 
% 
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); Hel*y+0.0011];

% With p=0.01 instead of p=0.02
% load Heln.mat        
% y=[x(1);
%     x(6);
%     x(10);
%     x(14);
%     x(18);
%     x(22);    
%     x(23)*60;
%     x(24)*60;
%     x(25)*60;
%     x(26)*60;
%     x(27)*60;
%     x(30)*60];        % Measurement vector y
% 
% 
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); Heln*y-0.0046];

% Case 2
%
load Hel2.mat           % The H-matrix with the measurement combinations
% 
% y=[x(1);
%     x(6);
%     x(10);
%     x(14);
%     x(18);
%     x(22);    
%     x(23)*60;
%     x(24)*60;
%     x(25)*60;
%     x(26)*60;
%     x(27)*60;
%     x(30)*60];        % Measurement vector y
% 
% 
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); Hel2*y+0.0030];

%%

%For OPTI toolbox
if (nargout ==1&&p.OPTI==1)
    c=[c;ceq];
end

