Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I draw vectors in octave?

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?

like image 809
Zack Per Avatar asked Sep 18 '25 10:09

Zack Per


1 Answers

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:

enter image description here

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

like image 189
Ash Avatar answered Sep 21 '25 04:09

Ash