I want to make a program that adds two vectors and plots them together with their addition. But they have to be position vectors.
I tried creating v1=[0;2]
and v2=[1;3]
but displaying them with plot
results in a number of scattered points.
How can I specify the starting position of the vectors, e.g. make v1
and v2
start from the origin?
I usually do what you are describing with the quiver
function, whose purpose is to draw vector fields. Your example could be drawn with
xx=[0;2];
yy=[0;0];
quiver(xx,yy,[0;1],[2;3],0,"linewidth",4);axis equal;xlim([-4 4]);ylim([0 5]);grid on;
It produces the following output:
The starting points of the vectors are specified by the two variables xx
and yy
in the following way: the starting point of the n-th
vector is given by [xx(n,1);yy(n,1)]
(look at help meshgrid
for information on that. Basically, meshgrids are just a way to define the domain of a function, which here happens to be a vector field).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With