% Ploting the results
figure(1);clf;
figure(2);clf;

files = {'resModelLagMs200Final.mat'
         'resModelLagMs170Final.mat'
         'resModelLagMs159Final.mat'
         'resModelLagMs150Final.mat'
         'resModelLagMs120Final.mat'};
colorSet = colormap('lines');

colorSet = colorSet([1:5,7:end],:);

xval = 40;
yval1 = [];
yval2 = [];
for i = 1:length(files);
    load(files{i})
    data{i} = res;




figure(1)
r = plot(data{i}.minTuning.lagConstant,data{i}.minTuning.tuning(1,:)./data{i}.minTuning.tuning(2,:));
xlab1 = xlabel('Process time constant, $\tau_1/\theta$', 'interpreter','latex');
ylab1 = ylabel('Integral time, $\tau_I/\theta$', 'interpreter','latex');
%legend('Opt','SIMC','Sp','D')
axis([0 50 0 11])
set(r, 'color', colorSet(i,:), 'LineWidth', 1.5);
index = find(data{i}.minTuning.lagConstant==xval);
yval1 = [yval1; data{i}.minTuning.tuning(1,index)./data{i}.minTuning.tuning(2,index)];
hold on


figure(2)
p = plot(data{i}.minTuning.lagConstant,data{i}.minTuning.tuning(1,:)./data{i}.minTuning.lagConstant); %
xlab2 = xlabel('Process time constant, $\tau_1/\theta$', 'interpreter','latex');
ylab2 = ylabel('Controller gain, $K_ck\theta / \tau_1$', 'interpreter','latex'); %%
%legend('Opt','SIMC','Sp','D')
axis([0 50 0 1])
set(p, 'color',colorSet(i,:),'LineWidth', 1.5);
index = find(data{i}.minTuning.lagConstant==xval);
yval2 = [yval2; data{i}.minTuning.tuning(1,index)./data{i}.minTuning.lagConstant(index)];
hold on

end
%% SIMC
 figure(1)
 r = plot([0 8 50],[0 8 8]);
 set(r, 'color', [0 0 0], 'LineWidth', 1.5,'LineStyle','--');
 %      leg = legend('$M_s=1.70$', '$M_s=1.59$','$M_s=1.50$'); %,'SIMC($\tau_c=\theta$)'
% %     set(leg,'interpreter','latex','box','off','Location','best')
 figure(2)
 p = plot([0 50],[0.5 .5]);
 set(p, 'color', [0 0 0], 'LineWidth', 1.5,'LineStyle','--');


set(xlab1,'fontsize',14)
set(ylab1,'fontsize',14)
set(xlab2,'fontsize',14)
set(ylab2,'fontsize',14)
% 
annotationString  = {'$M_s=2.00$';
                     '$M_s=1.70$';
                     '$M_s=1.59$';
                     '$M_s=1.50$';
                     '$M_s=1.20$'};

        %xval(6)=xval(5);
        yval1(6)=8.1;
        yval2(6)=.51;
        xval=ones(5,1)*xval;
        xval(6) = 25;

%ypos = [4.5,4,3.5,3,2.5,2];                 
yval1(5)=yval1(5)-.6;               
for i = 1:length(annotationString)+1
    
    figure(1)
        annotationString{6}='SIMC$(\tau_c=\theta)$';
        
    [figx figy] = dsxy2figxy(gca, xval(i), yval1(i)-0.15);

    curve1 = annotation('textbox',[figx, figy, .07, .05],'string',annotationString{i},'interpreter','latex','fontsize',14,'color',colorSet(i,:),'FitBoxToText','on','LineStyle','non');    
%end

figure(2)
        annotationString{6}='SIMC$(\tau_c=\theta)$';

                 
                 
%for i = 1:length(annotationString)
    [figx figy] = dsxy2figxy(gca, xval(i), yval2(i)-.018);
    curve1 = annotation('textbox',[figx, figy, .07, .05],'string',annotationString{i},'interpreter','latex','fontsize',14,'color',colorSet(i,:),'FitBoxToText','on','LineStyle','non');    
end
saveas(r,'TuningIntegralVsLagMs.eps','psc2')
saveas(p,'TuningGainVsLagMs.eps','psc2')
