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, eg 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).
Here is an example of drawing math vectors in Octave:
clf
xs=[0 0 1 5 0]
ys=[0 0 7 1 0]
xe=[5 1 5 1 6]
ye=[1 7 1 7 8]
q=1;
h=quiver(xs(q),ys(q),xe(q),ye(q), 0,'b');
hold on
set (h, "maxheadsize", 0.033);
q=3;
h=quiver(xs(q),ys(q),xe(q),ye(q), 0,'--b');
set (h, "maxheadsize", 0.033);
q=2;
h=quiver(xs(q),ys(q),xe(q),ye(q), 0,'r');
set (h, "maxheadsize", 0.033);
q=4;
h=quiver(xs(q),ys(q),xe(q),ye(q), 0,'--r');
set (h, "maxheadsize", 0.033);
q=5;
h=quiver(xs(q),ys(q),xe(q),ye(q), 0,'g');
set (h, "maxheadsize", 0.033);
axis("square")
grid on
hold off
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.