clc
clear all
close all

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

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

Vm=1200:50:1800;

% Loading the saved values of the cost (F0) for all the candidate controlled
% variables with disturbances in Vmax.
load F0oVm.mat
load F0Vm_F.mat
load F0Vm_L.mat
load F0Vm_D.mat
load F0Vm_LD.mat
load F0Vm_LF.mat
load F0Vm_LV.mat
load F0Vm_xD.mat
load F0Vm_FF0.mat
load F0Vm_DF0.mat

% Calculating the loss for each candidate controlled variable
LossF_Vm = [(F0Vm_F-F0oVm)./abs(F0oVm)]*100;

LossL_Vm = [(F0Vm_L-F0oVm)./abs(F0oVm)]*100;

LossD_Vm = [(F0Vm_D-F0oVm)./abs(F0oVm)]*100;

LossLD_Vm = [(F0Vm_LD-F0oVm)./abs(F0oVm)]*100;

LossLF_Vm = [(F0Vm_LF-F0oVm)./abs(F0oVm)]*100;

LossLV_Vm = [(F0Vm_LV-F0oVm)./abs(F0oVm)]*100;

LossxD_Vm = [(F0Vm_xD-F0oVm)./abs(F0oVm)]*100;

LossFF0_Vm = [(F0Vm_FF0-F0oVm)./abs(F0oVm)]*100;

LossDF0_Vm = [(F0Vm_DF0-F0oVm)./abs(F0oVm)]*100;


%% 
% Plotting loss due to disturbance in Vmax, using the spline function
% to get smoother graphs.
figure (1)
xx=Vm;
yy=spline(Vm,LossF_Vm,xx);
plot(xx,yy,'b','LineWidth',2.5)
xlim([1200 1800])
ylim([0 2])
set(gca,'XTick',1200:200:1800);
set(gca,'YTick',0:0.5:2);
xlabel('V_m_a_x [kmol/h]','FontSize',12,'FontWeight','bold')
ylabel('Loss [%]','FontSize',12,'FontWeight','bold')
title('Case 2 - Large loss','FontSize',12,'FontWeight','bold')
hold on

yy=spline(Vm,LossL_Vm,xx);
plot(xx,yy,'r','LineWidth',2.5)

yy=spline(Vm,LossD_Vm,xx);
plot(xx,yy,'g','LineWidth',2.5)

yy=spline(Vm,LossFF0_Vm,xx);
plot(xx,yy,':','LineWidth',2.5)

yy=spline(Vm,LossDF0_Vm,xx);
plot(xx,yy,'--','LineWidth',2.5)

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

hold off

figure (2)

yy=spline(Vm,LossxD_Vm,xx);
plot(xx,yy,'k','LineWidth',2.5)
xlim([1200 1800])
ylim([0 0.2])
set(gca,'XTick',1200:200:1800);
set(gca,'YTick',0:0.05:0.2);
xlabel('V_m_a_x [kmol/h]','FontSize',12,'FontWeight','bold')
ylabel('Loss [%]','FontSize',12,'FontWeight','bold')
title('Case 2 - Small loss','FontSize',12,'FontWeight','bold')

hold on

yy=spline(Vm,LossLD_Vm,xx);
plot(xx,yy,'y','LineWidth',2.5)

yy=spline(Vm,LossLF_Vm,xx);
plot(xx,yy,'m','LineWidth',2.5)

yy=spline(Vm,LossLV_Vm,xx);
plot(xx,yy,'c','LineWidth',2.5)

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

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.8:0.02:1.0;

% Loading the saved values of the cost (F0) for all the candidate controlled
% variables with disturbances in zF0.
load F0ozF0.mat
load F0zF0_F.mat
load F0zF0_L.mat
load F0zF0_D.mat
load F0zF0_LD.mat
load F0zF0_LF.mat
load F0zF0_LV.mat
load F0zF0_xD.mat
load F0zF0_FF0.mat
load F0zF0_DF0.mat

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

LossL_zF0 = [(F0zF0_L-F0ozF0)./abs(F0ozF0)]*100;

LossD_zF0 = [(F0zF0_D-F0ozF0)./abs(F0ozF0)]*100;

LossLD_zF0 = [(F0zF0_LD-F0ozF0)./abs(F0ozF0)]*100;

LossLF_zF0 = [(F0zF0_LF-F0ozF0)./abs(F0ozF0)]*100;

LossLV_zF0 = [(F0zF0_LV-F0ozF0)./abs(F0ozF0)]*100;

LossxD_zF0 = [(F0zF0_xD-F0ozF0)./abs(F0ozF0)]*100;

LossFF0_zF0 = [(F0zF0_FF0-F0ozF0)./abs(F0ozF0)]*100;

LossDF0_zF0 = [(F0zF0_DF0-F0ozF0)./abs(F0ozF0)]*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 2])
set(gca,'XTick',0.8:0.05:1);
set(gca,'YTick',0:0.5:2);
xlabel('z_F_0 [-]','FontSize',12,'FontWeight','bold')
ylabel('Loss [%]','FontSize',12,'FontWeight','bold')
title('Case 2 - 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,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','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.2])
set(gca,'XTick',0.8:0.05:1);
set(gca,'YTick',0:0.05:0.2);
xlabel('z_F_0 [kmol/h]','FontSize',12,'FontWeight','bold')
ylabel('Loss [%]','FontSize',12,'FontWeight','bold')
title('Case 2 - 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)

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

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

hold off

%% 
% 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;
F0o_ie=-497.8799;       % Optimal value of cost in case II

% Loading the saved values of the cost (F0) for all the candidate controlled
% variables with implementatione error in CV.
load F0ieF.mat
load F0ieL.mat
load F0ieD.mat
load F0ie_LD.mat
load F0ie_LF.mat
load F0ie_LV.mat
load F0iexD.mat
load F0ie_FF0.mat
load F0ie_DF0.mat

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

LossL_ie = [(F0ieL-F0o_ie)./abs(F0o_ie)]*100;

LossD_ie = [(F0ieD-F0o_ie)./abs(F0o_ie)]*100;

LossLD_ie = [(F0ie_LD-F0o_ie)./abs(F0o_ie)]*100;

LossLF_ie = [(F0ie_LF-F0o_ie)./abs(F0o_ie)]*100;

LossLV_ie = [(F0ie_LV-F0o_ie)./abs(F0o_ie)]*100;

LossxD_ie = [(F0iexD-F0o_ie)./abs(F0o_ie)]*100;

LossFF0_ie = [(F0ie_FF0-F0o_ie)./abs(F0o_ie)]*100;

LossDF0_ie = [(F0ie_DF0-F0o_ie)./abs(F0o_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 2])
set(gca,'XTick',0.8:0.1:1.2);
set(gca,'YTick',0:0.5:2);
xlabel('Implementation error [%]','FontSize',12,'FontWeight','bold')
ylabel('Loss [%]','FontSize',12,'FontWeight','bold')
title('Case 2 - 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.2])
set(gca,'XTick',0.8:0.1:1.2);
set(gca,'YTick',0:0.05:0.2);
xlabel('Implementation error [%]','FontSize',12,'FontWeight','bold')
ylabel('Loss [%]','FontSize',12,'FontWeight','bold')
title('Case 2- 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

%% 
% 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 (F0) for all the candidate controlled
% variables with implementation error in Mr.
load F0oMr.mat
load F0Mr_F.mat
load F0Mr_L.mat
load F0Mr_D.mat
load F0Mr_LD.mat
load F0Mr_LF.mat
load F0Mr_LV.mat
load F0Mr_xD.mat
load F0Mr_FF0.mat
load F0Mr_DF0.mat

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

LossL_Mr = [(F0Mr_L-F0oMr)./abs(F0oMr)]*100;

LossD_Mr = [(F0Mr_D-F0oMr)./abs(F0oMr)]*100;

LossLD_Mr = [(F0Mr_LD-F0oMr)./abs(F0oMr)]*100;

LossLF_Mr = [(F0Mr_LF-F0oMr)./abs(F0oMr)]*100;

LossLV_Mr = [(F0Mr_LV-F0oMr)./abs(F0oMr)]*100;

LossxD_Mr = [(F0Mr_xD-F0oMr)./abs(F0oMr)]*100;

LossFF0_Mr = [(F0Mr_FF0-F0oMr)./abs(F0oMr)]*100;

LossDF0_Mr = [(F0Mr_DF0-F0oMr)./abs(F0oMr)]*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([2400 3200])
ylim([0 0.1])
set(gca,'XTick',2400:200:3200);
set(gca,'YTick',0:0.02:0.1);
xlabel('Implementation error - M_r [kmol]','FontSize',12,'FontWeight','bold')
ylabel('Loss [%]','FontSize',12,'FontWeight','bold')
title('Case 2','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 (F0) for all the candidate controlled
% variables with implementation error in xB.
load F0oxB.mat
load F0xB_F.mat
load F0xB_L.mat
load F0xB_D.mat
load F0xB_LD.mat
load F0xB_LF.mat
load F0xB_LV.mat
load F0xB_xD.mat
load F0xB_FF0.mat
load F0xB_DF0.mat

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

LossL_xB = [(F0xB_L-F0oxB)./(F0oxB)]*100;

LossD_xB = [(F0xB_D-F0oxB)./(F0oxB)]*100;

LossLD_xB = [(F0xB_LD-F0oxB)./(F0oxB)]*100;

LossLF_xB = [(F0xB_LF-F0oxB)./(F0oxB)]*100;

LossLV_xB = [(F0xB_LV-F0oxB)./(F0oxB)]*100;

LossxD_xB = [(F0xB_xD-F0oxB)./(F0oxB)]*100;

LossFF0_xB = [(F0xB_FF0-F0oxB)./(F0oxB)]*100;

LossDF0_xB = [(F0xB_DF0-F0oxB)./(F0oxB)]*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.1])
set(gca,'XTick',0.009:0.001:0.012);
set(gca,'YTick',0:0.02:0.1);
xlabel('Implementation error - x_B [-]','FontSize',12,'FontWeight','bold')
ylabel('Loss [%]','FontSize',12,'FontWeight','bold')
title('Case 2','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
