Back to main page
Back to draw contents

Vectors

This section shows how to draw vectors in 2d and 3d.


Vectors in 2d.

draw2d(xrange   = [-10,50],
       yrange   = [-10,50],
       color    = turquoise,
       terminal = jpg,
          vector([1,1],[10,20]),
       head_both   = true,
       head_length = 5,
       line_width  = 3,
       color       = yellow,
       head_angle  = 20,
          vector([40,5],[-30,30]),
       line_type = dots,
       color     = "midnight-blue",
       head_both = false,
          vector([40,40],[-30,-40]))$

A vector field is plotted as a set of vectors.

/* vector origins are {(x,y)| x,y=1,...,10}  */
coord: setify(makelist(k,k,1,10))$
points2d: listify(cartesian_product(coord,coord))$

/* compute vectors at the given points  */
vf2d(x,y):= vector([x,y],2*[sin(x),cos(y)])$
vect2: makelist(vf2d(k[1],k[2]),k,points2d)$

/* draw the vector field */
apply(draw2d,
      append([head_length=0.1, color=blue],
             vect2))$
/* now, unit vectors are plotted */
apply(draw2d,
      append([head_length=0.1, color=blue, unit_vectors=true],
             vect2))$

A vector field in 3d.

/* vector origins are {(x,y,z)| x,y=1,...,5}  */
coord: setify(makelist(k,k,1,5))$
points3d: listify(cartesian_product(coord,coord,coord))$

/* compute vectors at the given points  */
vf3d(x,y,z):= vector([x,y,z],2*[sin(x),cos(y),log(z)])$
vect3: makelist(vf3d(k[1],k[2],k[3]),k,points3d)$

/* draw the vector field */
apply(draw3d,
      append([head_length=0.1, color=blue],
             vect3))$
/* now, unit vectors are plotted */
apply(draw3d,
      append([head_length=0.1, color=blue, unit_vectors=true],
             vect3))$

Back to main page
Back to draw contents


by Mario Rodríguez Riotorto