function values = cobweb(f,x0,n)
%COBWEB: MATLAB function M-file that takes as input an
%inline function f, an initial point x0, and a number of
%iterations n, and returns a sequence of values x(1) = x0, 
%x(2) = x1, ... x(n+1) = xn for the recursion x(n+1)=f(x(n)), 
%and plots the cobwebbing associated with this recursion.
%
x(1) = x0;
for k=2:n+1
    x(k)=f(x(k-1));
end
%Specify the interval of x values. The gap variable will ensure
%that no lines appear on the boundary of the figure.
a = min(x);
b = max(x);
gap = (b-a)/10;
%Plot the graphs of f(x) and g(x) = x
y=linspace(a-gap,b+gap,round((b-a)*25));
fvals = f(y);
plot(y,y,y,fvals);
%Plot the cobwebbing
hold on
for k=1:n
    plot([x(k) f(x(k))], [x(k+1) x(k+1)], '--');
    plot([x(k+1) x(k+1)], [x(k+1) f(x(k+1))], '--');
end
%Place a circle on the final point
plot(x(n+1),f(x(n+1)),'o','MarkerFaceColor','k','MarkerSize',5)
values = x;


