01 Vectors
[Your names]

Work with someone else, but submit your own completed Mathematica notebook file by uploading it to Moodle.

Mathematica Review

[As you go through this notebook execute each computation cell, and make sure you understand the results you get.  Place the cursor anywhere in a computation cell and press ⇧-⌤ to execute the computation.]

Mathematica can be used like a calculator.    Multiplication can be represented with an asterisk (*), the times symbol (×), or a blank space (␣).  Exponentiation is represented with a caret (^). Standard operator precedence is followed (e.g., in the computation below, exponentiation is performed first, multiplication and division are performed next, and subtraction and addition are performed last). [The first “computation cell” is below...]

4-2.1*3^2+5/6

Typically, Mathematica provides exact answers.  Built in functions always begin with an uppercase letter and use square brackets (e.g., Cos[x] rather than cos(x)).

Sqrt[Cos[Pi]+13]/10^2

Numerical approximations can be obtained by using the N function.

N[Sqrt[Cos[Pi]+13]/10^2]

An alternate way to get a numerical approximation is to add a decimal point to any number in your expression:

Sqrt[Cos[Pi]+13.]/10^2

Numerical approximations can be obtained to however many digits are desired.

N[Sqrt[Cos[Pi]+13]/10^2,50]

Typesetting of expressions is possible using the Basic Math Assistant Palette or special keys (e.g., a fraction is obtained with Ctrl-/, the square root is obtained with Ctrl-2, π is obtained with Esc p Esc, and exponentiation is obtained with Ctrl-^).

((Sqrt[Cos[π]+13])/(10^2))

An equation can be solved exactly and/or with a numerical approximation.

Solve[(x^2)+3x+1==0,x]
N[%]
NSolve[(x^2)+3x+1==0,x]

We discussed in class the idea of solving systems of equations.  Here’s an example: (Think of “&&” as meaning “and”.)

Solve[  
3 a + b ==14 && a-b==2,
 {a,b}
]

Note that Mathematica uses a double equals sign (==) to represent the equality in an equation.  The single equals sign (=) is the assignment operator, used to assign numbers and “expressions” to variables.

a=(x^2)+x
b=2x+1
c=a+b

Expression syntax for functions you define

After you’ve executed the line above, c is an “expression” that represents a function, c(x) from real numbers to real numbers.  

To evaluate the function for some specific value of x, say, you want to find c(1), you can use the replacement operator, /., with a replacement rule (indicated by the arrow, which you type in as the two characters “->”.), as in this example,

c /. x->1

Expressions can also be graphed, differentiated, and integrated.

Plot[c,{x,-5,2}]
D[c,x]
Integrate[c,x]
Integrate[c,{x,-5,2}]

Exercise 1- 5pts

Consider the function f(x)=(x^3)-cos(2x).  Using expression syntax, find the exact value and a numerical approximation of f(4).  Graph f on the interval [-2,2].  Find f'(x) and (∫_(-2)^2)f(x)dx .

Function syntax for functions you define

A different way to deal with mathematical functions is to define them as “Mathematica functions”. like this:

MyC[x_]:=1 + 3 x +x^2

Here are examples of evaluating the function at a particular value of x, plotting, differentiating and integrating

MyC[1]
Plot[ MyC[t], {t, -5,2}   ]
D[ MyC[x], x     ]
Integrate[ MyC[x], x   ]
(∫_(-5)^2)MyC[x] \[DifferentialD]x

Note that you have greater latitude, with function syntax, of what you put in the argument of the function.  E.g. you can easily evaluate (and then simplify)  MyC[x-2]:

MyC[x-2]
Simplify [ % ]

Exercise 2 - 4 pts

Consider again the function f(x)=(x^3)-cos(2x).  This time use function syntax to define a function for f(x), and then find the exact value and a numerical approximation of f(4).  Graph f on the interval [-2,2].  Find f'(x) and (∫_(-2)^2)f(x)dx .

Exercise 3 - 2 pts

Attempt to find an approximate solution to the equation f(x)=0 using the NSolve function.

When that does not work, place the cursor on NSolve and right-click your mouse, then choose “Get Help”.   Notice the numerous examples showing how to use the NSolve function.  Look for the  See Also  section.  Try some of the other methods until you are able to make one give you an approximate solution to f(x)=0.  Do it!

Vectors in the Plane

A point is a location in some space.  A point in (ℝ^n) is located by a list of n numbers.  For example, we may write P=(1,3) and Q=(2,1) to locate two points P and Q in (ℝ^2).  In Mathematica, we can assign a list of numbers to a variable.  Since Mathematica uses parentheses to group together parts of arithmetic expressions, it uses braces--{...} to indicate the beginning and ending of a list.  Since Mathematica begins its built in names with a uppercase letter, I usually use names that begin with a lowercase letter, which is why I used lowercase p rather than uppercase P.

p={1,3}
q={2,1}

Points can be graphed.

plot1=Graphics[{Point[p],Point[q]},Axes->True,PlotRange->4]

Here is one way to add to the graph a line that passes through the two points.

plot2=ParametricPlot[(1-t)p+t q,{t,-10,10}];
Show[plot1,plot2]

A vector is a list of two points.  For example, if P=(1,3) and Q=(2,1), then (OverVector[PQ])=((1,3),(2,1)). The first point is the base or tail and the second point is the head.

(OverVector[pq])={p,q};
Graphics[{Point[p],Point[q],Arrow[(OverVector[pq])]},
Axes->True,
PlotRange->4]

[You don’t *have* to use the arrow expression name for a  vector.  In fact, below I’ll use “v” without an arrow.  But it’s nice to be able to add the arrow to the expression name to banish any doubt in your audience’s mind that we’re talking about a vector quantity.]

The components of a vector (OverVector[PQ]), where P=((Subscript[a, 1]),(Subscript[b, 1])) and Q=((Subscript[a, 2]),(Subscript[b, 2])), are the quantities v=⟨a,b⟩=⟨(Subscript[a, 2])-(Subscript[a, 1]),(Subscript[b, 2])-(Subscript[b, 1])⟩, and the length of (OverVector[PQ]) is ||(OverVector[PQ])||=||v||=(Sqrt[(a^2)+(b^2)]). [The “norm” of a vector is the same as the “length” or “magnitude” of a vector.]

(OverVector[pq])
v=Last[(OverVector[pq])]-First[(OverVector[pq])]
Norm[v]

In comparison with (OverVector[pq]), (OverVector[ov]) is an equivalent vector with its base at the origin. (OverVector[av]) (see the definitions below) is an equivalent vector that does not have its base at the origin, and (OverVector[bv]) is a parallel vector that is not equivalent.

o={0,0};
(OverVector[ov])={o,v};
a={-3,1};
(OverVector[av])={a,a+v};
b={3,-2};
(OverVector[bv])={b,b-v};
Graphics[{Arrow[(OverVector[pq])],Arrow[(OverVector[ov])],Arrow[(OverVector[av])],Arrow[(OverVector[bv])]},Axes->True,PlotRange->4]

[By the way:  The semi-colon character, ;, is used to suppress the output of one line of Mathematica code (the line still gets “executed”)]

Now we’re going to re-define a and b to be scalars, instead of vectors.

The following commands draw (in two dimensions) a vector u, a vector v, a vector a u+b v, and an m by n grid of vectors  equivalent to u and v.

u={1,-2};v={3,1};
a=2;b=-1;
m=3;n=2;
o={0,0};
g0={Dotted,Arrowheads[0.02]};
g1=Table[Arrow[{(i-1)u+j v,i u+j v}],{i,-m+1,m},{j,-n,n}];
g2=Table[Arrow[{i u+(j-1) v,i u+j v}],{i,-m,m},{j,-n+1,n}];
g3={Dashing[{}],Thick,Arrowheads[0.03],Text[Style["u",Larger,Bold],1.1u,Background->White],Arrow[{o,u}],Text[Style["v",Larger,Bold],1.1v,Background->White],Arrow[{o,v}],Text[Style["au+bv",Larger,Bold],1.1(a u+b v),Background->White],Arrow[{o,a u+b v}]};
plot3=Graphics[Join[g0,g1,g2,g3],Axes->True]

Exercise 4 - 4 Pts

Choose a variety of values for u, v, a, b, m, and n in the above commands to investigate the following.  Give a precise geometric description of the relationship among the vectors u, v, and a u+b v.  Is there some special choice of u and v such that it would not be possible to obtain all vectors in two dimensions as linear combinations of u and v? Given an example of such a choice of  u and v.

Vectors in Three Dimensions

The default style of Plots in Mathematica is to show the bounding box with the axes along three of the edges: x is downward right diagonal, y is upward right diagonal, and z is upward vertical.  Use the mouse to obtain different viewpoints.  Dragging from side to side moves the graphic around the z axis, while dragging up and down makes the z axis tumble.  Pressing Ctrl while dragging up and down zooms in and out. Pressing Shift while dragging pans in the direction of the drag.

a={3,0,1};b={2,3,5};
ListPointPlot3D[{a,b}]

The ability to visualize is helped with the judicious use of some options.

ListPointPlot3D[{a,b},
AxesOrigin->{0,0,0},
PlotStyle->PointSize[0.03],PlotRange->{{-5,10},{-5,10},{-5,10}}]

Here are two functions that Patricia Oakley wrote that helped my ability to visualize.  myAxis returns graphic primitives, or “subroutines” that create an axis in the given component direction with the given label in the given color from lo to hi; the axis is dashed for negative values.  myPoint includes dashed lines indicating how one might plot the point by hand.  (You do not need to understand all of this code.)

myAxis[label_,color_,component_,lo_,hi_]:=Module[{e,g,o},
o={0,0,0};e=o;e[[component]]=1;
g={color,Text[label,1.05 hi e],Arrowheads[0.02],Arrow[{o,hi e}]};
If[lo<0,g=Join[g,{Dashed,Line[{lo e,o}],Dashing[{}]}]];
g];
myPoint[p_,color_]:={color,Dashed,Line[{{p[[1]],0,0},{p[[1]],p[[2]],0},p}],PointSize[Medium],Point[p],Dashing[{}]};

The following command makes use of myAxis and myPoint and moves the view point so that the result is similar to what we did by hand in the “pipe-cleaner” exercise.  You could change the colors to match your pipe cleaner axes.

Graphics3D[
Join[
myAxis["x",Orange,1,-2,7],
myAxis["y",Darker[Yellow],2,-5,7],
myAxis["z",Green,3,-9,8],
myPoint[a,Black],
myPoint[b,Black]
],
Axes->True,ViewPoint->{100,100,100}
]

Exercise 5 - 4 pts

Add to the above graph the point c=(-2,-5,-8) and the line segments (OverBar[ab]), (OverBar[bc]), and (OverBar[ca]).

Exercise 6 - 2 pts

Graph the three axes (pick an appropriate ViewPoint) to match the view point given in this diagram.

Lines in Three Dimensions

Graph the point (Subscript[P, 0])=⟨3,-1,4⟩, the vector v=⟨2,1,7⟩ based at (Subscript[P, 0]), and the line r(t)=(Subscript[P, 0])+t v for -2≤t≤2 with these commands:

p0={3,-1,4};v={2,1,7};
Graphics3D[Join[
myAxis["x",Orange,1,-5,5],
myAxis["y",Darker[Yellow],2,-5,5],
myAxis["z",Green,3,-5,5],{Black,PointSize[0.03],Point[p0]},
{Arrowheads[0.03],Arrow[{p0,p0+v}]},
{Line[{p0-3v,p0+3v}]}],
Axes->True,
ViewPoint->{100,100,100}]

Add the red point r(t)=(Subscript[P, 0])+t v with t controlled by a slider.

Manipulate[p0={3,-1,4};v={2,1,7};
Graphics3D[Join[
myAxis["x",Orange,1,-5,5],
myAxis["y",Darker[Yellow],2,-5,5],
myAxis["z",Green,3,-5,5],{Black,PointSize[0.03],Point[p0]},
{Arrowheads[0.03],Arrow[{p0,p0+v}]},{Line[{p0-3v,p0+3v}]},
{Red,Point[p0+t v]}],
Axes->True,
ViewPoint->{100,100,100}],
{{t,0},-2,2,0.1, Appearance->"Labeled"}]

Exercise 7 - 2 pts

Choose a variety of values for t in the above manipulator to investigate the following.  Give a precise geometric description of the vector r(t)=(Subscript[P, 0])+t v. Include in your description the relationship of (Subscript[P, 0]) and v to the line.

Exercise 8 - 3 pts

Graph the lines (Subscript[r, 1])(t)=⟨1,0,1⟩+t⟨3,3,5⟩ and (Subscript[r, 2])(t)=⟨3,6,1⟩+t⟨4,-2,7⟩ for t running from -3 to 3.  Rotate your graph this way and that to decide whether the two lines either do or do not intersect. State what you find.