function xprime=dynamic_Petlyuk(t,X) 
%This model is for simulations
%This is a dynamic model with PI controllers, It returns derivative  
% using the basic non-linear model Petlyuk_Nlmodel and it can be used for both
%steady-state simulation as well as dynamic simulations
%At steady state au=0 (except for condensers and reboiler holdups as they
%don't have steady-state effect)
%using this model simulations have been done to obtain steady state-gain
%for input and disturbances (Gy, Gyd)
%using this model simulations have been done to obtain Juu
%using this model dynamic simulations have been done for sequential
%controller tuning one by one by keeping au=0 or 1
%How? Just call the above function with different script files 
%1. for initial steady state simulation with LV configuartion the 
% script file is            : init_sim_Petlyuk.m
%2. to calculate Gy and Gyd : script gains_Petlyuk.m
%3. to calculate Juu        : script Juu_Petlyuk.m
%4. for dynamic simulation  : script file dynamic_Petlyuk.m
global UU dyndata tend NI i coldata au stages_Temperature Temp_nom
indmat_Yes_S1



% D and B used to control hold ups in the condenser (X(im_mt)) and reboiler
%  (X(im_mr))respectively

[D,e1] = Controller_PI(X(im_mt),dyndata.x_nom(im_mt),dyndata.u_nom(end-1),X(226),-1,1001,0,2);
[B,e2] = Controller_PI(X(im_mr),dyndata.x_nom(im_mr),dyndata.u_nom(end),X(227),-1,1001,0,2);


%get Temperatures for current timestep
Temp_current=get_Temperature(stages_Temperature,1,X);

%Controller
au=0;

[L,e3] = Controller_PI(Temp_current(1),Temp_nom(1),...
            dyndata.u_nom(1),X(228),-0.15*1,16,0,4);

%[L,e3] = Controller_PI(Temp_current(1),Temp_nom(1),...
            %dyndata.u_nom(1),X(228),-0.15,16,0,4);

[S1,e4] = Controller_PI(Temp_current(2),Temp_nom(2),...
            dyndata.u_nom(5),X(229),0.27*1,11.2,0,4);

%[S1,e4] = Controller_PI(Temp_current(2),Temp_nom(2),...
            %dyndata.u_nom(5),X(229),0.27,11.2,0,4);
[Rl,e5] = Controller_PI(Temp_current(3),Temp_nom(3),...
            dyndata.u_nom(3),X(230),-0.33*1,8,0,4);  % tc=1min

            %[Rl,e5] = Controller_PI(Temp_current(3),Temp_nom(3),...        
            %dyndata.u_nom(3),X(230),-0.33*1,8,0,4);  % tc=1min
VBB = dyndata.u_nom(2);              %Boilups if fixed
RV = dyndata.u_nom(4);               % RV is fixed

% loop to obtain the values of i/p and disturbances throughout simulation
% time
if(t>0.01 || rem(i,10)==0) || t==tend
%     t
UU=[UU; t [L;VBB;Rl;RV;S1;dyndata.u_nom(6:9); D; B]'];
end
i=i+1;
%     t
xprime=Petlyuk_NLmodel(t,X(1:end),[L;VBB;Rl;RV;S1;dyndata.u_nom(6:9); D; B ]);
xprime=[xprime;e1;e2;e3;e4;e5];
