0

So I have this matrix A = [1.9, 0.025; 0.1, 1.225]; And I want to multiply it with the vector v = [1;0];

I want to plot the sum of it up to 25 iteration, so I have a for loop and I use the sum function like this:

iter = 25;
v = [1;0];

for k = 1:iter
    s(k) = sum(A^k*v);
    hold on
    plot(k,s(k))
end

But the plot shows up empty, how would I go about doing this? This is for general understanding of plots with matrices and arrays and for loops, not really a school assignment.

EdG
  • 1,596
Corvo
  • 61
  • What is A? You haven't defined it. If you do, it should plot the points. – Javi Mar 21 '18 at 08:26
  • You are raising to the 25th power every time. Should that be sum(A^k*v) – Empy2 Mar 21 '18 at 08:41
  • Now it is correct. I wanted to raise it by the amount of iteration and plot the resulting sum against the iteration k. – Corvo Mar 21 '18 at 08:42

3 Answers3

0

you are multiplying a 2*2 Matrix A with a 2*1 vector and then you are summing up the values of the resulting vector, so you have a scalar.

What do you expect to see when you plot a single Point? A BIG DOT? then you must wait Long..

0

It is corrected now

iter = 25;  
v = [1;0];

for k = 1:iter

  s(k) = sum(A^k*v);
  hold on
  plot(k,s(k))
end

Like this, but still no valid plot.

Corvo
  • 61
0

Because you are plotting points, you don't see something. If you add something like 'o' to the plot command (e.g., plot(k,s(k),'o')), you should see something.

BTW, generally speaking, it would be better to do the plot outside the loop, e.g.:

iter = 25;  
v = [1; 0];
A = [1.9, 0.025; 0.1, 1.225];

s = zeros(25, 1); % Initialize vector to prevent growing vector inside loop
for k = 1:iter
    v = A*v;
    s(k) = sum(v);
end
plot(s, '-o')

Plot generated by the matlab code

EdG
  • 1,596