% Stability of fixed points and long term behavior in 
% discrete single species population dynamics models.

function bifurcation

init = .5;
iter_wait = 300; 
iter_record = 400;

 range = 5:.005:6.5;
 f = @(x,r) r*x^2*(1-x);
 fp = @(x,r) r*x*(2-3*x);

% range = 0:.02:4;
% f = @(x,r) r*x*(1-x);
% fp = @(x,r) r*(1-2*x);

%  range = 1:.01:4;
%  f = @(x,r) x*exp(r*(1-x));
%  fp = @(x,r) (1-r*x)*exp(r*(1-x));


figure;
for r = range
  n(1) = init;
  for i = 1:iter_wait
    n(i+1) = f(n(i),r);
  end
  for i = (iter_wait+1):iter_record
    n(i+1) = f(n(i),r);
    plot(r,n(i),'r.'); hold on;
  end
end

figure;
ctr = 0;
for r = range
  liap = 0;
  n(1) = init;
  for i = 1:iter_record
    n(i+1) = f(n(i),r);
    liap = liap + log(abs(fp(n(i),r))+1e-5);
  end
  ctr = ctr + 1;
  liapunov(ctr) = liap / iter_record;
end
plot(range,liapunov,'-');
hold on;
plot(range,zeros(size(range)),'k');

