Repeating matrix multiplication linear system

3 ビュー (過去 30 日間)
Rick
Rick 2014 年 7 月 16 日
コメント済み: Rick 2014 年 7 月 16 日
Hello, I am trying to do a matrix multiplication with a loop, I have a matrix A
A =
0 0 45
1/25 0 0
0 39/100 3/10
and a vector
x =
2000
100
25
Well, I do the multiplication
A*x
ans =
1125
80
93/2
But then I want to do A*ans
A*ans
ans =
4185/2
45
903/20
And I want to repeat this 50 times. My problem that I am running into is when I write a for loop, then it will only select the element. I don't know how to set an instance of a vector, here is what I have tried so far.
e = 2000;
l = 100;
a = 25;
x = [e; l; a];
for t = 0:50
if t == 0
PopulationInfo = A*x
else
PopulationInfo = A*PopulationInfo;
end
But this can't go anywhere because if I do A*x(t) it will just be an element of x, not the entire vector. Also, I want to do a plot starting at t=0 (the initial e, l, and a are populations at t=0), but with this loop there is no 0th element of an array.
  2 件のコメント
Sara
Sara 2014 年 7 月 16 日
what do you mean "then it will only select the element"?
Rick
Rick 2014 年 7 月 16 日
編集済み: Rick 2014 年 7 月 16 日
e stands number for eggs, l stands for larva, and a is for adults. I want a plot of the number of eggs vs. t for t = 1:50, as well as number of adults and number of larva.
The formula is that e(t+1) = A*e(t), l(t+1) = A*l(t), and a(t+1) = A*a(t). The values for e,l, and a are at t=0.

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

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 16 日
編集済み: Azzi Abdelmalek 2014 年 7 月 16 日
A =[ 0 0 45
1/25 0 0
0 39/100 3/10]
e = 2000;
l = 100;
a = 25;
x = [e; l; a];
for t = 1:50
x(:,t+1) = A*x(:,t);
end
t = 0:1:50;
plot(t,x)
  2 件のコメント
Rick
Rick 2014 年 7 月 16 日
Thanks, but how do I plot the elements of populatonInfo vs. t?
Sara
Sara 2014 年 7 月 16 日
Using Joseph answer (below), you can do
t = 0:1:50;
plot(t,x(1,:),'r',t,x(2,:),'b',t,x(3,:),'g')

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

その他の回答 (2 件)

Joseph Cheng
Joseph Cheng 2014 年 7 月 16 日
to store each iteration of t.
e = 2000;
l = 100;
a = 25;
x = [e; l; a];
for t = 0:50
x(:,t+2) = A*x(:,t+1);
end

Rick
Rick 2014 年 7 月 16 日
Here is the code
e = 2000;
l = 100;
a = 25;
x = [e; l; a];
PopulationInfo = A*x;
for t = 1:50
PopulationInfo = A*PopulationInfo;
end
for t = 0:50
x(:,t+2) = A*x(:,t+1);
end
t = 0:1:50;
plot(t,x(1,:),'r',t,x(2,:),'b',t,x(3,:),'g');
But I get the error
Error using plot
Vectors must be the same lengths.
  4 件のコメント
Sara
Sara 2014 年 7 月 16 日
Azzi has modified his answer to include the plotting. What you don't like?
Rick
Rick 2014 年 7 月 16 日
didn't see the edit, thank u to Azzi and Sara and everyone here!

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by