% Script for calculating gain matrix GY (w.r.t. input) 
clear all; close all; clc;
global coldata dyndata hydraulic termodata UU alfa tend NI i
%% COLUMN DATA %%

% Number of stages in 6 sections of Petlyuk 
coldata.Nt = [12 12 12 12 12 12];
% Initial holdup stages
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];
termodata.P = 1.0;
hydraulic.k1 = 1/0.063;
%% THERMODYNAMIC DATA %%
alfa=[4.215374389	2.102194688	1] %ethanol propanol butanol

%% load nominal x u
load Nominal_final.mat  % The nominal value obtained from optimization for ...
                        %minimizing the sum of impurities in the product streams (D, S and R)
UU=[];                       
i=0;                    %to initiate Petlyuk_openloop make change in CS by i+1
indmat_Yes_S1
% Initial values made global
dyndata.x_nom = x_nom; 
u_nom(4)= 0.9*u_nom(4); %Rv
u_nom(5)= 0.3342; % S
u_nom(3)= 0.3233; % Rl
u_nom(1)= 1.3988; % L
dyndata.u_nom=u_nom;%           
dyndata.temp_nom=temp_nom;
%% Run this file to get initial values with LV Configuartion, uncomment
% line number 
NI=5; % Number of integerators
options = odeset('MaxStep',10);
tend=20000;
[t,x]=ode15s(@Simulation_Petlyuk,[0 tend],[dyndata.x_nom;zeros(NI,1)],options);
%% xf states to plot profiles.
xf=x(end,1:225)';
u_nom=UU(end,2:12)';
dxdt = norm(Petlyuk_NLmodel(0, xf, u_nom));
