clearvars
close all
axisfontsize=16;
panelletterfontsize=18;
figname='Matlab_FinalFig1';
figfolder= 'C:\Users\matlh\OneDrive - University of Leeds\Research\My Projects\In-Leeds\Coop-FixRelThr\Research Data Leeds Repository\';
datafigname= 'trajectory [x_th, nu, delta]=[0.36666666666666664, 0.1, 0.5] 0.csv';
x_th = 11/30;
% Load figure data
Data = readmatrix([figfolder datafigname],'NumHeaderLines',1);
FigData.Nth = cat(2,Data(:,4),x_th*Data(:,3));
FigData.K_Nth = cat(2,Data(:,4),(1-x_th)*Data(:,3));
FigData.K = cat(2,Data(:,4),Data(:,3));
FigData.Nc = cat(2,Data(:,4),Data(:,1));
FigData.Nd = cat(2,Data(:,4),Data(:,2)); clearvars Data
FigData.xlim = [20 40]; FigData.ylim = [0 1100];
% Plot figure
figure; set(gcf,'Position',[38.6 49 1439.2 704]);
hold on; plot(FigData.Nth(:,1),FigData.Nth(:,2),'b--')
hold on; plot(FigData.Nth(:,1),FigData.K_Nth(:,2),'r--')
hold on; plot(FigData.K(:,1),FigData.K(:,2),'k-')
hold on; plot(FigData.K(:,1),FigData.Nc(:,2)+FigData.Nd(:,2),'-','Color',[0 .75 0])
hold on; plot(FigData.Nc(:,1),FigData.Nc(:,2),'b-')
hold on; plot(FigData.Nd(:,1),FigData.Nd(:,2),'r-')
xlim(FigData.xlim); ylim(FigData.ylim);
% Modify the plot
f=gcf;
set(f,'PaperPositionMode','auto');         
set(f,'PaperOrientation','landscape'); %set(f,'PaperOrientation','landscape'); %set(f,'PaperOrientation','portrait');
set(f,'Position',[61 61 3/2*964.8 704],'Name','Matlab_FinalFig1'); %set(f,'Position',[332.2 61 964.8 704],'Name','Matlab_FinalFig1')
Axes = gca;
sub(2)=subplot(2,3,3,Axes); %sub(2)=subplot(2,2,2,Axes);
xLim=xlim; xlim auto
set(findobj(gca,'Type','Line','Color',[0,0,0]),'LineWidth',2);
% Compute deterministic Nc and Nd.
S=0.2;A=0.5;Kmn=100;Kmx=1000;x_th;
XLim=xlim; dt=XLim(2)/1e6; tpoints=XLim(1):dt:XLim(2); clearvars XLim
K=findobj(gca,'Type','Line','Color',[0,0,0]);
k(:,1)=K.XData; k(:,2)=K.YData; NoNans=~isnan(k(:,1))&~isnan(k(:,2));
K=interp1(k(NoNans,1),k(NoNans,2),tpoints);
Nc=nan(size(tpoints)); Nd=Nc;
Nc(1)=x_th*K(1); Nd(1)=(1-x_th)*K(1);
for i=2:numel(tpoints)
    nc=Nc(i-1);
    nd=Nd(i-1);
    n=Nc(i-1)+Nd(i-1);
    h=nc<x_th*n;
    k=K(i);
    denom=(1-S)*nc/n+(1-A*h)*(nd/n);
    dnc=nc*((1-S)-(1-A*h)*n/k-(A*h-S)*nc/k)/denom;
    dnd=nd*((1-A*h)-(1-S)*n/k+(A*h-S)*nd/k)/denom;
    Nc(i)=Nc(i-1)+dnc*dt;
    Nd(i)=Nd(i-1)+dnd*dt;
end; clearvars k
% Plot simulated and deterministic N
o=findobj(gca,'Type','Line',{'Color',[0,0,1],'-or','Color',[1,0,0]});
hold on; plot(o(1).XData,o(1).YData+o(2).YData,'-','Color',[0 .75 0],'LineWidth',0.5);
plot(tpoints,Nd+Nc,'-','Color',[0 .75 0],'LineWidth',2);
% Plot deterministic Nc and Nd.
plot(tpoints,Nc,'b-','LineWidth',2); plot(tpoints,Nd,'r-','LineWidth',2);
% Set x-axis to zero.
xlim(xLim); ylim(FigData.ylim);
x0=min(xlim); o=findobj(gca); 
arrayfun(@(x)set(x,'XData',x.XData-x0),o(2:end),'un',0);
o(1).XLim=o(1).XLim-min(o(1).XLim); 
% Aestetics
yticks([0,100,1000])
xticks(0:10:max(xlim)); xlabel('Time (a.u.)'); %xticklabels({});
text(0.46,0.7,'K','HorizontalAlignment','right','VerticalAlignment','middle','Color','k','FontWeight','Bold','FontSize',axisfontsize,'Units','Normalized')
text(0.17,0.935,'N','Color',[0 .75 0],'HorizontalAlignment','left','VerticalAlignment','middle','FontWeight','Bold','FontSize',axisfontsize,'Units','Normalized')
text(0.08,0.46,'N_S','Color',[1 0 0],'HorizontalAlignment','center','VerticalAlignment','middle','FontWeight','Bold','FontSize',axisfontsize,'Units','Normalized')
text(0.08,0.25,'N_R','Color',[0 0 1],'HorizontalAlignment','center','VerticalAlignment','middle','FontWeight','Bold','FontSize',axisfontsize,'Units','Normalized')
%text(0.2,0.075,'N_{th}','Color',[0 0 1],'LineStyle',':','HorizontalAlignment','left','VerticalAlignment','middle','FontSize',axisfontsize,'Units','Normalized')
set(gca,'Fontsize',axisfontsize)
text(gca,-.05,1.05,'(c)','Units','Normalized','HorizontalAlignment','right',...
        'VerticalAlignment','bottom','Color',[0,0,0],'FontWeight','Bold','FontSize',panelletterfontsize)
text(gca,-1.3,1.05,'(b)','Units','Normalized','HorizontalAlignment','right',...
        'VerticalAlignment','bottom','Color',[0,0,0],'FontWeight','Bold','FontSize',panelletterfontsize)
text(gca,-2.55,1.05,'(a)','Units','Normalized','HorizontalAlignment','right',...
        'VerticalAlignment','bottom','Color',[0,0,0],'FontWeight','Bold','FontSize',panelletterfontsize)
%%
sub(4)=subplot(2,3,6); %sub(4)=subplot(2,2,4);
plot(tpoints([1 end]),x_th*[1 1],'k--');
hold on; plot(FigData.Nc(:,1),FigData.Nc(:,2)./(FigData.Nc(:,2)+FigData.Nd(:,2)),'b-');
plot(tpoints([1 end]),Nc([1 end])./(Nc([1 end])+Nd([1 end])),'b-','LineWidth',2);
ylim([.25 .5]);
% Set x-axis to zero.
yTicks=sort(cat(2,ylim,x_th)); yticks(yTicks); yticklabels(cellfun(@(x)round(x,2),num2cell(yTicks),'un',0));
xlim(xLim);
x0=min(xlim); o=findobj(gca); 
arrayfun(@(x)set(x,'XData',x.XData-x0),o(2:end),'un',0);
o(1).XLim=o(1).XLim-min(o(1).XLim);
% Aesthetics
box off
xticks(0:10:max(xlim)); xlabel('Time (a.u.)');
text(0.3,0.8,'\bf $x\equiv\frac{N_R}{N}$','Color',[0 0 1],'HorizontalAlignment','center','VerticalAlignment','middle','FontSize',1.2*axisfontsize,'Units','Normalized','Interpreter','Latex')
%text(0.2,0.25,'$\frac{N_{th}}{K_+}$','Color',[0 0 0],'HorizontalAlignment','center','VerticalAlignment','middle','FontSize',axisfontsize,'Units','Normalized','Interpreter','Latex')
%text(0.9,0.85,'$\frac{N_{th}}{K_-}$','Color',[0 0 0],'HorizontalAlignment','center','VerticalAlignment','middle','FontSize',axisfontsize,'Units','Normalized','Interpreter','Latex')
set(gca,'Fontsize',axisfontsize)
text(gca,-.05,1.05,'(d)','Units','Normalized','HorizontalAlignment','right',...
        'VerticalAlignment','bottom','Color',[0,0,0],'FontWeight','Bold','FontSize',panelletterfontsize)
%% Margins
%set(f,'PaperPositionMode','auto','PaperOrientation','landscape','position',[77.8, 80.2, 1356.8, 380]);
fig_pos = f.PaperPosition;
set(f,'PaperSize',[0.79*fig_pos(3) 0.955*fig_pos(4)]);
f.PaperPosition(1:2) = f.PaperPosition(1:2) + [-.8 +.02];
%% Save Figure
print(f,[figfolder figname ''],'-dpdf','-painters'); %'-painters' sets de render and saves it as vectorial.
%savefig(f,[figfolder figname '.fig'])
%saveas(f,[figfolder figname '.png'],'png')
%saveas(f,[figfolder figname '.pdf'],'pdf')