Matrix Multiplication Using For Loop
이전 댓글 표시
I am trying to multiply a 4x4 matrix to a 4x1 vector. The code is supposed to calculate population after t number of years. I keep getting an error message saying that x1 is undefined. If possible, I would also like to declare it as a function and write another file that will call that function with any t that I input. Here is what I have..
function P = Population(t)
y(1) = 6250;
for i = 1:t
x = [x1; x2; x3; x4];
m = [0 0 0 9.55;
0.4 0 0 0;
0 0.15 0 0;
0 0 0.95 0];
P = m * x;
y(i) = sum(P)
end
답변 (1개)
James Tursa
2017년 3월 25일
편집: James Tursa
2017년 3월 25일
1 개 추천
The error message is pretty clear. You are attempting to build the x variable out of four other variables named x1, x2, x3, x4. None of these four other variable are defined. Where are they supposed to come from? The only variables that are defined at that point in your code are t, y, and i. Also I would point out that you are building up a y vector but not returning it as an output of your function, so y is thrown away.
댓글 수: 2
Jonathan Lee
2017년 3월 25일
편집: Jonathan Lee
2017년 3월 25일
James Tursa
2017년 3월 25일
Please post your current complete code for us to see.
카테고리
도움말 센터 및 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!