I keep getting this error on my code, I would really appreciate any help.

% Define birth rates and survival rates
B = [0.05, 0.03, 0.02, 0.01, 0, 0, 0, 0, 0, 0, 0, 0];
S = [0.9, 0.93, 0.95, 0.96, 0.97, 0.98, 0.99, 0.99, 0.98, 0.97, 0.95, 0.93];
% Construct the Leslie matrix
L = diag(B);
L(1,2:12) = S(1:11);
L(2:12,1:11) = diag(S(1:11), -1);
Unable to perform assignment because the size of the left side is 11-by-11 and the size of the right side is 12-by-12.
L(12,12) = S(12); % add last entry for S
% Define initial population vector
P = zeros(12, 10);
P(:,1) = [0.15, 0.1, 0.08, 0.07, 0.08, 0.1, 0.09, 0.08, 0.07, 0.05, 0.04]' * 100000;
% Simulate population growth over 10-year period
for t = 2:10
P(:,t) = L * P(:,t-1);
end
% Plot population distribution over time
age_groups = {'0-4', '5-9', '10-14', '15-19', '20-24', '25-29', '30-34', '35-39', '40-44', '45-49', '50-54', '55-59'};
figure;
bar(P');
legend(age_groups);
xlabel('Time (years)');
ylabel('Population size');
title('Population distribution over time');
Unable to perform assignment because the size of the left side is 11-by-11 and the size of the right side is
12-by-12.

1 Comment

I don't know what a Leslie matrix is. What do you think L should be? What size and what the elements should be in terms of the B and S matrices?

Sign in to comment.

Answers (1)

diag(S(1:11), -1)
When you diag(VECTOR, INDEX) then diag() wants to put all of the contents of VECTOR onto the diagonal indicated by the INDEX. That creates an array which is length(VECTOR) + abs(INDEX) rows and columns. 11 elements placed on diagonal 0 gives an 11 x 11. 11 elements placed on diagonal #-1 or #+1 requires a 12 x 12. 11 elements placed on diagonal #-2 or +2 requires a 13 x 13 matrix.
Your overall matrix that you are storing into is 12 x 12, but you are selecting an 11 x 11 subset of it to store values into.

Categories

Asked:

on 4 May 2023

Answered:

on 4 May 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!