%%% MAKES A COB WEB PLOT FOR A LOGISTIC MAP

%%%% STEP 1: PLOT THE MAP FUNCTION AND THE LINE y=x
 fplot('(-y^3+3*y)',[-2 2]); hold on;
 axis([-2 2 -2 2]);
 grid off;

 fplot('1*y',[-2 2],'b');

 
%%%%%% STEP 2: COMPUTE TRAJECTORY 
a=3;b=-1;          
x0=0.2; % Initial condition
plot(x0,0,'k*')
hold on
N=100;           % Number of iterations
x(1) = x0;    
for ic=1:N
 x(ic+1) = (-x(ic)^3+3*x(ic)); 
end

%%%%%% STEP 3: PLOT COBWEB
 line([x(1) x(1)],[0 x(2)],'Color','r') 
  for ic=1:N-1
  line([x(ic) x(ic+1)],[x(ic+1) x(ic+1)],'Color','r')  
  line([x(ic+1) x(ic+1)],[x(ic+1) x(ic+2)],'Color','r')
  plot(x(ic+1), x(ic+1),'ko');
 end
 line([x(N) x(N+1)],[x(N+1) x(N+1)],'Color','r') 
 
 %%%%%% STEP 4: SIGN THE PLOT
  a=3;b=-1;          
z0=0.2; % Initial condition
plot(z0,0,'k*')
hold on
N=100;           % Number of iterations
z(1) = z0;    
for ic=1:N
 z(ic+1) = (z(ic)^3+3*z(ic)); 
end

%%%%%% STEP 3: PLOT COBWEB
 line([z(1) z(1)],[0 z(2)],'Color','r') 
  for ic=1:N-1
  line([z(ic) z(ic+1)],[z(ic+1) z(ic+1)],'Color','r')  
  line([z(ic+1) z(ic+1)],[z(ic+1) z(ic+2)],'Color','r')
  plot(z(ic+1), z(ic+1),'ko');
 end
 line([z(N) z(N+1)],[z(N+1) z(N+1)],'Color','r') 
 

 
 figure
 plot(x)
 hold on 
 plot(z)
 hold off