%TEST SCRIPT
% Calculate F matrix
clear all; close all; clc;

global coldata dyndata hydraulic termodata UU alfa

% COLUMN DATA %%
%Pass the options for optimization
% Number of stages in each section (C11,C12,C21,C22,C23,C24)
coldata.Nt = [12 12 12 12 12 12];
% Initial holdup in reboiler and condenser
coldata.M0 = [1 1 1 1 1 1 1 1 1 1 1 1];
%Boiling points of ethanol, propanol and n-butanol
coldata.Tboil = [78.37 97 117.4];
%Hydraulics
hydraulic.k1 = 1/0.063;
% Relative volatility of Ethanol, Propanol and n-Butanol with respect to the
%heaviest component n-Butanol
alfa=[4.215374389 2.102194688 1];

% Optimization 
% Re-optimize for disturbances (F, zA, zB, V, and RV)
% Load output temperature at the nominal values Y0
load Y0
% All linear equality and inequality constraints given in specs, the
% Disturbances can be directly set in specs
% Initial values for optimization made global
load Nominal_final.mat  
dyndata.x_nom = x_nom;
dyndata.u_nom = u_nom;
dyndata.temp_nom = temp_nom;
options = optimset('fmincon');
options = optimset(options,'TolFun',1e-8,'TolCon',1e-8,'MaxFunEvals',500000,'Display','iter',...
'Algorithm','active-set');
Specs
dd2 = 1e-5;
%dd2=dyndata.u_nom(7)*0.2;
beq(2)=dyndata.u_nom(7)+dd2; 

%Run the optimization with the disturbance in feed compositions
[x_opt,fval,exitflag,output,lambda,Grad,Hessian] = fmincon(@(x) Objective(x),[dyndata.x_nom;dyndata.u_nom],Ain,bin,Aeq,beq,[],[],@(x) Petlyuk_nonlcon(x),options);

x_nom=x_opt(1:225);u_nom=x_opt(226:end);

dxdt = norm(Petlyuk_Nlmodel(0,x_nom, u_nom));
[~, Temp]=Petlyuk_NLmodel(0,x_nom,u_nom);
ydd2= [Temp];