clc
clear all
close all

%% Script for plotting the losses with the Brute force method in case I

% Loss for disturbances in F0, with F, L, D, L/D, L/F, L/V, xD, F/F0
% and D/F0 kept constant, respectively

F0=368:11.5:552;

% Loading the saved values of the cost (V) for all the candidate controlled
% variables with disturbances in F0.
load VoptF0.mat
load VF0_F.mat
load VF0_L.mat
load VF0_D.mat
load VF0_LD.mat
load VF0_LF.mat
load VF0_LV.mat
load VF0_xD.mat
load VF0_FF0.mat
load VF0_DF0.mat

% Calculating the loss for each candidate controlled variable
LossF_F0 = [(VF0_F-VoptF0)./VoptF0]*100;

LossL_F0 = [(VF0_L-VoptF0)./VoptF0]*100;

LossD_F0 = [(VF0_D-VoptF0)./VoptF0]*100;

LossLD_F0 = [(VF0_LD-VoptF0)./VoptF0]*100;

LossLF_F0 = [(VF0_LF-VoptF0)./VoptF0]*100;

LossLV_F0 = [(VF0_LV-VoptF0)./VoptF0]*100;

LossxD_F0 = [(VF0_xD-VoptF0)./VoptF0]*100;

LossFF0_F0 = [(VF0_FF0-VoptF0)./VoptF0]*100;

LossDF0_F0 = [(VF0_DF0-VoptF0)./VoptF0]*100;

%% 
% Plotting the losses due to disturbance in F0, using the spline function
% to get smoother graphs.
figure (1)
xx=F0;
yy=spline(F0,LossF_F0,xx);
plot(xx,yy,'b','LineWidth',2.5)
xlim([370 550])
ylim([0 10])
set(gca,'XTick',400:50:550);
set(gca,'YTick',0:2:10);
xlabel('F_0 [kmol/h]','FontSize',12,'FontWeight','bold')
ylabel('Loss [%]','FontSize',12,'FontWeight','bold')
title('Case 1 - Large loss','FontSize',12,'FontWeight','bold')
hold on

yy=spline(F0,LossL_F0,xx);
plot(xx,yy,'r','LineWidth',2.5)

yy=spline(F0,LossD_F0,xx);
plot(xx,yy,'g','LineWidth',2.5)

yy=spline(F0,LossLV_F0,xx);
plot(xx,yy,'c','LineWidth',2.5)

yy=spline(F0,LossFF0_F0,xx);
plot(xx,yy,':','LineWidth',2.5)

yy=spline(F0,LossDF0_F0,xx);
plot(xx,yy,'--','LineWidth',2.5)

legend('F','L','D','L/V','F/F0','D/F0')

hold off

figure (2)

yy=spline(F0,LossxD_F0,xx);
plot(xx,yy,'k','LineWidth',2.5)
xlim([370 550])
ylim([0 0.5])
set(gca,'XTick',400:50:550);
set(gca,'YTick',0:0.1:0.5);
xlabel('F_0 [kmol/h]','FontSize',12,'FontWeight','bold')
ylabel('Loss [%]','FontSize',12,'FontWeight','bold')
title('Case 1 - Small loss','FontSize',12,'FontWeight','bold')

hold on

yy=spline(F0,LossLD_F0,xx);
plot(xx,yy,'y','LineWidth',2.5)

yy=spline(F0,LossLF_F0,xx);
plot(xx,yy,'m','LineWidth',2.5)

legend('x_D','L/D','L/F')

hold off

%% 
% Loss for disturbances in zF0, with F, L, D, L/D, L/F, L/V, xD, F/F0
% and D/F0 kept constant, respectively

zF0=0.80:0.02:1.0;

% Loading the saved values of the cost (V) for all the candidate controlled
% variables with disturbances in zF0.
load VoptzF0.mat
load VzF0_F.mat
load VzF0_L.mat
load VzF0_D.mat
load VzF0_LD.mat
load VzF0_LF.mat
load VzF0_LV.mat
load VzF0_xD.mat
load VzF0_FF0.mat
load VzF0_DF0.mat

% Calculating the loss for each candidate controlled variable
LossF_zF0 = [(VzF0_F-VoptzF0)./VoptzF0]*100;

LossL_zF0 = [(VzF0_L-VoptzF0)./VoptzF0]*100;

LossD_zF0 = [(VzF0_D-VoptzF0)./VoptzF0]*100;

LossLD_zF0 = [(VzF0_LD-VoptzF0)./VoptzF0]*100;

LossLF_zF0 = [(VzF0_LF-VoptzF0)./VoptzF0]*100;

LossLV_zF0 = [(VzF0_LV-VoptzF0)./VoptzF0]*100;

LossxD_zF0 = [(VzF0_xD-VoptzF0)./VoptzF0]*100;

LossFF0_zF0 = [(VzF0_FF0-VoptzF0)./VoptzF0]*100;

LossDF0_zF0 = [(VzF0_DF0-VoptzF0)./VoptzF0]*100;

%% 
% Plotting loss due to disturbance in zF0, using the spline function
% to get smoother graphs.
xx=zF0;

figure (3)

yy=spline(zF0,LossF_zF0,xx);
plot(xx,yy,'b','LineWidth',2.5)
xlim([0.8 1])
ylim([0 10])
set(gca,'XTick',0.8:0.05:1);
set(gca,'YTick',0:2:10);
xlabel('z_F_0 [-]','FontSize',12,'FontWeight','bold')
ylabel('Loss [%]','FontSize',12,'FontWeight','bold')
title('Case 1 - Large loss','FontSize',12,'FontWeight','bold')

hold on

yy=spline(zF0,LossL_zF0,xx);
plot(xx,yy,'r','LineWidth',2.5)

yy=spline(zF0,LossD_zF0,xx);
plot(xx,yy,'g','LineWidth',2.5)

yy=spline(zF0,LossLV_zF0,xx);
plot(xx,yy,'c','LineWidth',2.5)

yy=spline(zF0,LossFF0_zF0,xx);
plot(xx,yy,':','LineWidth',2.5)

yy=spline(zF0,LossDF0_zF0,xx);
plot(xx,yy,'--','LineWidth',2.5)

legend('F','L','D','L/V','F/F0','D/F0')

hold off

figure (4)

yy=spline(zF0,LossxD_zF0,xx);
plot(xx,yy,'k','LineWidth',2.5)
xlim([0.80 1])
ylim([0 0.5])
set(gca,'XTick',0.8:0.05:1);
set(gca,'YTick',0:0.1:0.5);
xlabel('z_F_0 [kmol/h]','FontSize',12,'FontWeight','bold')
ylabel('Loss [%]','FontSize',12,'FontWeight','bold')
title('Case 1 - Small loss','FontSize',12,'FontWeight','bold')

hold on

yy=spline(zF0,LossLD_zF0,xx);
plot(xx,yy,'y','LineWidth',2.5)

yy=spline(zF0,LossLF_zF0,xx);
plot(xx,yy,'m','LineWidth',2.5)

legend('x_D','L/D','L/F')

hold off

% break 

%% 
% Loss for implementation error in F, L, D, L/D, L/F, L/V, xD, F/F0
% and D/F0, respectively

ie=0.8:0.05:1.2;
% Loading the saved values of the cost (V) for all the candidate controlled
% variables with implementation error in the candidate controlled variables.
Vopt_ie=1275.7;             % Optimal cost for case I
load VieF.mat
load VieL.mat
load VieD.mat
load Vie_LD.mat
load Vie_LF.mat
load Vie_LV.mat
load ViexD.mat
load Vie_FF0.mat
load Vie_DF0.mat

% Calculating the loss for each candidate controlled variable
LossF_ie = [(VieF-Vopt_ie)./Vopt_ie]*100;

LossL_ie = [(VieL-Vopt_ie)./Vopt_ie]*100;

LossD_ie = [(VieD-Vopt_ie)./Vopt_ie]*100;

LossLD_ie = [(Vie_LD-Vopt_ie)./Vopt_ie]*100;

LossLF_ie = [(Vie_LF-Vopt_ie)./Vopt_ie]*100;

LossLV_ie = [(Vie_LV-Vopt_ie)./Vopt_ie]*100;

LossxD_ie = [(ViexD-Vopt_ie)./Vopt_ie]*100;

LossFF0_ie = [(Vie_FF0-Vopt_ie)./Vopt_ie]*100;

LossDF0_ie = [(Vie_DF0-Vopt_ie)./Vopt_ie]*100;

%%
% Plotting loss due to implementation error in c, using the spline function
% to get smoother graphs.

figure (5)

xx=ie;
yy=spline(ie,LossF_ie,xx);
plot(xx,yy,'b','LineWidth',2.5)
xlim([0.8 1.2])
ylim([0 10])
set(gca,'XTick',0.8:0.1:1.2);
set(gca,'YTick',0:2:10);
xlabel('Implementation error [%]','FontSize',12,'FontWeight','bold')
ylabel('Loss [%]','FontSize',12,'FontWeight','bold')
title('Case 1 - Large loss','FontSize',12,'FontWeight','bold')
hold on

yy=spline(ie,LossL_ie,xx);
plot(xx,yy,'r','LineWidth',2.5)

yy=spline(ie,LossD_ie,xx);
plot(xx,yy,'g','LineWidth',2.5)

yy=spline(ie,LossLV_ie,xx);
plot(xx,yy,'c','LineWidth',2.5)

yy=spline(ie,LossFF0_ie,xx);
plot(xx,yy,':','LineWidth',2.5)

yy=spline(ie,LossDF0_ie,xx);
plot(xx,yy,'--','LineWidth',2.5)

legend('F','L','D','L/V','F/F0','D/F0')

hold off

figure (6)

yy=spline(ie,LossxD_ie,xx);
plot(xx,yy,'k','LineWidth',2.5)
xlim([0.8 1.2])
ylim([0 0.5])
set(gca,'XTick',0.8:0.1:1.2);
set(gca,'YTick',0:0.1:0.5);
xlabel('Implementation error [%]','FontSize',12,'FontWeight','bold')
ylabel('Loss [%]','FontSize',12,'FontWeight','bold')
title('Case 1- Small loss','FontSize',12,'FontWeight','bold')

hold on

yy=spline(ie,LossLD_ie,xx);
plot(xx,yy,'y','LineWidth',2.5)

yy=spline(ie,LossLF_ie,xx);
plot(xx,yy,'m','LineWidth',2.5)

legend('x_D','L/D','L/F')

hold off

% break 

%% 
% Loss for implementation error in Mr with F, L, D, L/D, L/F, L/V, xD, F/F0
% and D/F0 kept constant, respectively

Mr=2300:50:2800;
% Loading the saved values of the cost (V) for all the candidate controlled
% variables with implementation error in Mr.
load VoptMr.mat
load VMr_F.mat
load VMr_L.mat
load VMr_D.mat
load VMr_LD.mat
load VMr_LF.mat
load VMr_LV.mat
load VMr_xD.mat
load VMr_FF0.mat
load VMr_DF0.mat

% Calculating the loss for each candidate controlled variable
LossF_Mr = [(VMr_F-VoptMr)./VoptMr]*100;

LossL_Mr = [(VMr_L-VoptMr)./VoptMr]*100;

LossD_Mr = [(VMr_D-VoptMr)./VoptMr]*100;

LossLD_Mr = [(VMr_LD-VoptMr)./VoptMr]*100;

LossLF_Mr = [(VMr_LF-VoptMr)./VoptMr]*100;

LossLV_Mr = [(VMr_LV-VoptMr)./VoptMr]*100;

LossxD_Mr = [(VMr_xD-VoptMr)./VoptMr]*100;

LossFF0_Mr = [(VMr_FF0-VoptMr)./VoptMr]*100;

LossDF0_Mr = [(VMr_DF0-VoptMr)./VoptMr]*100;

%% 
% Plotting loss due to implementation error (back-off) in Mr, using the spline function
% to get smoother graphs.
figure (7)

xx=Mr;
yy=spline(Mr,LossF_Mr,xx);
plot(xx,yy,'b','LineWidth',2.5)
xlim([2300 3300])
ylim([0 10])
set(gca,'XTick',2400:200:3200);
set(gca,'YTick',0:2:10);
xlabel('Implementation error - M_r [kmol]','FontSize',12,'FontWeight','bold')
ylabel('Loss [%]','FontSize',12,'FontWeight','bold')
title('Case 1','FontSize',12,'FontWeight','bold')
hold on

yy=spline(Mr,LossL_Mr,xx);
plot(xx,yy,'r','LineWidth',2.5)

yy=spline(Mr,LossD_Mr,xx);
plot(xx,yy,'g','LineWidth',2.5)

yy=spline(Mr,LossLV_Mr,xx);
plot(xx,yy,'c','LineWidth',2.5)

yy=spline(Mr,LossFF0_Mr,xx);
plot(xx,yy,':','LineWidth',2.5)

yy=spline(Mr,LossDF0_Mr,xx);
plot(xx,yy,'--','LineWidth',2.5)

yy=spline(Mr,LossxD_Mr,xx);
plot(xx,yy,'k','LineWidth',2.5)

yy=spline(Mr,LossLD_Mr,xx);
plot(xx,yy,'y','LineWidth',2.5)

yy=spline(Mr,LossLF_Mr,xx);
plot(xx,yy,'m','LineWidth',2.5)

legend('F','L','D','L/V','F/F0','D/F0','x_D','L/D','L/F')

hold off

%% 
% Loss for implementation error in xB with F, L, D, L/D, L/F, L/V, xD, F/F0
% and D/F0 kept constant, respectively

xB=0.009:0.00015:0.0105;
% Loading the saved values of the cost (V) for all the candidate controlled
% variables with implementation error in xB.
load VoptxB.mat
load VxB_F.mat
load VxB_L.mat
load VxB_D.mat
load VxB_LD.mat
load VxB_LF.mat
load VxB_LV.mat
load VxB_xD.mat
load VxB_FF0.mat
load VxB_DF0.mat

% Calculating the loss for each candidate controlled variable
LossF_xB = [(VxB_F-VoptxB)./VoptxB]*100;

LossL_xB = [(VxB_L-VoptxB)./VoptxB]*100;

LossD_xB = [(VxB_D-VoptxB)./VoptxB]*100;

LossLD_xB = [(VxB_LD-VoptxB)./VoptxB]*100;

LossLF_xB = [(VxB_LF-VoptxB)./VoptxB]*100;

LossLV_xB = [(VxB_LV-VoptxB)./VoptxB]*100;

LossxD_xB = [(VxB_xD-VoptxB)./VoptxB]*100;

LossFF0_xB = [(VxB_FF0-VoptxB)./VoptxB]*100;

LossDF0_xB = [(VxB_DF0-VoptxB)./VoptxB]*100;

%% 
% Plotting loss due to implementation error (back-off) in xB, using the spline function
% to get smoother graphs.
figure (8)

xx=xB;
yy=spline(xB,LossF_xB,xx);
plot(xx,yy,'b','LineWidth',2.5)
xlim([0.009 0.012])
ylim([0 0.5])
set(gca,'XTick',0.009:0.001:0.012);
set(gca,'YTick',0:0.1:0.5);
xlabel('Implementation error - x_B [-]','FontSize',12,'FontWeight','bold')
ylabel('Loss [%]','FontSize',12,'FontWeight','bold')
title('Case 1','FontSize',12,'FontWeight','bold')
hold on

yy=spline(xB,LossL_xB,xx);
plot(xx,yy,'r','LineWidth',2.5)

yy=spline(xB,LossD_xB,xx);
plot(xx,yy,'g','LineWidth',2.5)

yy=spline(xB,LossLV_xB,xx);
plot(xx,yy,'c','LineWidth',2.5)

yy=spline(xB,LossFF0_xB,xx);
plot(xx,yy,':','LineWidth',2.5)

yy=spline(xB,LossDF0_xB,xx);
plot(xx,yy,'--','LineWidth',2.5)

yy=spline(xB,LossxD_xB,xx);
plot(xx,yy,'k','LineWidth',2.5)

yy=spline(xB,LossLD_xB,xx);
plot(xx,yy,'y','LineWidth',2.5)

yy=spline(xB,LossLF_xB,xx);
plot(xx,yy,'m','LineWidth',2.5)

legend('F','L','D','L/V','F/F0','D/F0','x_D','L/D','L/F')

hold off
