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

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

%%
%Keeping CV constant at its optimal value for case 2
load yopt_nom_case2.mat

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

%%
%Null space method case 1
load H.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];
% % np=p.cs;
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); H*y-0.2655];

%%
%Null space method case 2

% Two disturbances
load H2.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];
% % np=p.cs;
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); H2*y-0.8315];

% One disturbance
% load H3.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];
% % np=p.cs;
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); H3*y-0.1602];

%%
%Null space method case 1 Simulink
load Hns.mat
% y=[x(1);
%     x(6);
%     x(10);
%     x(14);
%     x(18);
%     x(22);    
%     x(23);
%     x(24);
%     x(25);
%     x(26);
%     x(27);
%     x(30)];
% 
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); Hns*y-1.5372];

%%
% Exact local method
% Case 1
%
load Hel.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];
% 
% 
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); Hel*y-22.8872];

% Case 2
%
load Hel2.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];
% 
% 
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); Hel2*y-1081.3];

% Simulink
%
load HelS.mat
% 
% y=[x(1);
%     x(6);
%     x(10);
%     x(14);
%     x(18);
%     x(22);    
%     x(23);
%     x(24);
%     x(25);
%     x(26);
%     x(27);
%     x(30)];
% % 
% % 
% ceq=[colamodSS(X1,U1); CSTR_SS_model(0,X2,U2); HelS*y-177410];


%%

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

