clear all
%
% Plot cobweb graph for map x_(n+1)=f(x_n) 
% Logistic map f=4*lambda*x*(1-x) is used
%

lambda=.95;

n=40; % length of orbit
xorbit=zeros(1,n); % pre-allocate memory for orbit
xorbit(1)=.1; % initial point of orbit 

%%%%% Generate the oribt 
for i=2:n
    xorbit(i)=4*lambda*xorbit(i-1)*(1-xorbit(i-1));
end

A=[xorbit;xorbit];
A=A(1:end);
xcoord=A(1:end-1); % x-coordinate of the cobweb
ycoord=[0,A(3:end)]; % y-coordinate of the cobweb
    
figure(1)
hold off

plot(xcoord, ycoord)
hold on

%%%% Plot the graph of the map
x=0:.01:1;
f=4*lambda*x.*(1-x); % first iterate of f
plot(x,f,'r-','linewidth',1)

plot([0 1], [0 1], 'k--') % Add the diagonal line.

xlabel('x','fontsize',16)
ylabel('y','fontsize',16)

axis([0 1 0 1])