clear all;
close all;
clc;

%% plot WCE calc. at the end of Nsim = [1,4,8,12,16,20] with different colors
% % change color
% k = 0;
% Nsimmax = 20;
% G = cell(5,1);
% G{1,1} = 'b';
% G{2,1} = 'g';
% G{3,1} = 'r';
% G{4,1} = 'c';
% G{5,1} = 'k';
% G{6,1} = 'y';
% 
% for Nplot = [1,4,8,12,16,20]
% k = k + 1;
% load(['Nsim' num2str(Nplot) '.mat'])
% % plot(nred,objval_scaling, G{k,1})     %using scaling obj. fun
% % plot(nred,objval/Nsimmax, G{k,1})       %using scaling obj. fun with max Nsim
% plot(nred,objval, G{k,1})               %using nonscaling obj. fun
% hold on
% end
% 
% % plotting (labels, title, legend..)
% 
% xlabel('states in reduced-order controller')
% ylabel('worst case error (outputs)')
% title('Nsim = [1,4,8,12,16,20]');
% XLIM([1 16]);
% legend('Nsim = 1','Nsim = 4', 'Nsim = 8', 'Nsim = 12', 'Nsim = 16', 'Nsim = 20')

%% plot d01,d02 for every Nsim, nred
% k = 0;
% G = cell(5,1);
% G{1,1} = 'b*';
% G{2,1} = 'go';
% G{3,1} = 'r+';
% G{4,1} = 'cv';
% G{5,1} = 'kh';
% G{6,1} = 'yp';
% d01all = [];
% d02all = [];
% for Nplot = [1,4,8,12,16,20]
% k = k + 1;    
% load(['Nsim' num2str(Nplot) '.mat'])
% d01all = [d01all d01];
% d02all = [d02all d02];
% % subplot(2,1,1),
% % plot(nred,d01,G{k,1})
% plot(d01,d02,G{k,1})
% % plotting (labels, title, legend..)
% hold on
% xlabel('d01 disturbance')
% ylabel('d02 disturbance)')
% title('Nsim = [1,4,8,12,16,20]');
% % XLIM([1 16]);
% 
% % subplot(2,1,2),
% % plot(nred,d02,G{k,1})
% % hold on
% end
% 
% % plotting (labels, title, legend..)
% 
% % xlabel('states in reduced-order controller')
% % ylabel('worst case error (disturbances d02)')
% % title('Nsim = [1,4,8,12,16,20]');
% % XLIM([1 16]);
% legend('Nsim = 1','Nsim = 4', 'Nsim = 8', 'Nsim = 12', 'Nsim = 16', 'Nsim = 20')

%% plot d01,d02 for reduced model nred = 10 with changing Nsim
% k = 0;
% G = cell(5,1);
% G{1,1} = 'b^';
% G{2,1} = 'gx';
% G{3,1} = 'rv';
% G{4,1} = 'c+';
% G{5,1} = 'yh';
% G{6,1} = 'k+';
% d01_n10 = [];
% d02_n10 = [];
% for Nplot = [1,4,8,12,16,20]
% k = k + 1;    
% load(['Nsim' num2str(Nplot) '.mat'])
% d01_n10 = [d01_n10 d01(10)];
% d02_n10 = [d02_n10 d02(10)];
% % subplot(2,1,1),
% % plot(nred,d01,G{k,1})
% plot(d01(10),d02(10),G{k,1},'LineWidth',6)
% % plotting (labels, title, legend..)
% hold on
% xlabel('d01 disturbance')
% ylabel('d02 disturbance)')
% title('Nsim = [1,4,8,12,16,20]');
% 
% % subplot(2,1,2),
% % plot(nred,d02,G{k,1})
% % hold on
% end
% XLIM([-1.5 1.5]);
% YLIM([-1.5 1.5]);
% % plotting (labels, title, legend..)
% 
% % xlabel('states in reduced-order controller')
% % ylabel('worst case error (disturbances d02)')
% % title('Nsim = [1,4,8,12,16,20]');
% % XLIM([1 16]);
% legend('Nsim = 1','Nsim = 4', 'Nsim = 8', 'Nsim = 12', 'Nsim = 16', 'Nsim = 20')

%% plot WCE calc. at the end of Nsim 1:20
% Nsimmax = 20;
% for Nplot = 1:20
% load(['Nsim' num2str(Nplot) '.mat'])
% % plot(nred,objval_scaling) %using scaling obj. fun
% % plot(nred,objval/Nsimmax) %using scaling (1/maxNsim) obj. fun
% plot(nred,objval)           %using nonscaling obj. fun
% hold on
% end
% 
% % plotting (labels, title, legend..)
% 
% xlabel('states in reduced-order controller')
% ylabel('worst case error (outputs)')
% title('Nsim 1-20');
% XLIM([1 16]);
% % legend('Nsim = 1:20')

%% analyze check plot (compare WCE calculating and from MPC simulation)

% load ('analyze_check.mat');
% 
% figure;
% plot(nred,objval,'LineWidth',2)
% xlabel('states in reduced-order controller')
% ylabel('worst case error (outputs)')
% XLIM([1 16]);
% title(['Nsim' num2str(Nsim)])
% 
% figure;
% plot(nred,objvalsim,'LineWidth',2)
% xlabel('states in reduced-order controller')
% ylabel('worst case error (outputs)')
% XLIM([1 16]);
% title(['Cloosed loop simulation Nsim' num2str(Nsim)])

%% plot sum of WCE using MPC simulation and information from the end of  Nsim = [1,4,8,12,16,20] with different colors

% calculate objsum of WCE for this Nsim and plot it 
% change color
k = 0;
G = cell(5,1);
G{1,1} = 'b';
G{2,1} = 'g';
G{3,1} = 'r';
G{4,1} = 'c';
G{5,1} = 'k';
G{6,1} = 'y';
for Nplot = [1,4,8,12,16,20]
k = k + 1;
load(['Nsim' num2str(Nplot) '.mat'])
[objvalsumnred] = sum_WCE(d01,d02,nfull,nred,Nsim); 


plot(nred,objvalsumnred)               %using nonscaling obj. fun

hold on
end

% plotting (labels, title, legend..)

xlabel('states in reduced-order controller')
ylabel('sum of worst case error (outputs)')
title('Nsim = [1,4,8,12,16,20]');
XLIM([1 16]);
legend('Nsim = 1','Nsim = 4', 'Nsim = 8', 'Nsim = 12', 'Nsim = 16', 'Nsim = 20')

%% plot sum of WCE using MPC simulation and information from the end of Nsim = 1:20 

% % calculate objsum of WCE for this Nsim and plot it 
% % change color
% k = 0;
% for Nplot = 1:20
% k = k + 1;
% load(['Nsim' num2str(Nplot) '.mat'])
% [objvalsumnred] = sum_WCE(d01,d02,nfull,nred,Nsim); 
% 
% plot(nred,objvalsumnred)               %using nonscaling obj. fun
% 
% hold on
% end
% 
% % plotting (labels, title, legend..)
% 
% xlabel('states in reduced-order controller')
% ylabel('worst case error (outputs)')
% title('Nsim 1-20');
% XLIM([1 16]);

%% calculating and ploting to compare with updating obj. fun -  sum of WCE using simulation and info from the end of Nsim = 1:10
% 
% % calculate objsum of WCE for this Nsim and plot it 
% % change color
% k = 0;
% for Nplot = 1:10
% k = k + 1;
% load(['Nsim' num2str(Nplot) '.mat'])
% [objvalsumnred] = compare_sum_WCE(d01,d02,nfull,nred,Nsim); 
% 
% plot(nred,objvalsumnred)               %using nonscaling obj. fun
% 
% hold on
% end
% 
% % plotting (labels, title, legend..)
% 
% xlabel('states in reduced-order controller')
% ylabel('worst case error (outputs)')
% title('Nsim 1-10');
% XLIM([1 16]);
%% just plot plot sum of WCE usin Nsim = 1:20 with different colors

% k = 0;
% Nsimmax = 20;
% 
% for Nplot = 1:20
% k = k + 1;
% load(['Objsum_Nsim' num2str(Nplot) '.mat'])
% plot(nred,objvalsumnred,'LineWidth',2)               %using nonscaling obj. fun
% hold on
% end
% 
% % plotting (labels, title, legend..)
% 
% xlabel('states in reduced-order controller')
% ylabel('sum of worst case error (outputs)')
% title('Nsim 1-20');
% XLIM([1 16]);

%% just plot sum of WCE using Nsim = [1,4,8,12,16,20] with different colors

% % change color
% k = 0;
% Nsimmax = 20;
% G = cell(5,1);
% G{1,1} = 'b';
% G{2,1} = 'g';
% G{3,1} = 'r';
% G{4,1} = 'c:';
% G{5,1} = 'k--';
% G{6,1} = 'y:';
% 
% figure;
% for Nplot = [1,4,8,12,16,20]
% k = k + 1;
% load(['Objsum_Nsim' num2str(Nplot) '.mat'])
% plot(nred,objvalsumnred, G{k,1},'LineWidth',2)               %using nonscaling obj. fun
% hold on
% end
% 
% % plotting (labels, title, legend..)
% 
% xlabel('states in reduced-order controller')
% ylabel('sum of worst case error (outputs)')
% title('Nsim = [1,4,8,12,16,20]');
% XLIM([1 16]);
% legend('Nsim = 1','Nsim = 4', 'Nsim = 8', 'Nsim = 12', 'Nsim = 16', 'Nsim = 20')

%% plot sum of WCE  usin Nsim = 1:10 with different colors
          
% % change color
% k = 0;
% G = cell(5,1);
% G{1,1} = 'b:';
% G{2,1} = 'g';
% G{3,1} = 'r';
% G{4,1} = 'c';
% G{5,1} = 'k--';
% G{6,1} = 'y:';
% G{7,1} = 'g:';
% G{8,1} = 'r--';
% G{9,1} = 'c--';
% G{10,1} = 'b';
% 
% figure; %ploting for every Nsim
% for Nplot = 1:10
% k = k + 1;
% load(['Comparesum_Nsim' num2str(Nplot) '.mat'])
% 
%     plot(nred,objvalsumnred, G{k,1},'LineWidth',2)               %using nonscaling obj. fun
%     hold on
% end
% 
% % plot comparison with reall sum of WCE witn Nsim = 10
% 
% load('sumWCE.mat')
% objval(16) =  1.7186e-017;
% plot(nred,objval,'--ms','LineWidth',2)
% 
% % plotting (labels, title, legend..)
% 
% xlabel('states in reduced-order controller')
% ylabel('sum of worst case error (outputs)')
% title('Nsim = 1:10');
% XLIM([1 16]);
% legend('Nsim = 1','Nsim = 2', 'Nsim = 3', 'Nsim = 4', 'Nsim = 5', 'Nsim = 6', 'Nsim = 7', 'Nsim = 8', 'Nsim = 9', 'Nsim = 10', 'Real Nsim = 10')
% 
% % comparing Nsim = 10 and real
% figure
% load('Comparesum_Nsim10.mat')
% plot(nred,objvalsumnred,'b','LineWidth',2)
% hold on
% load('sumWCE.mat')
% objval(16) =  1.7186e-017;
% plot(nred,objval,'--ms','LineWidth',2)
% xlabel('states in reduced-order controller')
% ylabel('sum of worst case error (outputs)')
% title('Nsim = 10');
% XLIM([1 16]);
% legend('Nsim = 10', 'Real Nsim = 10')




