Mathematica Lab 3: Vectors

Math 113, Fall 2017
Dr. JohnGemmer
Wake Forest University

Vectors in Mathematica

Basic Operations with Vectors

Vectors in Mathematica are defined as lists {(Subscript[a, 1]),(Subscript[a, 2]),…, (Subscript[a, n])}.  Operations on vectors can be carried out in a natural way. Here is an example of vector addition.

{1,2,4} + {4,0,-1}

Here is an example of scalar multiplication.

5*{1,2,4}

The length of a vector can be determined using the Norm command.

Norm[{1,2,3}]

Drawing Vectors in 2D and 3D

Mathematica has various commands for drawing points, lines and arrows in 2D and 3D. You can look these up in the documentation center (under Help) along with the many options available for controlling their appearance.  The basic command for drawing arrows (which can be used to represent vector quantities) is the following: 

Arrow[{(Subscript[pt, 1]),(Subscript[pt, 2])}]
is a graphics primitive that represents an arrow from (Subscript[pt, 1]) to (Subscript[pt, 2]).

Graphics[
Arrow[ {{1,1},{2,3}} ]
]

Show[Graphics[Arrow[{{1,1},{2,3}}]],ImageSize->Small]

We can include axes and change the color as follows.

Show[Graphics[
{Red, Arrow[ {{1,1},{2,3}} ]},
Axes -> True, AxesOrigin -> {0,0}
],ImageSize->Small]

Drawing arrows in 3D is similar.

Graphics3D[
Arrow[ {{0,0,1},{1,.5,2}} ]
]

Observe that this is manipulable -- hold your cursor on it.  To include axes and change the color we add some additional options.

Show[Graphics3D[
{Red,Arrow[ {{0,0,1},{1,.5,2}} ]}
,Axes -> True, AxesOrigin -> {0,0,0}
],ImageSize->Small]

One can display several different arrows in the same picture using the Show command.

Show[
Graphics[
Arrow[ {{0,0},{1,1}} ]
],
Graphics[
{Red, Arrow[ {{1,1.5},{2,2.5}} ]}
],
Graphics[
{Blue, Arrow[ {{0.5,2},{1.5,3}} ]}
], 
Axes->True, AxesOrigin -> {0,0}
]

Let’s draw a picture for vector addition.

a = {1,1};
b = {.5,1.5};
Show[
Graphics[
Arrow[ {{0,0},a} ]   (* produces a black arrow representing the vector a *)
],
Graphics[
{Red, Arrow[ {a,a+b} ]}
(* produces a red arrow representing the vector b *)
],
Graphics[
{Blue, Arrow[ {{0,0},a+b} ]}
(* produces a blue arrow representing the sum a+b *)

], 
Axes->True, AxesOrigin -> {0,0}
]

Mathematica Task #1:  Vectors

Plot 3 of your favorite 2D vectors on the same graph.

Plot the vectors <-1,3,4>, <2,0,5>, and <3,2,1> on the same plot.

For vector v=<2,5>, plot 0.5v, v, 2v, 3v, -v, -2v.  Compute the norm of v.

Dot Products and Cross Products

To scale a vector by a constant, just multiply:

v={2,3,4}

2{2,3,4}
3v

Mathematica makes it easy to compute dot & cross products ... use Dot (or just .) and Cross.

w={-1,2,-1}

Dot[v,w]

(So we conclude v and w are orthogonal!)

Cross [v,w]

Here’s the shorthand way for dot & cross products.  (You can find the × symbol in the “Basic Math Assistant” Palette.)

v.w
v\[Cross]w

Reminder:  Below, I’ve “closed” the sections & subsections, for improved readability.  To open them, right-click on the dividers at the right, and click on “open/close group”.

Mathematica Task #2:  Computing Angles

Complete these problems.

The methane molecule (Subscript[CH, 4]) consists of a carbon atom bonded to four hydrogrn molecules, symmetrically spaced.  We can model this by placing a carbon atom at  (1/2,1/2,1/2) and the hydrogen atoms at (0,0,0), (1,1,0), (1,0,1), (0,1,1).  
a.  Draw a plot of this molecule’s 4 bonds.  (Recall the Arrow command from before.  Depict each atom using the Sphere[p,r] command.  Use Display to show multiple things at once.)
b.  Use the dot product formula to find the bond angle formed between any two of the line segments from hydrogen atoms to the carbon atom.

Write a function Angle[v_,w_] that takes any two vectors v and w as input and returns the angle between them.  Then compute the angle between <1,1,0> and <0,2,2>.