clc;clear; close all

set(0,'DefaultTextFontName','Times',...
     'DefaultTextFontSize',18,...
     'DefaultAxesFontName','Times',...
     'DefaultAxesFontSize',18,...
     'DefaultLineLineWidth',4,...
     'DefaultLineMarkerSize',7.75,...
     'DefaultStairLineWidth',2)

%% Colors
pos   = [100 100 500 400]; % figure position on screen

orange  = [1 0.5 0];
green   = [0 0.5 0];
greencg = [83,188,102]/256;
bluecg  = [44,144,195]/256;
movcg   = [198,102,177]/256;
indigo   = [0.2930 0 0.5078];

pistachio = [166 180 1]/256;
gold     = [0.9805, 0.7773,  0.25];
brick    = [108, 1, 2]/256;
cherry   = [222, 58, 58]/256;%[213, 1, 2]/256;
lilac    = [0.7383    0.5898    0.7930];
hotpink  = [0.8945    0.4102    0.5898];
cerulean = [0.1094    0.6641    0.8359];
plum     = [194, 147, 141]/256;

black = [0 0 0];

clr = [bluecg;indigo;movcg;greencg;black];
qmax = 10; qmin=0;
%% Compare new transformation method and fb lin for unmeasured disturbances
tend = 300;
filep = {'Shower_Dist_org','Shower_Dist_fast','Shower_Dist_slow','Shower_Dist_fblin'};

for j=1:4
     load(filep{j})
     
     figure(2); % plot T
     plot(time, T,'Color',clr(j,:))
     hold on
     xlim([0 tend])
     ylim([14 36])
     xlabel('Time [s]')
     ylabel('Temperature (y_2=T) [^oC]')
     set(gcf,'Position',pos)
     
     figure(3) % plot u1=q1
     plot(time,uplant(:,1),'Color',clr(j,:))
     hold on
     xlim([0 tend])
     ylim([-1 11])
     xlabel('Time')
     ylabel('Flowrate (u_1=q_1) [kg/s]')
     set(gcf,'Position',pos)
     
     figure(4) % plot u2=q2
     plot(time,uplant(:,2),'Color',clr(j,:))
     hold on
     xlim([0 tend])
     ylim([-1 11])
     xlabel('Time')
     ylabel('Flowrate (u_2=q_2) [kg/s]')
     set(gcf,'Position',pos)
     
end
figure(1) % plot y1=q
plot(time,q,'Color',greencg)
hold on
plot([0 time(end)],[q0 q0],'Color',clr(end,:),'LineStyle','--') % setpoint
hold on
xlim([0 tend])
%ylim([0.9 1.1])
ylabel('Flowrate (y_1=q) [kg/s]')
xlabel('Time [s]')
set(gcf,'Position',pos)
legend('y_1','y_1^s','Location','northwest')
legend boxoff
print('-depsc2','-r600','Shower_dist_y1.eps')

figure(2);plot([0 time(end)], [T0 T0],'Color',clr(end,:),'LineStyle','--') % setpoint
l2=legend('A=-0.01 (original)','A=-0.02 (faster)','A=-0.005 (slower)','A=0 (Feedback linearization)','y_2^s','Location','southwest');
legend boxoff
print('-depsc2','-r600','Shower_dist_y2.eps')

figure(3)
plot([0 tend],[qmax qmax],'k--')
plot([0 tend],[qmin qmin],'k--')
l3=legend('A=-0.01 (original)','A=-0.02 (faster)','A=-0.005 (slower)','A=0 (feedback linearization)','Location','southwest');
l3.Position = [0.1867    0.2533    0.6020    0.2604];
legend boxoff
print('-depsc2','-r600','Shower_dist_u1.eps')

figure(4)
plot([0 tend],[qmax qmax],'k--')
plot([0 tend],[qmin qmin],'k--')
l4=legend('A=-0.1 (original)','A=-0.2 (faster)','A=-0.05 (slower)','A=0 (feedback linearization)');
l4.Position = [0.1747    0.5896    0.6020    0.2604];
legend boxoff
print('-depsc2','-r600','Shower_dist_u2.eps')


%% Compare calculation block and cascade for input saturation
tend = 500;
filep={'Shower_Input_Sat_exact','Shower_Input_Sat__pairing1'...
     'Shower_Input_Sat__pairing2'};
qmin=0;qmax=10;
 ltyp2 = {'-','-.',':'};
for j=1:3
     load(filep{j})
     
     figure(11) % plot y1=q
     plot(time,q,'Color',clr(j,:))
     hold on
     xlim([0 tend])
     ylim([5 17])
     ylabel('Flowrate (y_1=q) [kg/s]')
     xlabel('Time [s]')
     set(gcf,'Position',pos)
     
     figure(12); % plot T
     plot(time, T,'Color',clr(j,:))
     hold on
     xlim([0 tend])
     ylim([34 41])
     xlabel('Time [s]')
     ylabel('Temperature (y_2=T) [^oC]')
     set(gcf,'Position',pos)
     
     figure(13) % plot u1=q1
     plot(time,uplant(:,1),'Color',clr(j,:),'LineStyle',ltyp2{j})
     hold on
     xlim([0 tend])
     ylim([-1 11])
     xlabel('Time')
     ylabel('Flowrate (u_1=q_1) [kg/s]')
     set(gcf,'Position',pos)
     
     figure(14) % plot u2=q2
     plot(time,uplant(:,2),'Color',clr(j,:),'LineStyle',ltyp2{j})
     hold on
     xlim([0 tend])
     ylim([-1 11])
     xlabel('Time')
     ylabel('Flowrate (u_2=q_2) [kg/s]')
     set(gcf,'Position',pos)
     
end
figure(11); plot(time,v1s,'Color',clr(4,:),'LineStyle',ltyp2{3}) % setpoint
legend('Model-based inversion','Cascade: u_1-v_1; u_2-v_2','Cascade: u_1-v_2; u_2-v_1','y_1^s','Location','best')
legend boxoff
print('-depsc2','-r600','Shower_sat_y1.eps')

figure(12);plot(time, v2s,'Color',clr(4,:),'LineStyle',ltyp2{3}) % setpoint
legend('Model-based inversion','Cascade: u_1-v_1; u_2-v_2','Cascade: u_1-v_2; u_2-v_1','y_2^s','Location','best')
legend boxoff
print('-depsc2','-r600','Shower_sat_y2.eps')

figure(13)
plot([0 tend],[qmax qmax],'k--')
plot([0 tend],[0 0],'k--')
l13=legend('Model-based inversion','Cascade: u_1-v_1; u_2-v_2','Cascade: u_1-v_2; u_2-v_1');
l13.Position=[0.3967    0.2800    0.4993    0.2475];
legend boxoff
print('-depsc2','-r600','Shower_sat_u1.eps')

figure(14)
plot([0 tend],[qmax qmax],'k--')
plot([0 tend],[0 0],'k--')
l14=legend('Model-based inversion','Cascade: u_1-v_1; u_2-v_2','Cascade: u_1-v_2; u_2-v_1');
l14.Position=[0.1703    0.1392    0.4993    0.2475];
legend boxoff
print('-depsc2','-r600','Shower_sat_u2.eps')

%% plot v for cascade
ls={'-','-','-'};
for k = 2:3
     load(filep{k})
     figure(15)
     plot(time,v(:,1),ls{k},'Color',clr(k,:))
     hold on
     xlabel('Time [s]')
     ylabel('Transformed input (v_1)')
     xlim([0 tend]);ylim([5 17])
     set(gcf,'Position',pos)
     
     figure(16)
     plot(time,v(:,2),ls{k},'Color',clr(k,:))
     hold on
     xlabel('Time [s]')
     ylabel('Transformed input (v_2)')
     xlim([0 tend]);ylim([33 50])
     set(gcf,'Position',pos)
end
figure(15); plot(time,v1s,'Color',greencg,'LineStyle','--')
l15=legend('Cascade: u_1-v_1; u_2-v_2','Cascade: u_1-v_2; u_2-v_1','v_1^s');
l15.Location = 'best';
legend boxoff
print('-depsc2','-r600','Shower_sat_v1.eps')

figure(16); plot(time,v2s,'Color',greencg,'LineStyle','--')
l16=legend('Cascade: u_1-v_1; u_2-v_2','Cascade: u_1-v_2; u_2-v_1','v_2^s');
l16.Position=[0.1633    0.6458    0.4887    0.2892];
legend boxoff
print('-depsc2','-r600','Shower_sat_v2.eps')
