How would i go about plotting this in matlab

61 Views Asked by At

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.

3

There are 3 best solutions below

0
On

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..

1
On

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.

0
On

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