MATLAB: Vectors must be the same length (leslie matrix)

errormatrixplotvectors

I was asked to do this: Change the adult bison fecundity to 0.2, 0.42, 1.0, and 1.4. Create one graph that has adult fecundity along the horizontal axis and equilibrium population structure along the vertical axis with a data set plotted for each population class (calves, yearlings, and adults).
Here's the code:
% 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('Adult Dominant Equilibrium Structure \n')
fprintf('Fecundity Eigenvalue Calves Yearlings Adults\n')
fprintf('------------------------------------------------------\n')
% Array of adult fecudity values
f = [0.2, 0.42, 1 1.4];
% Start the loop
for i = 1:length(f) % loop thru length of f vector
% Construct Leslie matrix with appropriate adult fecundity
A = [ 0 0 f(i) ;
0.6 0 0 ;
0 0.75 0.95 ];
% Get eigenvalues and eigenvectors
[v,lambda] = eig(A);
% Find dominant eigenvalue in lambda
% and make note of position in lambda matrix
lambdavector = max(lambda); % array of eigenvalues
for j = 1:length(lambdavector)
if max(max(abs(lambda))) == abs(lambdavector(j))
loc = j; % position of dom eig in lambda matrix
deig = lambda(j,j); % dominant eigenvalue

end
end
% Normalize eigenvector associated with dominant eigenvalue
normv(:,i) = v(:,loc)/sum(v(:,loc));
% This creates a matrix called normv in which the
% iˆth column corresponds to the dominant eigenvector
% Print these values
fprintf(' %3.1f ',f(i)) % adult fecundity value
fprintf(' %5.3f ',deig) % dominant eigenvalue
fprintf(' %5.3f ',normv(1,i)) % calf proprotion at eq
fprintf(' %5.3f ',normv(2,i)) % yearling proprotion at eq
fprintf(' %5.3f ',normv(3,i)) % adult proprotion at eq
fprintf('\n') % new line
end
% Generate graph
c = normv(1,:); % vector of calf proportions at eq
y = normv(2,:); % vector of yearling proportions at eq
a = normv(3,:); % vector of adult proprotions at eq
plot(f,c,'r.-',f,y,'g.-',f,a,'b.-')
legend('calves','yearlings','adults')
xlabel('Adult Fecundity')
ylabel('Equilibrium Structure')

Best Answer

fprintf('Adult Dominant Equilibrium Structure \n')
fprintf('Fecundity Eigenvalue Calves Yearlings Adults\n')
fprintf('------------------------------------------------------\n')
f = [0.2, 0.42, 1 1.4];
% Start the loop
for i = 1:length(f) % loop thru length of f vector
% Construct Leslie matrix with appropriate adult fecundity
A = [ 0 0 f(i) ;
0.6 0 0 ;
0 0.75 0.95 ];
% Get eigenvalues and eigenvectors
[v,lambda] = eig(A);
% Find dominant eigenvalue in lambda % and make note of position in lambda matrix l
lambdavector = max(lambda); % array of eigenvalues
for j = 1:length(lambdavector)
if max(max(abs(lambda))) == abs(lambdavector(j))
loc = j; % position of dom eig in lambda matrix
deig = lambda(j,j); % dominant eigenvalue

end
end
% Normalize eigenvector associated with dominant eigenvalue
normv(:,i) = v(:,loc)/sum(v(:,loc)); % This creates a matrix called normv in which the
% iˆth column corresponds to the dominant eigenvector
% Print these values
fprintf(' %3.1f ',f(i)) % adult fecundity value
fprintf(' %5.3f ',deig) % dominant eigenvalue
fprintf(' %5.3f ',normv(1,i)) % calf proprotion at eq
fprintf(' %5.3f ',normv(2,i)) % yearling proprotion at eq
fprintf(' %5.3f ',normv(3,i)) % adult proprotion at eq
fprintf('\n') % new line
end
% Generate graph
c = normv(1,:); % vector of calf proportions at eq
y = normv(2,:); % vector of yearling proportions at eq
a = normv(3,:); % vector of adult proprotions at eq
plot(f,c,'r.-',f,y,'g.-',f,a,'b.-')
legend('calves','yearlings','adults')
xlabel('Adult Fecundity')
ylabel('Equilibrium Structure')