%Lesson 5 - Math 151 
%This is to illustrate eigenvalues and 
%eigenvectors arising from a population
%projection model.
%Read in a command file that is
%this file of commands for MATLAB
%by first telling MATLAB to use the b: drive
%   !b:
%then telling it to run lesson5
%   lesson5
%where lesson5.m is a file on your 
%floppy in the b: drive
!c:
% P is the projection matrix and n0 is the
% initial population structure, ni is the population
% structure at time i, nitot is the total population size 
% at time i, ninorm is the normalized population structure
% at time i (sum of the elements of ninorm is 1)
P=[0 1 2; .5 0 0 ; 0 .6 0],pause
n0=[50;30;20],pause
n0tot=sum(n0)
n1=P*n0,pause
n1tot=sum(n1),pause
n1norm=n1/n1tot,pause
% growthi is the population growth rate from time i to i+1
growth0=(n1tot-n0tot)/n0tot,pause
% now compute the changes from time 10 to 11
n10=P^10*n0,pause
n10tot=sum(n10),pause
n10norm=n10/n10tot,pause
n11=P^11*n0,pause
n11tot=sum(n11),pause
n11norm=n11/n11tot,pause
growth10=(n11tot-n10tot)/n10tot,pause
% junk is a matrix whose first 2 rows are the normalized
% population structure, and third row is the difference
% and compi is a matrix whose first 2 columns are the 
% normalized population structure at times i to i+1 
% and last column is the difference between these 
junk=[n10norm';n11norm';(n11norm-n10norm)']
comp10=junk',pause
% now compute the changes from time 20 to time 21
n20=P^20*n0,pause
n20tot=sum(n20),pause
n20norm=n20/n20tot,pause
n21=P^21*n0,pause
n21tot=sum(n21),pause
n21norm=n21/n21tot,pause
growth20=(n21tot-n20tot)/n20tot,pause
junk=[n20norm';n21norm';(n21norm-n20norm)'],pause
comp2=junk',pause
% Now compute the eigenvalues and compare these
% to the population growth rate at time 20
[X,e]=eig(P),pause
growth20,pause
% Now get the eigenvector for the dominant eigenvalue, 
% normalize it to get ev1norm, and compare this to 
% the normalized population structure at time 21
ev1=X(:,1),pause
ev1norm=ev1/sum(ev1),pause
n21norm,pause

