views:

4073

answers:

4

I want to know the simplest way to plot vectors in MATLAB. For example:

a = [2 3 5];
b = [1 1 0];
c = a + b;

I want to visualize this vector addition as head-to-tail/parallelogram method. How do I plot these vectors with an arrow-head?

A: 

I'm not sure of a way to do this in 3D, but in 2D you can use the compass command.

rlbond
+2  A: 
a = [2 3 5];
b = [1 1 0];
c = a+b;

starts = zeros(3,3);
ends = [a;b;c];

quiver3(starts(:,1), starts(:,2), starts(:,3), ends(:,1), ends(:,2), ends(:,3))
axis equal
Amro
quiver and quiver3 leave blank spaces between vectors which make the vectors look like with small magnitude.
Aamir
Try adding the "scale" parameter, set to zero, to prevent automatic scaling, i.e. `quiver3(starts(:,1), starts(:,2), starts(:,3), ends(:,1), ends(:,2), ends(:,3), 0)`
Martin B
Thanks Martin for the scale argument. But the arrowheads in quiver3 doesn't look very nice as compared to arrow.m from Matlab File Exchage.
Aamir
+3  A: 

I found this arrow(start, end) function on MATLAB Central which is perfect for this purpose of drawing vectors with true magnitude and direction.

Aamir
+1  A: 

I agree with Aamir that the submission arrow.m from Erik Johnson on the MathWorks File Exchange is a very nice option. You can use it to illustrate the different methods of vector addition like so:

  • Tip-to-tail method:

    o = [0 0 0];  %# Origin
    a = [2 3 5];  %# Vector 1
    b = [1 1 0];  %# Vector 2
    c = a+b;      %# Resultant
    arrowStarts = [o; a; o];        %# Starting points for arrows
    arrowEnds = [a; c; c];          %# Ending points for arrows
    arrow(arrowStarts,arrowEnds);   %# Plot arrows
    
  • Parallelogram method:

    o = [0 0 0];  %# Origin
    a = [2 3 5];  %# Vector 1
    b = [1 1 0];  %# Vector 2
    c = a+b;      %# Resultant
    arrowStarts = [o; o; o];        %# Starting points for arrows
    arrowEnds = [a; b; c];          %# Ending points for arrows
    arrow(arrowStarts,arrowEnds);   %# Plot arrows
    hold on;
    lineX = [a(1) b(1); c(1) c(1)];  %# X data for lines
    lineY = [a(2) b(2); c(2) c(2)];  %# Y data for lines
    lineZ = [a(3) b(3); c(3) c(3)];  %# Z data for lines
    line(lineX,lineY,lineZ,'Color','k','LineStyle',':');  %# Plot lines
    
gnovice

related questions