%%% MAKES A COBWEB PLOT FOR A LOGISTIC MAP 
% (REG with small modifications by PHH)
%
% iterates on map x_{n+1}=alpha x_{n} ( 1 - x_{n} )
%
% input alpha (0 < alpha < 4) and x_0 (0 < x_0 < 1) 
%
% compute trajectory
%alpha=3.5;          % parameter
alpha=input('alpha=')
%x0=0.13;          % Initial condition 
x0=input('x0=')
N=20;           % Number of iterations
x(1) = x0;    
for ic=1:N
 x(ic+1) = alpha*x(ic)*(1-x(ic)); 
end

% plot the map function and the line y=x
clf;
t = 0:0.01:1;
plot(t,alpha*(t.*(1-t))); hold on;
 axis('square'); axis([0 1 0 1]);
 set(gca,'XTick',(0:0.1:1),'YTick',(0:0.1:1))
 %grid on;

 fplot('1*y',[0 1],'r');

%%%%%% STEP 3: PLOT COBWEB
 line([x(1) x(1)],[0 x(2)],'Color','g');
 plot(x(1), x(1),'ko');pause
 for ic=1:N-1
     line([x(ic) x(ic+1)],[x(ic+1) x(ic+1)],'Color','g');pause  
     plot(x(ic+1), x(ic+1),'ko');pause
     line([x(ic+1) x(ic+1)],[x(ic+1) x(ic+2)],'Color','g');pause
 end
 line([x(N) x(N+1)],[x(N+1) x(N+1)],'Color','g')
 
   %%%%%% STEP 4: SIGN THE PLOT
 at=text(0.1,0.82,['\alpha=',num2str(alpha)]); set(at,'FontSize',12);
 pause;
 close;
 
  %%%%%% STEP 5: PLOT THE TIME SERIES
  n=1:N+1;
  plot(n-1,x(n));
  axis([0 N 0 1]);
  xlabel('n'); ylabel('x(n)');
  pause;
  close;
  
  
