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
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
Jonathan Lee 2017 年 3 月 25 日
編集済み: Jonathan Lee 2017 年 3 月 25 日
Thank you for the help!
These four variables are dependent on each other. x1 = # of polar bear cubs born x2 = # of cubs that survive 1st year x3 = # of cubs that survive 2nd year x4 = # of adult female cubs
I just defined x1,x2,x3 = 0 and x4 = 6250 The code now runs but I cannot tell if the answer is correct without seeing a graph.
I tried j = 0:5:t; k = 0:5:y;
plot(j,k)
but I am getting an error message for the 'k' variable. Did I set the parameters incorrectly?
James Tursa
James Tursa 2017 年 3 月 25 日
Please post your current complete code for us to see.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2017 年 3 月 25 日

コメント済み:

2017 年 3 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by