clear all;
close all;
r = input('Enter the bifurcation parameter r (0 < r < 4): ');
figure(1);
axis([0 1 0 1]); hold on;
s = 0 : 0.01 : 1; % a mesh for the unit interval of x
plot(s,s,'g'); % plotting the diagonal y = x
y = r*s.*(1-s);
plot(s,y,'b') % plotting the discrete map: y = r*x*(1-x)
x = input('Enter the starting value x1 (0 < x1 < 1): ');
n = input('Enter the total number of iterations in a sequence n (0 < n < 50): ');
for k = 1:n
    x(k+1) = r*x(k)*(1 - x(k)); % automatic extension of the vector x from scalar
end
plot([x(1), x(1)],[x(1),x(2)],'r'); % the first vertical segment of the picture
for k = 2 : n
    plot([x(k-1),x(k)],[x(k),x(k)],'r'); % a horizonal segment of the picture
    plot([x(k),x(k)],[x(k),x(k+1)],'r'); % a vertical segment of the picture
    pause(1); % intermediate pause to follow the cobwebbing over time
end
title('Cobwebbing in the discrete logistic map');
xlabel('x_k'); ylabel('x_{k+1}'); hold off;
