% cobweb.m
% This script plots a time series
%       
% Inputs (these can be modified):
%   F is the map to be iterated
%   n is the number of times to iterate F
%   x0 is the initial point in the orbit
%   a, b are endpoints of the domain of F
% Outputs:
%   a plot of the cobweb diagram
F = @(x) 4.*x.*(1-x);
n = 200;
x0 = rand(1);
a = 0;
b = 1;
%%%%%%%%%%%%%%%%%%%%%
% Main code (does not need to be modified)
%%%%%%%%%%%%%%%%%%%%%
t = linspace(a,b,100);
figure(1);
clf();
hold on;
% Plot y = F(x) and y = x
plot(t,F(t),'k');
plot(t,t,'k:');
x = x0;
y = F(x0);
% Plot iterates
%plot([x,x], [0,y], 'b', 'linewidth', 2);
for i = 1:n
    Fy = F(y);
    plot([x,y], [y,y], 'b', 'linewidth', 2);
    plot([y,y], [y,Fy], 'b', 'linewidth',2);
    x = y;
    y = Fy;
end
% axis('equal')
grid on