% Filename: LesliePopGrowthPlot.m
% M-file to
%  show population growth rate as total individuals and as per 
%  capita rate over time for a Leslie population projection 

% Enter the Leslie matrix
P = [ 0 1 20;  .2 0  0;  0 .5  0];
  
% Enter the initial population structure0
x0 = [ 10 ; 0 ; 0 ];

% Population size at time 0
s0=sum(x0)

%  N is the time period to run

N=100

% We will create a matrix x that has three columns.
% Each column will contain time series data for one age class.
% Each row will correspond to a time step (t=0 to t=50) 
x = zeros(N+1,3);

x(1,:) = x0;       % Data for time step t = 0

% Use a for loop to generate times series data

for t = 1:N
   x(t+1,:) = P^t * x0;  % Data for time step t
end

% Time series information for population growth rate 
% is obtained by another for loop put total population 
%  in first column of matrix y and per capita growth rate 
% in second column

y=zeros(N,2)

for tt = 1:N
   y(tt,1) = sum(x(tt+1,:))-sum(x(tt,:));
   y(tt,2) = y(tt,1)/sum(x(tt,:));
end
 
% Generate plots
time = [1:N];
plot(time,y(:,1),'r-')
xlabel('Time step t')
ylabel('Total Population Change')
figure
plot(time,y(:,2),'r-')
xlabel('Time step t')
ylabel('Per capita Population growth')

% Compare the last per capita rate to eigenvalues

eig(P)
y(N,2)



