% Filename: LeslieFecundity.m
% M-file to
%      - Find the dominant eigenvalue and corresponding
%        eigenvector for an array of juvenile fecundities
%      - Print out a table of the results
%      - Print out results graphically

% Print out the header for the table
fprintf('Juvenile    Dominant       Equilibrium Structure    \n')
fprintf('Fecundity  Eigenvalue  Hatchlings  Juveniles  Adults\n')
fprintf('------------------------------------------------------\n')

% Array of juvenile fecudity values
f = [0.1 0.2 0.5 1.5 2 2.5 3 4];

% Start the loop
for i = 1:length(f) % loop thru length of f vector
   
    % Construct Leslie matrix with appropriate juvenile fecundity
    A = [   0   f(i) 3 ;
          0.5     0  0 ;
            0  0.25  0 ];
    
    % Get eigenvalues and eigenvectors
    [v,lambda] = eig(A);
    
    % Find dominant eigenvalue
    deig = abs(lambda(1,1));
    
    % Normalize eigenvector associated with dominant eigenvalue
    normv(:,i) = v(:,1)/sum(v(:,1));
        % This creates a matrix called normv in which the
        % i^th column corresponds to the dominant eigenvector
        % associated with the i^th juvenile fecundity value
        
    % Print these values
    fprintf('   %3.1f   ',f(i)) % juvenile fecundity value
    fprintf('     %5.3f   ',deig) % dominant eigenvalue
    fprintf('   %5.3f  ',normv(1,i)) % hatchling proprotion at eq
    fprintf('     %5.3f  ',normv(2,i)) % juvenile proprotion at eq
    fprintf('   %5.3f  ',normv(3,i)) % adult proprotion at eq
    fprintf('\n') % new line    
end

% Generate graph
h = normv(1,:);  % vector of hatchling proportions at eq
j = normv(2,:);  % vector of juvenile proportions at eq
a = normv(3,:);  % vector of adult proprotions at eq
plot(f,h,'r.-',f,j,'g.-',f,a,'b.-')
legend('hatchlings','juveniles','adults')
xlabel('Juvenile Fecundity')
ylabel('Equilibrium Structure')