
% Showing the conjugacy between TENT and LOGISTIC map
% Ricardo Carretero Mar/03

set(groot,'defaulttextinterpreter','latex');
set(groot,'defaultAxesTickLabelInterpreter','latex');
set(groot,'defaultLegendInterpreter','latex');
set(groot,'defaultAxesFontSize',14);

tent = inline('[(x<=0.5).*2.*x+(x>0.5).*2.*(1-x)]','x');
mod2 = inline('[mod(2*x,1)+(x==0.5)]','x');
logistic = inline('[4*x.*(1-x)]','x');
C = inline('[0.5*(1-cos(pi*x))]','x');

Npts=200;
Nit=60; % 60 % use Nit=60 to show loss of digits
x=linspace(0,1,Npts);
x1=linspace(0,0.5);
x2=linspace(0.5+1e-10,1-1e-10,10);
fs=18;
lw=2;
ms=8;
xt0=2/3;		% fixed pt
xt0=0.4;		% period-2
xt0=pi/30;		% chaotic orbit
xt0=0.01+rand(1,1)/1.1;	% random IC
xt0=rand(1,1);		% random IC
xt0

figure(1)
clf

subplot(2,2,1)
set(gca,'FontSize',[fs]);
plot(x1,tent(x1),'b','LineWidth',lw)
hold on
plot(x2,tent(x2),'b','LineWidth',lw)
plot(x,x,'k')

subplot(2,2,2)
set(gca,'FontSize',[fs]);
plot(x,logistic(x),'b','LineWidth',lw)
hold on
plot(x,x,'k')

subplot(2,2,3)
set(gca,'FontSize',[fs]);
subplot(2,2,4)
set(gca,'FontSize',[fs]);

Xt=[xt0];
Yt=[0];

xl0 = C(xt0);
xc0 = xl0;
Xl=[xl0];
Yl=[0];
Xc=[xc0];
Yc=[0];

xto=xt0;
xlo=xl0;
xco=xc0;

fprintf('Press any key...\n');
fprintf('xtent(%2.2d)=%1.52f\n',0,xto);
for i=1:Nit
 xtn=tent(xto);
 xln=logistic(xlo);
 %fprintf('xtent(%2.2d)=%1.52f\n',i,xtn);
 %%fprintf('xlog.(%2.2d)=%1.52f\n',i,xln);
 xcn=C(xtn);
 Xt=[Xt xto xtn];
 Yt=[Yt xtn xtn];
 Xl=[Xl xlo xln];
 Yl=[Yl xln xln];
 Xc=[Xc xco xcn];
 Yc=[Yc xcn xcn];
 xto=xtn;
 fprintf('xtent(%2.2d)=%1.52f\n',i,xto);
 xlo=xln;
 xco=xcn;
 subplot(2,2,1)
 plot(Xt(end-2:end),Yt(end-2:end),'r','LineWidth',lw)
 xlabel('$x_n$'); ylabel('$x_{n+1}$');
 subplot(2,2,2)
 plot(Xl(end-2:end),Yl(end-2:end),'r','LineWidth',lw)
 plot(Xc(end-2:end),Yc(end-2:end),'k','LineWidth',lw)
 xlabel('$y_n$'); ylabel('$y_{n+1}$');
 legend('','','log','C(tent)','Location','NW')
 subplot(2,2,3)
 plot((1:(length(Xt)+1)/2)-1,Xt(1:2:length(Xt)),'b-',(1:(length(Xt)+1)/2)-1,Xt(1:2:length(Xt)),'ro','MarkerFaceColor','r')
 ax=axis;ax(4)=1;axis(ax); if(ax(2)>Nit) ax(2)=Nit;axis(ax);end;
 xlabel('$n$'); ylabel('$x_n$');
 subplot(2,2,4)
 plot((1:(length(Xl)+1)/2)-1,Xl(1:2:length(Xl)),'b-')
 hold on
 plot((1:(length(Xl)+1)/2)-1,Xl(1:2:length(Xl)),'ko','MarkerFaceColor','w','LineWidth',lw)
 plot((1:(length(Xc)+1)/2)-1,Xc(1:2:length(Xc)),'rx','MarkerSize',ms,'LineWidth',lw)
 hold off
 ax=axis;ax(4)=1;axis(ax); if(ax(2)>Nit) ax(2)=Nit;axis(ax);end;
 xlabel('$n$'); ylabel('$y_n$ \& $C(x_n)$');
 if(i<Nit) pause;end
end


