%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
%~~~~~~~~~~   The file is used to import simulation data   ~~~~~~~~~~~~~~~%
%~~~~~~~~~~~~~~~~   from  Unisim and plot them   ~~~~~~~~~~~~~~~~~~~~~~~~~%
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%

clc
clf
clear all
close all

load Opt_data


%---------------------  Plots  -------------------------------------------%
% Combined plot of Cost function
figure(1)
p1=polyfit(X, J1, 7);
p2=polyfit(X, J2, 5);

plot([0 3.5],[min(mean(J1), mean(J2)) min(mean(J1), mean(J2))+1], 'LineWidth', 500, 'Color', [0.85 0.85 0.85])
hold on
area((0:0.1:2.5),polyval(p2,0:0.1:2.5)', 'FaceColor', [0.99 0.99 0.99]);


set(gca,'Layer','top')

pl1= plot(X, J1, '-ks','MarkerEdgeColor','k',...
                'MarkerFaceColor','m',...
                'MarkerSize',5);
hold on

pl2= plot(X, J2, '-ks','MarkerEdgeColor','k',...
                'MarkerFaceColor','g',...
                'MarkerSize',5);


pl3=plot(1.25, polyval(p1,1.25),'ko','MarkerEdgeColor','k',...
                'MarkerFaceColor','m',...
                'MarkerSize',10);       
pl4=plot(2.5, polyval(p2,2.5),'go','MarkerEdgeColor','k',...
                'MarkerFaceColor','g',...
                'MarkerSize',10);

opt_val=1.12; %Optimum

pl5=plot(opt_val, polyval(p2,opt_val),'bo','MarkerEdgeColor','k',...
                'MarkerFaceColor','b',...
                'MarkerSize',10);
grid on
xlabel('x_{B,ic4} [%]')
ylabel('J (cost function)')
legend ([pl1 pl2 pl3 pl4 pl5],'J (x_{D,nc4}=2%)','J (x_{D,nc4}=4%)', 'On 50% of spec',...
    'On spec', 'Optimal operation', 'Location', 'North')
axis([0 3.5 min(min(J1), min(J2))*1.001 max(max(J1), max(J2))*0.987])
hold off

