# Plot vectors 2D! With Matlab!

• MATLAB

## Main Question or Discussion Point

Hi... I have four vectors that I need together in one plot and also they should start at (2,2)

Vector A is a=[5 0]
B is b=[0 2]
I obtained unit vector and I need something similar to http://www.scribd.com/doc/13353344/Fundamentals-of-Electromagnetics [Broken] at page nine

I know the following commands

pi=[2,2];
plot( [pi(1),pi(1)+v(1)] , [pi(2),pi(2)+v(2)])

It works but not with four vectors. What should I do?

I'm not pretending you to do my homework so that I'm asking for the plot

Last edited by a moderator:

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
marcusl
Gold Member
Hi... I have four vectors that I need together in one plot and also they should start at (2,2)

Vector A is a=[5 0]
B is b=[0 2]
I obtained unit vector and I need something similar to http://www.scribd.com/doc/13353344/Fundamentals-of-Electromagnetics [Broken] at page nine

I know the following commands

pi=[2,2];
plot( [pi(1),pi(1)+v(1)] , [pi(2),pi(2)+v(2)])

It works but not with four vectors. What should I do?

I'm not pretending you to do my homework so that I'm asking for the plot
First a comment on Matlab style: It's generally a poor idea to reassign built-in Matlab function or variable names such as you did in the first line. In Matlab, pi is preassigned the value 3.14159...

On to your question: use the command quiver to plot vectors. The arguments are passed in the form of matrices specifying the x and y starting points and the u and v vector components. For your two vectors a and b:

x = meshgrid([2,2]);
y = meshgrid([2,2]);
u = meshgrid([5,0]);
v = meshgrid([0,2]);
quiver(x,y,u,v)

Add columns to plot more vectors, e.g., u = meshgrid([5,0,14,22,...])
Matlab renders the arrow a little shorter than you expect, for visual clarity, but you can rescale if desired. REad the help page for quiver.

Last edited by a moderator: