Application Center - Maplesoft

App Preview:

Visualizing Vectors in 2-D and 3-D

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

Sec12.2Visualizing Vectors.mws

Visualizing vectors in 2D and 3D

Worksheet by Mike May, S.J.- maymk@slu.edu

As we have been looking at vectors, how they are added, normalized vectors, dot and cross products, it seems worthwhile to look at a Maple visualization of everything we have been doing.

First want to load the appropriate commands. The commands for dot and cross product are in the linalg package. The commands for plotting vectors is in the plottools package. We also need the display command from the plots package. (Restart cleans up in case we were already running a Maple worksheet.)

> restart: with(linalg): with(plottools): with(plots):

Warning, the protected names norm and trace have been redefined and unprotected

Warning, the name changecoords has been redefined

Visualizing vectors in R^2, simple operations:

We want to define some two dimensional vectors. We will use a pair of vectors with numbers filled in 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];

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;

When we try to normalize a vector we have to make a technical note. Like us, Maple has a tendency to leave things like square roots outside the vector. I will use code that forces it to multiply through by the radical. (You don't need to understand the code. I am only making the comment in case you are interested in why it gets used later on.)

> (1/sqrt(5))*v1;
convert(evalm((1/sqrt(5))*v1),list);

1/5*sqrt(5)*[1, 2]

[1/5*sqrt(5), 2/5*sqrt(5)]

Visualizing the vectors and the sum takes a bit more work. The following block of code shows the addition clearly.

> #a block of code to look at two vectors and their sum
zerovec := [0,0]:
vec1 := [1,2]:
vec2 := [4, -3]:
vecsum := vec1+vec2;
plotv1 := arrow(zerovec, vector(vec1), .2, .4, .1, color=red):
label1 := textplot([vec1[1]/2,vec1[2]/2, `vec 1`],
align={ABOVE,RIGHT}, font = [HELVETICA, BOLD, 10]):
plotv2 := arrow(zerovec, vector(vec2), .2, .4, .1, color=green):
label2 := textplot([vec2[1]/2,vec2[2]/2, `vec 2`],
align={ABOVE,RIGHT}, font = [HELVETICA, BOLD, 10]):
plotv2m := arrow(vec1, vector(vec2), .2, .4, .1, color=green):
label2m := textplot([vec1[1]+vec2[1]/2,vec1[2]+vec2[2]/2,
`vec 2 moved`], align={ABOVE,RIGHT}, font = [HELVETICA, BOLD, 10]):
plotvecsum := arrow(zerovec, vector(vecsum), .2, .4, .1, color=blue):
labelsum := textplot([(vec1[1]+vec2[1])/2,(vec1[2]+vec2[2])/2,
`vec 1 + vec 2`], align={ABOVE,RIGHT},
font = [HELVETICA, BOLD, 10]):
display({plotv1, label1, plotv2, label2, plotv2m, label2m,
plotvecsum, labelsum}, scaling=constrained);

vecsum := [5, -1]

[Maple Plot]

Dot products and projections in 2D:

Next we want to look at dot products. First we look at our vectors and unit vectors in the same direction.

> zerovec := [0,0]:
vec1 := v1;
vec2 := v2;
magvec1 := sqrt(dotprod(v1,v1));
vec1norm := convert(evalm(vec1*(1/magvec1)),list);
magvec2 := sqrt(dotprod(v2,v2));
vec2norm := convert(evalm(vec2*(1/magvec2)),list);
plotv1 := arrow(zerovec, vector(vec1), .2, .4, .1, color=red):
label1 := textplot([vec1[1]/2,vec1[2]/2, `vec 1`],
align={ABOVE,RIGHT}, font = [HELVETICA, BOLD, 10]):
plotv2 := arrow(zerovec, vector(vec2), .2, .4, .1, color=yellow):
label2 := textplot([vec2[1]/2,vec2[2]/2, `vec 2`],
align={ABOVE,RIGHT}, font = [HELVETICA, BOLD, 10]):
plotv1n := arrow(zerovec, vector(vec1norm), .2, .4, .1, color=blue):
plotv2n := arrow(zerovec, vector(vec2norm), .2, .4, .1, color=brown):
display({plotv1, label1, plotv2, label2, plotv2n, plotv1n},
scaling=constrained);

vec1 := [1, 2]

vec2 := [3, -4]

magvec1 := sqrt(5)

vec1norm := [1/5*sqrt(5), 2/5*sqrt(5)]

magvec2 := 5

vec2norm := [3/5, -4/5]

[Maple Plot]

For doing projections, in this class we will always start by normalizing the vector we are projecting onto. (That means we divide it by its length.) That means the projection will be the unit vctor times the dot product of the two vectors.

> #a block of code for finding the provjection
#of vec1 onto vec2
zerovec := [0,0]:
vec1 := [3,4];
vec2 := [1,3];
magvec2 := sqrt(dotprod(vec2,vec2));
vec2norm := convert(evalm(vec2*(1/magvec2)),list);
dotproduct := dotprod(vec1, vec2norm);
projvec := convert(evalm(dotproduct*vec2norm),list);
plotv1 := arrow(zerovec, vector(vec1), .2, .4, .1, color=red):
plotvproj := arrow(zerovec, vector(projvec), .14, .4, .1, color=yellow):
plotv2n := arrow(zerovec, vector(vec2norm), .22, .4, .1, color=green):
label1 := textplot([vec1[1]/2,vec1[2]/2, `vec 1`],
align={ABOVE,RIGHT}, font = [HELVETICA, BOLD, 10]):
label2 := textplot([vec2norm[1]/2,vec2norm[2]/2, `vec 2 normalized`],
align={ABOVE,RIGHT}, font = [HELVETICA, BOLD, 10]):
labelproj := textplot([projvec[1]/2,projvec[2]/2, `projection vector`],
align={ABOVE,RIGHT}, font = [HELVETICA, BOLD, 10]):
display({plotv1, plotvproj, plotv2n, label1, label2, labelproj},
scaling=constrained);

vec1 := [3, 4]

vec2 := [1, 3]

magvec2 := sqrt(10)

vec2norm := [1/10*sqrt(10), 3/10*sqrt(10)]

dotproduct := 3/2*sqrt(10)

projvec := [3/2, 9/2]

[Maple Plot]

Visualizing in 3D

We now want to 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];

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 it did 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 done by moving vectors.

> #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(zerovec, (vec1), normvec, linestyle=1,
thickness=3, color=red):
label1 := textplot3d([vec1[1]/2,vec1[2]/2,vec1[3]/2, ` vec 1`],
align=RIGHT, font = [HELVETICA, BOLD, 10], color=red):
plotv2 := line(zerovec, (vec2), normvec, linestyle=1,
thickness=3, color=green):
label2 := textplot3d([vec2[1]/2,vec2[2]/2,vec2[3]/2, ` vec 2`],
align=RIGHT, font = [HELVETICA, BOLD, 10],color=green):
plotv2m := line(vec1, vec1 + (vec2), normvec, linestyle=1,
thickness=3, color=green):
label2m := textplot3d([vec1[1]+vec2[1]/2, vec1[2]+vec2[2]/2,
vec1[3]+vec2[3]/2, ` vec 2 moved`], align=RIGHT,
font = [HELVETICA, BOLD, 10], color=green):
plotvecsum := line(zerovec, (vecsum), normvec, linestyle=1,
thickness=3, color=blue):
labelsum := textplot3d([(vec1[1]+vec2[1])/2, (vec1[2]+vec2[2])/2,
(vec1[3]+vec2[3])/2, ` vec 1 + vec 2`], align={ABOVE,RIGHT},
font = [HELVETICA, BOLD, 10], color=blue):
display({plotv1, label1, plotv2, label2, plotv2m, label2m,
labelsum, plotvecsum},axes=framed,scaling=constrained);

vecsum := [5, 7, 9]

[Maple Plot]

>

Dot products and projections in 3D:

We now repeat everything we did in 2 dimensions, but with three dimensions. First we look at our vectors and unit vectors in the same direction.

> zerovec := [0,0, 0]:
vec1 := v3;
vec2 := v4;
magvec1 := sqrt(dotprod(vec1,vec1));
vec1norm := convert(evalm(vec1*(1/magvec1)),list);
magvec2 := sqrt(dotprod(vec2,vec2));
vec2norm := convert(evalm(vec2*(1/magvec2)),list);
plotv1 := line(zerovec, (vec1), linestyle=1,
thickness=1, color=red):
label1 := textplot3d([vec1[1]/2,vec1[2]/2,vec1[3]/2, ` vec 1`],
align=RIGHT, font = [HELVETICA, BOLD, 10], color=red):
plotv2 := line(zerovec, (vec2), linestyle=1,
thickness=1, color=green):
label2 := textplot3d([vec2[1]/2,vec2[2]/2,vec2[3]/2, ` vec 2`],
align=RIGHT, font = [HELVETICA, BOLD, 10],color=green):
plotv1n := line(zerovec, (vec1norm),linestyle=1,
thickness=3, color=blue):
plotv2n := line(zerovec, (vec2norm), linestyle=1,
thickness=3, color=brown):
display3d({plotv1, label1, plotv2, label2,
plotv2n, plotv1n},axes=framed,scaling=constrained);

vec1 := [1, 5, 3]

vec2 := [4, 2, 6]

magvec1 := sqrt(35)

vec1norm := [1/35*sqrt(35), 1/7*sqrt(35), 3/35*sqrt...

magvec2 := 2*sqrt(14)

vec2norm := [1/7*sqrt(14), 1/14*sqrt(14), 3/14*sqrt...

[Maple Plot]

Repeating the process we used in 2D, we normalize before projecting.

> zerovec := [0,0, 0]:
vec1 := v3;
vec2 := v4;
magvec2 := sqrt(dotprod(vec2,vec2));
vec2norm := convert(evalm(vec2*(1/magvec2)),list);
dotproduct := dotprod(vec1, vec2norm);
projvec := convert(evalm(dotproduct*vec2norm),list);
magvec2 := sqrt(dotprod(vec2,vec2));
plotv1 := line(zerovec, (vec1), linestyle=1,
thickness=1, color=red):
label1 := textplot3d([vec1[1]/2,vec1[2]/2,vec1[3]/2, ` vec 1`],
align=RIGHT, font = [HELVETICA, BOLD, 10], color=red):
plotv2n := line(zerovec, (vec2norm), linestyle=1,
thickness=3, color=brown):
label2n := textplot3d([vec2norm[1]/2,vec2norm[2]/2,vec2norm[3]/2,
` vec 2 normalized`], align=RIGHT,
font = [HELVETICA, BOLD, 10], color=brown):
plotvperp := line(vec1, (projvec), linestyle=1,
thickness=1, color=black):
plotvproj := line(zerovec, (projvec), linestyle=1,
thickness=1, color=green):
labelproj := textplot3d([projvec[1]/2,projvec[2]/2,projvec[3]/2,
` projection vector`],
align=RIGHT, font = [HELVETICA, BOLD, 10],color=green):
display3d({plotv1, label1, plotv2n, label2n, plotvproj, labelproj,
plotvperp}, axes=framed,scaling=constrained);

vec1 := [-1, 3, -2]

vec2 := [3, -2, 1]

magvec2 := sqrt(14)

vec2norm := [3/14*sqrt(14), -1/7*sqrt(14), 1/14*sqr...

dotproduct := -11/14*sqrt(14)

projvec := [-33/14, 11/7, -11/14]

magvec2 := sqrt(14)

[Maple Plot]

The cross product of vectors in 3D:

The syntax for cross product is simple.

> v3:= [-1, 3, -2]; v4 := [3, -2, 1];
"v3 x v4 =" =crossprod(v3, v4);

v3 := [-1, 3, -2]

v4 := [3, -2, 1]

We want to observe that the cross product is perpendicular to both of the other vectors.

> zerovec := [0,0, 0]:
vec1 := v3;
vec2 := v4;
cornvec:= vec1+vec2:
crossvec := convert(crossprod(vec1, vec2),list);
plotv1 := line(zerovec, (vec1), linestyle=1,
thickness=3, color=red):
label1 := textplot3d([vec1[1]/2,vec1[2]/2,vec1[3]/2, ` vec 1`],
align=RIGHT, font = [HELVETICA, BOLD, 10], color=red):
plotv2 := line(zerovec, (vec2), linestyle=1,
thickness=3, color=brown):
label2 := textplot3d([vec2[1]/2,vec2[2]/2,vec2[3]/2, ` vec 2`],
align=RIGHT, font = [HELVETICA, BOLD, 10], color=brown):
plotvcross := line(zerovec, (crossvec), linestyle=1,
thickness=3, color=blue):
labelcross := textplot3d([crossvec[1]/2,crossvec[2]/2,crossvec[3]/2,
` vec 1 cross vec 2`],
align=RIGHT, font = [HELVETICA, BOLD, 10], color=blue):
plotpoly := polygon([zerovec, vec1, cornvec, vec2],
thickness=1, color=green):
labelproj := textplot3d([projvec[1]/2,projvec[2]/2,projvec[3]/2,
` projection vector`],
align=RIGHT, font = [HELVETICA, BOLD, 10],color=green):
display3d({plotv1, label1, plotv2, label2, plotvcross,
labelcross, plotpoly}, axes=framed,scaling=constrained);

vec1 := [-1, 3, -2]

vec2 := [3, -2, 1]

crossvec := [-1, -5, -7]

[Maple Plot]