% Example of matrix population modeling.
% Population is divided into 15-year age brackets.
 
% set up a population matrix, A
% A(1,j)   = number of female children born to each woman 
%            in the jth age bracket in 15 years.
% A(j+1,j) = probability that members of the jth age bracket 
%            survive the next 15 years
% A(5,5)   = probability that members of the last age bracket 
%            survive the next 15 years
 
 A = [  .5   .5  .5    .5 
       .95   0    0     0  
       0    .90  0     0 
       0    0    .70   0]  
 
% initial female population, p0, is uniformly distributed
 p0 = [25;25;25;25];
 
% The jth column of the matrix P will contain the
% female population vector at year 2000+(j-1)*15.
 P = [p0]; pj = p0;
 
% iterate for numyears*15 years
 numyears = 33;
 for j=1:numyears
    pj = A*pj;     % compute population at year 2000+j*15
    P  = [P pj];   % store information in column j+1 of P
 end
 
% plot data as a bar chart
 figure(1), clf
 bar(2000+[0:numyears]*15, P','stacked')
 shading faceted, colormap(cool)
 xlabel('year', 'fontsize', 18,'interpreter','latex')
 ylabel('population', 'fontsize',18,'interpreter','latex')
 legend('0-14','15-29','30-44','45+')
 leg=legend('0-14','15-29','30-44','45+','location','northoutside','orientation','horizontal');
 set(leg,'fontsize',14,'interpreter','latex')
