Visualizing vectors in 2D and 3DWorksheet by Mike May, S.J.- maymk@slu.eduEdited by Russell Blyth - blythrd@slu.eduIn this worksheet we use Maple to visualize vector concepts. First we want to load the appropriate commands. The commands for dot and cross product are in the Linear Algebra package. The commands for plotting vectors is in the plottools package. We also need the display command from the plots package. QypJKHJlc3RhcnRHJSpwcm90ZWN0ZWRHISIiLUkld2l0aEc2IjYjSS5MaW5lYXJBbGdlYnJhRzYkSShfc3lzbGliR0YoRiRGJS1GJzYjSSpwbG90dG9vbHNHNiRGJEYsRiUtRic2I0kmcGxvdHNHRjBGJQ==Visualizing vectors in R^2, simple operations:We define some two dimensional vectors. We will use a pair of vectors with numbers so we have a specific example and a pair of vectors with variables so we can see the general case.v1 := <1,2>; v2 := <3,-4>;
w1 := <a1, a2>; w2 := <b1, b2>;To add the vectors we use normal addition. Scalar multiplication also works."v1+v2" = v1+v2;
"w1+w2" = w1+w2;
"3*v1" = 3*v1;
"5*w2" = 3*w2;There are several ways to normalize a vector (that is, to produce a unit vector in the direction of the original vector).QyQqJkkjdjFHNiIiIiItSSVzcXJ0R0YlNiMtSTBkZWxheURvdFByb2R1Y3RHNiQlKnByb3RlY3RlZEcvSSttb2R1bGVuYW1lR0YlSSxUeXBlc2V0dGluZ0c2JEYtSShfc3lzbGliR0YlNiRGJEYkISIiRiY=v1/sqrt(v1.v1);Normalize(v1, Euclidean);Visualizing the sum of two vectors takes a bit more work. The following block of code shows the addition clearly.#a block of code to see two vectors and their sum
zerovec := <0,0>:
vec1 := <1,2>:
vec2 := <4,-3>:
vecsum := vec1+vec2;
plotv1 := arrow(zerovec, vec1, .2, .4, .1, color=red):
label1 := textplot([vec1[1]/2,vec1[2]/2, `vector 1`],
align={ABOVE,RIGHT}, font = [HELVETICA, BOLD, 12]):
plotv2 := arrow(zerovec, vec2, .2, .4, .1, color=green):
label2 := textplot([vec2[1]/2,vec2[2]/2, `vector 2`],
align={ABOVE,RIGHT}, font = [HELVETICA, BOLD, 12]):
plotv2m := arrow(vec1, vec2, .2, .4, .1, color=green):
label2m := textplot([vec1[1]+vec2[1]/2,vec1[2]+vec2[2]/2,
`vector 2 translated`], align={ABOVE,RIGHT}, font = [HELVETICA, BOLD, 12]):
plotvecsum := arrow(zerovec, vecsum, .2, .4, .1, color=blue):
labelsum := textplot([(vec1[1]+vec2[1])/2,(vec1[2]+vec2[2])/2, `vector 1 + vector 2`], align={ABOVE,RIGHT},
font = [HELVETICA, BOLD, 12]):
display({plotv1, label1, plotv2, label2, plotv2m, label2m,
plotvecsum, labelsum}, scaling=constrained);Dot products and projections in 2D:Next we look at dot products. First we show our vectors and the unit vectors in the same directions. The unit vectors are shown wider than the original vectors so you can see them.zerovec := <0,0>:
vec1 := v1;
vec2 := v2;
magvec1 := sqrt(v1.v1);
vec1norm := vec1*(1/magvec1);
magvec2 := sqrt(v2.v2);
vec2norm := vec2*(1/magvec2);
plotv1 := arrow(zerovec, vec1, .1, .2, .1, color=red):
label1 := textplot([vec1[1]/2,vec1[2]/2, `vector 1`],
align={ABOVE,RIGHT}, font = [HELVETICA, BOLD, 12]):
plotv2 := arrow(zerovec, vec2, .1, .2, .1, color=yellow):
label2 := textplot([vec2[1]/2,vec2[2]/2, `vector 2`],
align={ABOVE,RIGHT}, font = [HELVETICA, BOLD, 12]):
plotv1n := arrow(zerovec, vec1norm, .2, .4, .2, color=blue):
plotv2n := arrow(zerovec, vec2norm, .2, .4, .2, color=brown):
display({plotv1, label1, plotv2, label2, plotv2n, plotv1n},
scaling=constrained);When we compute the projection of one vector onto another, we will always start by normalizing the vector we are projecting onto, producing a unit vector in the same direction. Then the projection is this unit vector times the dot product of the two vectors.#a block of code for finding the projection
#of vec1 onto vec2
zerovec := <0,0>:
vec1 := <3,4>;
vec2 := <1,3>;
magvec2 := sqrt(vec2.vec2);
vec2norm := vec2*(1/magvec2);
dotproduct := vec1. vec2norm;
projvec := dotproduct*vec2norm;
plotv1 := arrow(zerovec, vec1, .2, .4, .1, color=red):
plotv2 := arrow(zerovec, vec2, .2, .4, .1, color=blue):
plotvproj := arrow(zerovec, projvec, .14, .28, .1, color=yellow):
plotv2n := arrow(zerovec, vec2norm, .25, .5, .1, color=green):
plotvperp := arrow(projvec, vec1-projvec, .10, .20, .07, color=black):
label1 := textplot([vec1[1]/1.5,vec1[2]/1.5, ` vector 1`],
align={ABOVE,RIGHT}, font = [HELVETICA, BOLD, 12]):
label1a := textplot([vec2[1]/1.5,vec2[2]/1.5, `vector 2 `],
align={ABOVE,LEFT}, font = [HELVETICA, BOLD, 12]):
label2 := textplot([vec2norm[1]/2,vec2norm[2]/2, `vector 2 normalized `],
align={ABOVE,LEFT}, font = [HELVETICA, BOLD, 12]):
labelproj := textplot([projvec[1]/1.2,projvec[2]/1.2, `projection vector `],
align={ABOVE,LEFT}, font = [HELVETICA, BOLD, 12]):
labelorth := textplot([projvec[1]+(vec1[1]-projvec[1])/2,projvec[2]+(vec1[2]-projvec[2])/2, ` orthogonal component `],
align={ABOVE,RIGHT}, font = [HELVETICA, BOLD, 12]):
display({plotv1, plotv2, plotvproj, plotv2n, plotvperp, label1, label1a, label2, labelproj,labelorth},
scaling=constrained);Visualizing in 3DWe now repeat everything in 3 dimensions. We start by defining some vectors.v3 := <1, 5, 3>; v4 := <4, 2, 6>;
w3 := <c1, c2, c3>; w4 := <d1, d2, d3>;Addition of vectors and scalar multiplication works the same way as in 2D."v3+v4 ="=v3+v4;
"w3+w4 =" = w3+w4;
"3*v3 =" = 3*v1;
"5*w4 =" = 3*w4;Plotting once again shows that vector addition is performed by translating vectors.Rotate the plot to see the vectors clearly.#a block of code for adding 2 vectors in 3D.
zerovec := <0,0,0>;
vec1 := v3:
vec2 := v4:
normvec := <-1,-1,1>:
vecsum := vec1+vec2:
plotv1 := line(convert(zerovec,list), convert(vec1,list), normvec, linestyle=1, thickness=3, color=red):
label1 := textplot3d([vec1[1]/2,vec1[2]/2,vec1[3]/2, ` vector 1`], align=RIGHT, font = [HELVETICA, BOLD, 12], color=red):
plotv2 := line(convert(zerovec,list), convert(vec2,list), normvec, linestyle=1, thickness=3, color=green):
label2 := textplot3d([vec2[1]/2,vec2[2]/2,vec2[3]/2, ` vector 2`], align=RIGHT, font = [HELVETICA, BOLD, 12],color=green):
plotv2m := line(convert(vec1,list), convert(vecsum,list), normvec, linestyle=1, thickness=3, color=green):
label2m := textplot3d([vec1[1]+vec2[1]/2, vec1[2]+vec2[2]/2,
vec1[3]+vec2[3]/2, ` vector 2 moved`], align=RIGHT,
font = [HELVETICA, BOLD, 12], color=green):
plotvecsum := line(convert(zerovec,list), convert(vecsum,list), normvec, linestyle=1, thickness=3, color=blue):
labelsum := textplot3d([(vec1[1]+vec2[1])/2, (vec1[2]+vec2[2])/2, (vec1[3]+vec2[3])/2, ` vector 1 + vector 2`], align={ABOVE,RIGHT}, font = [HELVETICA, BOLD, 12], color=blue):
display({plotv1, label1, plotv2, label2, plotv2m, label2m,
labelsum, plotvecsum},axes=normal,scaling=constrained);Dot products and projections in 3D:As we did in two dimensions, we show our vectors and the unit vectors in the same directions.zerovec := <0,0,0>:
vec1 := v3;
vec2 := v4;
magvec1 := sqrt(vec1.vec1);
vec1norm := vec1*(1/magvec1);
magvec2 := sqrt(vec2.vec2);
vec2norm := vec2*(1/magvec2);
plotv1 := line(convert(zerovec,list), convert(vec1,list), linestyle=1, thickness=2, color=red):
label1 := textplot3d([vec1[1]/1.5,vec1[2]/1.5,vec1[3]/1.5, ` vector 1`], align=RIGHT, font = [HELVETICA, BOLD, 12], color=red):
plotv2 := line(convert(zerovec,list), convert(vec2,list), linestyle=1, thickness=2, color=green):
label2 := textplot3d([vec2[1]/1.5,vec2[2]/1.5,vec2[3]/1.5, ` vector 2`], align=RIGHT, font = [HELVETICA, BOLD, 12],color=green):
plotv1n := line(convert(zerovec,list), convert(vec1norm,list),linestyle=1, thickness=5, color=blue):
plotv2n := line(convert(zerovec,list), convert(vec2norm,list), linestyle=1, thickness=5, color=brown):
display3d({plotv1, label1, plotv2, label2,
plotv2n, plotv1n},axes=normal,scaling=constrained);Repeating the process we used in 2D, we normalize before projecting. zerovec := <0,0,0>:
vec1 := v3;
vec2 := v4;
magvec2 := sqrt(vec2.vec2);
vec2norm := vec2*(1/magvec2);
dotproduct := vec1. vec2norm;
projvec := dotproduct*vec2norm;
magvec2 := sqrt(vec2.vec2);
plotv1 := line(convert(zerovec,list), convert(vec1,list), linestyle=1, thickness=2, color=red):
label1 := textplot3d([vec1[1]/1.5,vec1[2]/1.5,vec1[3]/1.5, ` vector 1`], align=RIGHT, font = [HELVETICA, BOLD, 12], color=red):
plotv2 := line(convert(zerovec,list), convert(vec2,list), linestyle=1, thickness=2, color=blue):
label2 := textplot3d([vec2[1]/1.2,vec2[2]/1.2,vec2[3]/1.2, ` vector 2`], align=RIGHT, font = [HELVETICA, BOLD, 12], color=blue):
plotv2n := line(convert(zerovec,list), convert(vec2norm,list), linestyle=1, thickness=6, color=brown):
label2n := textplot3d([vec2norm[1]/1.5,vec2norm[2]/1.5,vec2norm[3]/1.5, ` vector 2 normalized`], align=RIGHT,
font = [HELVETICA, BOLD, 12], color=brown):
plotvperp := line(convert(vec1,list), convert(projvec,list), linestyle=1, thickness=2, color=black):
plotvproj := line(convert(zerovec,list), convert(projvec,list), linestyle=1, thickness=4, color=green):
labelproj := textplot3d([projvec[1]/1.5,projvec[2]/1.5,projvec[3]/1.5, ` projection vector`], align=RIGHT, font = [HELVETICA, BOLD, 12],color=green):
labelorth := textplot3d([projvec[1]+(vec1[1]-projvec[1])/2,projvec[2]+(vec1[2]-projvec[2])/2,projvec[3]+(vec1[3]-projvec[3])/2, ` orthogonal component `], align=RIGHT, font = [HELVETICA, BOLD, 12],color=black):
display3d({plotv1, label1, plotv2, label2,plotv2n, label2n, plotvproj, labelproj, plotvperp,labelorth}, axes=normal,scaling=constrained);Exercise1) Adapt the code above to illustrate the projection of the vector <-2,5,-6> onto the vector <3,-3,4>.What can you say about the direction of the projection vector relative to the vector projected onto in this case?The cross product of vectors in 3D:The syntax for the cross product of two vectors in R3 is simple.v3:= <-1, 3, -2>; v4 := <3, -2, 1>;
"v3 x v4 =" = CrossProduct(v3, v4);We check that the cross product appears to be perpendicular to both vectors v3 and v4.zerovec := <0,0,0>:
vec1 := v3;
vec2 := v4;
cornvec:= vec1+vec2:
crossvec := CrossProduct(vec1, vec2);
plotv1 := line(convert(zerovec,list), convert(vec1,list), linestyle=1, thickness=3, color=red):
label1 := textplot3d([vec1[1]/2,vec1[2]/2,vec1[3]/2, ` vector 1`], align=RIGHT, font = [HELVETICA, BOLD, 12], color=red):
plotv2 := line(convert(zerovec,list), convert(vec2,list), linestyle=1, thickness=3, color=brown):
label2 := textplot3d([vec2[1]/2,vec2[2]/2,vec2[3]/2, ` vector 2`], align=RIGHT, font = [HELVETICA, BOLD, 12], color=brown):
plotvcross := line(convert(zerovec,list), convert(crossvec,list), linestyle=1, thickness=3, color=blue):
labelcross := textplot3d([crossvec[1]/1.5,crossvec[2]/1.5,crossvec[3]/1.5, ` vector 1 cross vector 2`], align=RIGHT, font = [HELVETICA, BOLD, 12], color=blue):
plotpoly := polygon([convert(zerovec,list), convert(vec1,list), convert(cornvec,list), convert(vec2,list)], thickness=1, color=green):
display3d({plotv1, label1, plotv2, label2, plotvcross,
labelcross, plotpoly}, axes=normal,scaling=constrained);Exercise2) Plot the cross-product and associated parallelogram (as above) for the following pairs of vectors and then answer the question after the plots. <-1, 3, -2> & <3, -2, -1> <-1, 3, -2> & <-2, -2, -1> <-1, 3, -2> & <-2, 2, -1>(Note that the first vector in all of these is common; your plots may have different zoom factors.)What general observations can you make about this sequence of calculations?