% script to try the Leslie matrix example of age-structured populations

% define our Leslie matrix
A=[0.,2.,1.5,0.;0.4,0.,0.,0.;0.,0.3,0.,0.;0.,0.,0.1,0.]
% compute eigenvalues
eigenvalues=eig(A);
% check magnitudes
abseigenvalues=abs(eigenvalues)

% load initial population
N=[1000.;200.;100;10]

% apply matrix repeatedly and save the results
for i=1:30
   nextyear=A*N(:,i);
   N=[N,nextyear];
end

%%%% analyze results

% compute total population for each time
totals = sum(N);
% extract each age for plotting
babies = N(1,:);
twos = N(2,:);
threes = N(3,:);
fours = N(4,:);

% OPTIONAL: convert to age distribution
if 0
  babies=babies./totals;
  twos = twos./totals; 
  threes = threes./totals; 
  fours =fours./totals;
  totals =totals./totals;
end

%plot
t=0:1:size(N,2)-1;
plot(t,babies,t,twos,t,threes,t,fours,t,totals)

% try longer time
% try increasing birth rates or survival rates
% try other leslie examples

