Multiply matrix with previous result

4 views (last 30 days)
Hey,
I'm looking to make function that can oversee changes in population. The population is set by having 3 different ages that have different survival rates and possibilities to have children etc. Basicly it goes like this: M*x=b, where M is set propabilities of change for next year, x is the current population number by three groups, and b is the answer for next years number of children, young adults and adults.
M = [0 0 2; 0.25 0 0; 0 0.52 0.73]
x_k = [24; 15; 28]
Now I need to make a loop that multiplys the first population number (x_k) with M, and after that M with previous result.
b = M*x_k
b_2 = M*b
b_3 = M*b_2
etc... until a set amout of years has passed, example 10 years for n = 10
x_k(1,1,1) = [24; 15; 28]
for n=1:10
x(n) = M*x_k(n); %filter
x_k(n+1)=x(n)
end
How could I fix this for loop to get to my desired results? I know my first problem is with x_k(1) since im trying to shove 3 numbers to one variable, but still have trouble typing it differently. Thank you!

Accepted Answer

Steven Lord
Steven Lord on 25 Sep 2021
Use a matrix to store the population at each year, not a vector.
M = [0 0 2; 0.25 0 0; 0 0.52 0.73];
M = 3×3
0 0 2.0000 0.2500 0 0 0 0.5200 0.7300
x_k = [24; 15; 28];
x_k = 3×1
24 15 28
population = zeros(size(x_k, 1), 20); % each column holds the population for a year
population(:, 1) = x_k;
for year = 2:20
population(:, year) = M*population(:, year-1);
end
populationAtYear20 = population(:, 20)
populationAtYear20 = 3×1
45.2917 11.3976 22.4973

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!