% chinook popluation evolution over 24 years

% initial age distribution
x0=[1000;1000;1000];

% projection matrix
M=[0 0 2000; 0.005 0 0; 0 .10 0];
% M = [0 0 0; 1 0 0; 0 1 0];
% M=[0 0 1000; 0.005 0 0; 0 .10 0];

% % optional: Inspect M
% % set format so that Matlab uses the best of fixed or floating point
%format short g 
%M

% suppose we want to calculate population size and age distribution for the
% next 24 years

% 1) initialize an array that can hold 25 age distribution vectors (initial
%    distribution + 24 years)
X=zeros(3,25);

% 2) place the initial age distribution vector in the first column of X
X(:,1) = x0;

% 3) Calculate the second through twenty-fourth column of X by iterating
%    the equation x(k) = M x(k-1) for k from two through twenty four
for k=2:25
	X(:,k)=M*X(:,k-1);
end

% 4) plot results
% create 3-by-25 array with counter of years (i.e. 0-24 in each row)
Y=repmat(0:24,3,1);
figure
% 4.1) plot number of salmon in the three age classes over time
subplot(2,1,1)
plot(Y', X','LineWidth',3)
xlabel('time in years')
ylabel(sprintf('number of individuals\nper age class'))
legend('Juveniles','Subadults','Adults')
% 4.2) repeat but with logarithmic scale on the y-axis
subplot(2,1,2)
semilogy(Y', X','LineWidth',3)
xlabel('time in years')
ylabel(sprintf('number of individuals\nper age class'))
legend('Juveniles','Subadults','Adults')

print(gcf,'-dpng','population_timeseries_1')

% 4.3) plot age distribution in years 0:7
figure
for i=1:8
    subplot(2,4,i)
    set(gca,'FontSize',14)
    barh(log10(X(:,i)));
    set(gca,'YTickLabel',{'Y1' 'Y2' 'Y3'})
    xlabel('log10(indiv.)')
    title(['Y' num2str(i-1)])
    axis([-1 6 0.5 3.5])
end

print(gcf,'-dpng','population_histogram_1')
