Multiply matrix with previous result
조회 수: 3 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
Steven Lord
2021년 9월 25일
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];
x_k = [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)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!