Laboratory 2
Vectors, Dot Products, and Cross Products

Introduction

In this lab we learn how to represent two and three dimensional vectors both algebraically and geometrically using Mathematica. We shall also learn the basic algebra of vectors, including vector addition, scalar multiplication, the dot product, and the cross product, as well as their application.

Example 2.1 Representation of Vectors

A vector is a directed line segment having both a length and a direction. Since a vector in the plane or in space is determined by an ordered pair or ordered triple, we represent a vector in Mathematica in terms of its components. For instance, here is how we define the vector (OverVector[w]) whose x, y, and z components are (Subscript[w, 1]), (Subscript[w, 2]), and (Subscript[w, 3]) respectively.

w={w1,w2,w3}

To refer to a particular component of a vector (or a list), we use double square brackets. For example, here is the y-component of the vector (OverVector[w]) defined above.

w[[2]]

We can also use Mathematica to perform vector addition and scalar multiplication. Suppose (OverVector[v])=(5,11,3) and (OverVector[w])=(2,-4,21) so that

v={5 ,11, 3}
w={2,-4,21}

To calculate the vector 3(OverVector[v]) we execute

3v

We could instead execute

3*v

Here is the sum (OverVector[v])+(OverVector[w])

v+w

The difference (OverVector[v])-(OverVector[w]) is found similarly

v-w

We also want to display graphs of vectors. In the two-dimensional case this will be done using the command vector, which we first define below. You must execute the command that defines the command vector before you can use it elsewhere.

vector[a_,b_,c_:0]:=If[c==0,Return[Graphics[{Arrowheads[Medium],Arrow[{a,b}]}]],Return[Graphics[{Hue[c],Arrowheads[Medium],Arrow[{a,b}]}]]]

The command vector[a_,b_,c_:0] is a function with the three variables a, b, and c. Here a and b represent the initial and terminal points of our vector, and hence must be entered as lists. The last input, c is an optional number between 0 and 1 that gives the color of the vector. If c is not specified then the vector is drawn in black. To use this command to display a given vector, we specify the initial point and the terminal point for that vector. Thus, if we want the vector from the point (1,2) to the point (2,-1) in the standard black, we execute the command

Show[vector[{1,2},{2,-1}]]

Notice that the command vector[] returns a "raw graphics" and we need to use a Show command in order to display its output. This may seem to be a nuisance, but it allows us to enhance the graph by adding a few options to our Show command: the option Axes->True puts axes on our graph, while the option PlotRange->{{-4,4},{-3,3}} tells Mathematica to show the portion of the graph for which -4≤x≤4, -3≤y≤3, and the option AspectRatio->Automatic ensures that the graph is drawn to the same scale in the x-direction and the y-direction.

Show[vector[{1,2},{2,-1}],Axes->True,PlotRange->{{-4,4},{-3,3}},AspectRatio->Automatic]

We can show more than one vector at a time by enclosing the collection of vectors inside curly braces { }. Finally, we can specify the third parameter in the vector command to change the color; this number must be between 0 and 1. As an example, let us plot the vectors (OverVector[v])=(2,4), (OverVector[w])=(1,-3)in their default black, and let us plot the vector (OverVector[z])=(OverVector[v])+(OverVector[w]) in red.

v={2,4}
w={1,-3}
z=v+w

Show[{vector[{0,0},v],vector[{0,0},w],vector[{0,0},z,1]},Axes->True,PlotRange->{{-4,4},{-4,4}},AspectRatio->Automatic]

Here is a graphical demonstration of the subtraction of two vectors. Suppose (OverVector[a])=(3,1) and (OverVector[b])=(2,-2); algebraically we can calculate the vector (OverVector[a])-(OverVector[b]) as follows.

a={3,1}
b={2,-2}
a-b

Now let us plot (OverVector[a]), (OverVector[b]), and (OverVector[a])-(OverVector[b]) as position vectors; in particular we shall plot the vectors whose initial point is the origin (0,0) and whose terminal point is either (OverVector[a]), (OverVector[b]), or (OverVector[a])-(OverVector[b]). To help us visualize the process, we shall plot the vector (OverVector[a])-(OverVector[b]) in red.

Show[{vector[{0,0},a],vector[{0,0},b],vector[{0,0},a-b,1]},Axes->True,PlotRange->{{-4,4},{-4,4}},AspectRatio->Automatic]

Compare the vector (OverVector[a])-(OverVector[b]) in red with the vector starting at (OverVector[b]) and ending at (OverVector[a]), drawn in blue below.

Show[{vector[{0,0},a],vector[{0,0},b],vector[{0,0},a-b,1],vector[b,a,0.66]},Axes->True,PlotRange->{{-4,4},{-4,4}},AspectRatio->Automatic]

What can you conclude about the relationship between these vectors?

We can also examine vectors in three dimensions. To do so, we define the command vector3d below. Like the command vector, we must execute the command that defines vector3d before we can use it.

vector3d[a_,b_,c_:0]:=Module[{d},
d=Norm[b-a];
If[c==0,Return[Graphics3D[{Thick,Black,Line[{a,b}],Translate[Rotate[Scale[Table[{EdgeForm[],Polygon[{{.25Cos[j(Pi/6)],.25Sin[j(Pi/6)],0},{.25Cos[(j+1)(Pi/6)],.25Sin[(j+1)(Pi/6)],0},{0,0,1}}]},{j,0,11}],d/10,{0,0,0}],{{0,0,1},b-a}],(9b+a)/10]}]],
Return[Graphics3D[{Thick,Hue[c],Line[{a,b}],Translate[Rotate[Scale[Table[{EdgeForm[],Polygon[{{.25Cos[j(Pi/6)],.25Sin[j(Pi/6)],0},{.25Cos[(j+1)(Pi/6)],.25Sin[(j+1)(Pi/6)],0},{0,0,1}}]},{j,0,11}],d/10,{0,0,0}],{{0,0,1},b-a}],(9b+a)/10]}]]
]
]

Like the command vector, the command vector3d takes as input the starting point and ending point of the vector. To see it in action, let us define the vectors (OverVector[o])=(0,0,0), (OverVector[i])=(1,0,0), (OverVector[j])=(0,1,0), and (OverVector[k])=(0,0,1).

o={0,0,0};i={1,0,0};j={0,1,0};k={0,0,1};

We can then plot the vectors in each of the coordinate directions as follows.

Show[{vector3d[o,i],vector3d[o,j],vector3d[o,k]}]

Like we did when we viewed the two dimensional vectors, we can tweak our picture by adding a few options. The setting Axes->True tells Mathematica to plot the scale along the axes, while the PlotRange->{{-1,1},{-1,1},{-1,1}} command tells Mathematica to show us the portion of the graph for which -1≤x,y,z≤1. The BoxRatios->Automatic command ensures that we have the same scale in the x-, y-, and z- directions, while the AxesLabel->{"x","y","z"} simply labels the axes for us.  Finally, we shall change the viewpoint of the graph to the point (6,2,2) so that the result looks like the way we would traditionally draw the graph.

Show[{vector3d[o,i],vector3d[o,j],vector3d[o,k]},Axes->True,BoxRatios->Automatic,AxesLabel->{"x","y","z"},PlotRange->{{-1,1},{-1,1},{-1,1}},ViewPoint->{6,2,2}]

The color of three dimensional vectors are changed in the same fashion as the two-dimensional vectors. Here is an example of a graph of (OverVector[v])=(1,-2,1), (OverVector[w])=(2,1,-1), and (OverVector[v])-(OverVector[w]), with the last of these drawn in red.

v={1,-2,1};
w={2,1,-1};
Show[{vector3d[o,v],vector3d[o,w],vector3d[o,v-w,1]},Axes->True,PlotRange->{{-3,3},{-3,3},{-3,3}},BoxRatios->Automatic,AxesLabel->{"x","y","z"},ViewPoint->{6,2,2}]

Note what happens if we also plot the vector starting at (OverVector[v]) and ending at (OverVector[w]) in blue.

Show[{vector3d[o,v],vector3d[o,w],vector3d[o,v-w,1],vector3d[w,v,0.67]},Axes->True,PlotRange->{{-3,3},{-3,3},{-3,3}},BoxRatios->Automatic,AxesLabel->{"x","y","z"},ViewPoint->{6,2,2}]

Experiment 2.1

1. For the vectors (OverVector[v])=(1,-3) and (OverVector[w])=(2,-4), plot the vectors (OverVector[v]), (OverVector[w]), and (OverVector[v])-(OverVector[w]) on the same graph, with the last of these in red. Plot the vector starting at (OverVector[w]) and ending at (OverVector[v]) on the graph as well. What is the relationship between this vector and the vector (OverVector[v])-(OverVector[w])?

2. For given vectors (OverVector[v])=(1,1,0) and (OverVector[w])=(-1,0,2), plot the vectors (OverVector[v]), (OverVector[w]), (OverVector[v])+(OverVector[w]), and (OverVector[v])-(OverVector[w]) on the same graph, with the last of these in red. Plot the vector starting at (OverVector[w]) and ending at (OverVector[v]) on the graph as well. What is the relationship between this vector and the vector (OverVector[v])-(OverVector[w])?
	
3. For given vectors (OverVector[v])=(1,2,-1) and (OverVector[w])=(-1,1,2), plot the vectors (OverVector[v]), (OverVector[w]), (OverVector[v])+(OverVector[w]), and (OverVector[v])-(OverVector[w]) on the same graph, with the last of these in red. Plot the vector starting at (OverVector[w]) and ending at (OverVector[v]) on the graph as well. What is the relationship between this vector and the vector (OverVector[v])-(OverVector[w])?

Example 2.2 The Dot Product and Cross Product

We perform the dot and cross products using the Mathematica commands Dot[] and Cross[]. These commands also have infix operator forms. A period (.) between vectors performs a dot product, and a × between vectors performs a cross product. To enter the symbol ×, first hit the Esc key and type "cross" then hit again the Esc key. As an example, let us set (OverVector[v])=((Subscript[v, 1]),(Subscript[v, 2]),(Subscript[v, 3])) and (OverVector[w])=((Subscript[w, 1]),(Subscript[w, 2]),(Subscript[w, 3])). We enter these vectors in Mathematica as follows.

v={v1,v2,v3}
w={w1,w2,w3}

To find the dot product, we execute

v.w

or

Dot[v,w]

To find the cross product, we can try either

v\[Cross]w

or

Cross[v,w]

Now suppose that we want to find the length of the vector (OverVector[v])=((Subscript[v, 1]),(Subscript[v, 2]),(Subscript[v, 3])). We know that ||(OverVector[v])||=(Sqrt[(OverVector[v])·(OverVector[v])]), so to calculate the length of v we ask for the following.

Sqrt[v.v]

or

Norm[v]

Finally, we know that we can find the angle between two vectors by using the dot product. In particular, we know that if θ is the angle between the vector (OverVector[v]) and the vector (OverVector[w]), then θ=(cos^(-1))(((OverVector[v])·(OverVector[w]))/(||(OverVector[v])|| ||(OverVector[w])||)). We can find this using Mathematica in the following fashion.

ArcCos[(v.w)/Sqrt[(v.v)*(w.w)]]

To see how this all works in practice, let us set (OverVector[v])=(2,-1,1) and (OverVector[w])=(1,2,-1). Let us find (OverVector[v])·(OverVector[w]), (OverVector[v])×(OverVector[w]), ||(OverVector[v])||, ||(OverVector[w])||, and the angle between (OverVector[v]) and (OverVector[w]). First we define the vectors.

v={2,-1,1}
w={1,2,-1}

Now we find the dot product

v.w

and the cross product.

v\[Cross]w

The length of (OverVector[v]) is

Sqrt[v.v]

while the length of (OverVector[w]) is

Sqrt[w.w]

Finally, the angle between (OverVector[v]) and (OverVector[w]) is

ArcCos[(v.w)/(Norm[v]Norm[w])]

Note that Mathematica gave us an exact answer. To find a numerical approximation, we can use the command N[] as follows.

N[ArcCos[(v.w)/(Norm[v]Norm[w])]]

Keep in mind that Mathematica returns answers in radians. If we want the answer in degrees, we can execute the following

N[ArcCos[(v.w)/(Norm[v]Norm[w])]]/Degree

Let us examine a graph of the vectors (OverVector[v]) and (OverVector[w]); we shall also show the vector (OverVector[v])×(OverVector[w]) in red.

Show[{vector3d[{0,0,0},v],vector3d[{0,0,0},w],vector3d[{0,0,0},v\[Cross]w,1]},Axes->True,PlotRange->Automatic,BoxRatios->Automatic,AxesLabel->{"x","y","z"},ViewPoint->{6,2,2}]

We can see from the picture that the vector (OverVector[v])×(OverVector[w]) is perpendicular to (OverVector[v]) and (OverVector[w]).

Experiment 2.2

1. Let (OverVector[v])=(2,1,0) and (OverVector[w])=(-1,-3,1). Compute  (OverVector[v])·(OverVector[w]), (OverVector[v])×(OverVector[w]), the length of (OverVector[v]), the length of (OverVector[w]), and the angle between two vectors in degree measure. Give a graph that shows (OverVector[v]), (OverVector[w]), and the vector (OverVector[v])×(OverVector[w]).

2.  Let (OverVector[v])=(1,1,-2) and (OverVector[w])=(-1,2,-1). Compute  (OverVector[v])·(OverVector[w]), (OverVector[v])×(OverVector[w]), the length of (OverVector[v]), the length of (OverVector[w]), and the angle between two vectors in degree measure. Give a graph that shows (OverVector[v]), (OverVector[w]), and the vector (OverVector[v])×(OverVector[w]).

Example 2.3 Applications of the Dot Product and Cross Product

Finding a unit vector perpendicular to two given vectors.

Suppose we wanted to find a unit vector that is perpendicular to each of (OverVector[a])=(-1,0,2) and (OverVector[b])=(-2,1,-1). To do so, we can compute the cross product, and then divide by the length of the result. First we define (OverVector[a]) and (OverVector[b]).

a={-1,0,2}
b={-2,1,-1}

The cross product is then found.

a\[Cross]b

{-2,-5,-1}

Because we want our vector to have length 1, we divide by the length of the result.

n=a\[Cross]b/Sqrt[a\[Cross]b.a\[Cross]b]

Let us plot the vectors (OverVector[a]), (OverVector[b]), and (OverVector[n]), with the vector (OverVector[n]) in red.

Show[{vector3d[{0,0,0},a],vector3d[{0,0,0},b],vector3d[{0,0,0},n,1]},Axes->True,PlotRange->{{-2,2},{-2,2},{-2,2}},BoxRatios->Automatic,AxesLabel->{"x","y","z"},ViewPoint->{6,2,2}]

Finding the area of a triangle.

Suppose that we want to find the area of the triangle whose vertices are the points (2,-3,4), (0,1,-4), and (-4,2,1). We know that the area of the parallelogram spanned by the vectors (OverVector[a]) and (OverVector[b]) is ||(OverVector[a])×(OverVector[b])||. For this reason we let (OverVector[a]) be the vector that starts at (2,-3,4) and ends at (0,1,-4); we also let b be the vector that starts at (2,-3,4) and ends at (-4,2,1).

a={0,1,-4}-{2,-3,4}
b={-4,2,1}-{2,-3,4}

We can plot the vector that starts at (2,-3,4) and ends at (0,1,-4) as well as the vector that starts at (2,-3,4) and ends at (-4,2,1).

Show[vector3d[{2,-3,4},{0,1,-4}],vector3d[{2,-3,4},{-4,2,1}],Axes->True,PlotRange->{{-4,4},{-4,4},{-4,4}},BoxRatios->Automatic,AxesLabel->{"x","y","z"},ViewPoint->{6,2,2}]

c=a\[Cross]b;(1/2)Norm[c]

Since the area of the triangle is half of the area of the parallelogram, the area of the triangle is 7(Sqrt[14]).

Theory

We prove that a((OverVector[v])×(OverVector[w]))=(a (OverVector[v]))×(OverVector[w]), where (OverVector[v]) and (OverVector[w]) are arbitrary vectors in space and a is an arbitrary scalar. First we separately compute the left hand and right sides.

Clear[a,v,w,L,R]
v={v1,v2,v3}
w={w1,w2,w3}

Let (OverVector[L]) be the left hand side of our proposed equality, namely (OverVector[L])=a((OverVector[v])×(OverVector[w])).

L=a*(v\[Cross]w)

We then let (OverVector[R])=(a (OverVector[v]))×(OverVector[w]) be the right side of our proposed equality.

R=(a*v)\[Cross]w

Next we want to show (OverVector[L])=(OverVector[R]), or equivalently, that (OverVector[L])-(OverVector[R])=(OverVector[0]). However, if we calculate (OverVector[L])-(OverVector[R]) we find the following.

L-R

Simplify[L-R]

We conclude that (OverVector[L])=(OverVector[R]) and that our proposed identity is true.

Experiment 2.3a

1. Find a unit vector that is perpendicular to each of (OverVector[a])=(1,2,1) and (OverVector[b])=(-2,2,-1). Include a graph.

2. Find a unit vector that is perpendicular to each of (OverVector[a])=(-1,1,2) and (OverVector[b])=(0,1,0). Include a graph.

Experiment 2.3b

1. Find the area of the triangle whose vertices are the points (1,3,5), (3,3,0), and (-2,0,5).

2. Find the area of the triangle whose vertices are the points (-2,-3,5), (0,1,2), and (3,2,0).

Experiment 2.3c

Use Mathematica to prove that |(OverVector[v])×(OverVector[w])((|)^2)=|(OverVector[v])((|)^2)|(OverVector[w])((|)^2)-((((OverVector[v])·(OverVector[w])))^2), where v,w, are arbitrary vectors in space.

Credits

These laboratories were created by Raouf Boules, Geoffrey Goodson, Ohoe Kim, Alexei Kolesnikov, Angel Kumchev and Michael O'Leary for use in the Calculus courses of Towson University. Commercial use is prohibited without permission of the authors. Non-commercial use is permitted, provided this credit section is retained.