Math 21a: Multivariable Calculus, Fall 2003
 Reading Period Lab

Mathematica Review and Additional Features

There is no assignment to be turned in for this lab - it's basically just for fun.  The goal of this lab is to give you a chance to check out some more cool things you can do with Mathematica while you start your 21a review to get geared up for the final.  First off, here is a brief review of the language rules for Mathematica.

Entering Mathematica Expressions
To create a new input cell, position the cursor between two existing cells so that Mathematica switches the cursor to a horizontal I-beam, click the mouse, and then start typing.
If you type into an existing output cell, Mathematica will make a copy of the cell and allow you to edit the content.
If you have two expressions in the same input cell, separate them by ; so Mathematica knows that they are two different expressions and should be evaluated separately.

Evaluating Mathematica Expressions
To evalute an expression or to have Mathematica input or read an expression, click anywhere in the input cell and press simultaneously the 'shift' and 'enter' key or the 'enter' key on the keypad.

Mathematica Syntax
Capitalize first letter of built-in functions and variables.
	Correct:     Sin[x], E^Pi
		Incorrect:  sin[x], e^pi, LOG[e]
Use () to group expressions and [] to enclose function arguments.
	Correct:    (1+2)*3, Sin[Pi], E^Log[5]
		Incorrect:  {[1+2]*3}, Sin(pi), e^ln 5
Use [], x_, and := when defining functions.
	Correct:     f[x_]:=g[x]^2+3x		
	Incorrect:   f(x)=g(x)^2+3x
Remember to evaluate function definitions by pressing  simultaneously the keys 'shift' and 'enter' or just the key 'enter' on the keypad over the cell before using them.

Implicit Multiplication
Separate expressions with a space to denote multiplication.
	Correct: (-b+Sqrt[b^2-4a c])/(2a)		
	Incorrect: (-b+Sqrt[b^2-4ac])/(2a)

Mathematica Notebook and Outlines
A notebook is a Mathematica document.  It is organized as an outline as indicated by the blue brackets on the right.  You can double-click  the blue brackets to collapse and expand a particular section of the outline.  Thus, if your Mathematica notebook all of a sudden "disappears," most likely, you have collapsed the entire notebook.  Just double click the right-most blue bracket to expand the outline again.

3D Surface Plots

Mathematica uses the Plot3D function to graph a surface defined by a function of two variables.  To plot such a graph, you need to specify the function f(x, y) and the names and domains of the two independent variables in the following format:
	Plot3D[function to be plotted, {variable1, lower range, upper range},
	 {variable2, lower range, upper range}]

For example, to graph the saddle f(x, y) = (x^2) - (y^2) over the domain -2 ≤ x ≤ 2, -2 ≤ y ≤ 2,  have Mathematica evaluate the following cell:

Plot3D[x^2 - y^2, {x, -2, 2}, {y, -2, 2}, 
      BoxRatios -> Automatic]

The BoxRatios->Automatic option at the end of the Plot3D function tells Mathematica to use the same scale for the x, y, and z axes.  (This is similar to the AspectRatio->Automatic option that we saw in the last lab.)  However, when the graph is significantly longer in one direction than another, this option can get in the way.  In those cases, just remove the option and Mathematica will plot the graph using non-uniform   scaling. For example, have Mathematica evaluate

Plot3D[x^2 - y^2, {x, -2, 2}, {y, -2, 2}]

Another Plot option in Mathematica is PlotPoints-> n.  This option tells Mathematica to use n points to plot the function.  The default is n = 15.  Thus, if we include PlotPoints->30 in the Plot3D function, Mathematica would plot the surface with higher resolution. Here is an example to try:

Plot3D[x^2 - y^2, {x, -2, 2}, {y, -2, 2},  
      PlotPoints -> 30]

Function of Surfaces in Polar Coordinates

If a surface is radially symmetric (meaning that it depends only on the distance away from the origin), it is often helpful to use polar coordinates (r, θ) for the plane instead of the usual Cartesian coordinates (x, y).  Here, r=(Sqrt[(x^2) + (y^2)]) is the distance from the point (x, y) to the origin, and t   is the angle between the vector x i+y j and the unit vector i.  The two coordinate systems are related by the equations:
	x = r cos(t)		y = r sin(t)
Thus, instead of describing the surface by z = f(x, y), we can describe the surface in polar coordinates as z = g(r, t).  For example, the surface z = f(x, y) = (x^2) + (y^2) can also be described by z = g(r, t) = (r^2) where we have used the identity (x^2) + (y^2) = (r^2) to derive the polar form of the function.

To plot a surface defined by a function in polar coordinates, we will use the ParametricPlot3D function:
	ParametricPlot3D[{r Cos[t], r Sin[t], Evaluate[f[r,t]]}, 
	 {r, (Subscript[radius, min]), (Subscript[radius, max])}, {t, (Subscript[angle, min]), (Subscript[angle, max])}]
Basically, we are telling Mathematica that the points in our surface are described by points x i+y j+f(r, t) k for (Subscript[radius, min])≤r≤(Subscript[radius, max]) and (Subscript[angle, min])≤t≤(Subscript[angle, max]).  Since we are using polar coordinates, we can rewrite the points as: r cos(t) i+r sin(t) j+f(r, t) k. Note that we used here 't' instead of θ to simplify the typing in Mathematica. (Any letter except r would do well as a replacement).

For example, graph the surface z = (r^2) for 0 ≤ r ≤ 2.  Unless otherwise specified, we will assume that the range of  t  is 0 ≤ t ≤ 2  π.

ParametricPlot3D[{r Cos[t],  r Sin[t],  r^2},  {r,  0,  
        2},  {t,  0,  2  Pi}]

Combining Multiple Plots into a Single Graph

In Mathematica, in addition to plotting a single function at a time, we can also combine the plot of several functions onto a single graph.  To combine multiple graphs, we use the Show function.
	Show[graph1, graph2, ...]
For example, to plot a circle without using parametric plots, we first define the plot of the upper half, define the plot of the lower half, and combine the two plots into one graph as shown below:

Graph1 = Plot[Sqrt[25 - x^2],  {x, -5, 5}]

Graph2 = Plot[-Sqrt[25 - x^2],  {x, -5, 5}]

Show[Graph1,  Graph2,  AspectRatio -> Automatic]

Like all other Mathematica plot functions, we can add plot options to the end of the Show function to achieve a particular effect.  In this case, we added the AspectRatio->Automatic option to tell Mathematica to use the same scale for the x and y axes.

Plotting Surfaces in Cartesian Coordinates

Paraboloid

An upward facing   paraboloid is described by the function f(x, y) = (x^2) + (y^2).

Plot3D[x^2 + y^2, {x, -2, 2}, {y, -2, 2}, 
      BoxRatios -> Automatic]

For practice, change the Plot3D input  to get a downward facing parabola and have Mathematica redo the plot.

Cone

The function f(x, y) = 2 (Sqrt[ (x^2) +  (y^2)]) describes an upward facing cone. To verify, tell Mathematica to evaluate the following:

Plot3D[ 2  Sqrt[x^2 + y^2], {x, -2, 2}, {y, -2, 2}, 
      BoxRatios -> Automatic]

For practice, change the function in the Plot3D input above to give a cone with elliptical instead of circular cross sections. Have Mathematica plot the result.

The Hyperboloid

The function f(x, y) = (Sqrt[1 + (x^2) +  (y^2)]) gives an upward-facing  hyperboloid. To verify, have Mathematica evaluate the following cell:

Plot3D[Sqrt[1 + x^2 + y^2], {x, -3, 3}, {y, -3, 3}, 
      BoxRatios -> Automatic]

For practice, change the function in the Plot3D input above so as the bottom of the hyperboloid is (0, 0, 5) without the changing the shape of the hyperbolic. Tell Mathematica to plot the result.

Sinusoidal Waves

The following four surfaces are made out of sine waves, but they look quite different from each other:
	f(x, y)  =  sin(x + y)		f(x, y)  =  sin(x)  +  sin(y)
	f(x, y)  =  sin(xy)		f(x, y)  =  sin(x) sin(y)
Before using Mathematica to graph these surfaces, imagine what the surfaces should look like.  Then, have Mathematica graph these functions in turn by evaluating the next four cells:

Plot3D[Sin[x + y], {x, -2 Pi,  2  Pi}, {y, -2 Pi, 2  Pi}, 
      BoxRatios -> Automatic, PlotPoints -> 30]

Plot3D[Sin[x] + Sin[y], {x, -2 Pi, 2  Pi}, {y, -2 Pi, 2  Pi}, 
      BoxRatios -> Automatic,  PlotPoints -> 30]

Plot3D[Sin[x y], {x, -Pi,  Pi}, {y, -Pi, Pi}, 
      BoxRatios -> Automatic, PlotPoints -> 30]

Plot3D[Sin[x] Sin[y], {x, -2 Pi, 2 Pi}, {y, -2 Pi, 2  Pi}, 
      BoxRatios -> Automatic, PlotPoints -> 30]

Plotting Surfaces in Polar Coordinates

Radially Symmetric Functions

Earlier, we used polar coordinates to describe the surface of a paraboloid f(r, θ) = (r^2) and used the ParametricPlot3D function in Mathematica to plot the surface. You can review the picture by evaluating:

ParametricPlot3D[{r Cos[t], r Sin[t], r^2},  {r, 0, 2},  {t, 0, 
        2  Pi}]

For practice with another function, plot the top half of a sphere in polar coordinates (f(x, y) = (Sqrt[1 - (x^2) - (y^2)])).  You will need to adjust the limits of the domain to avoid taking the square root of a negative number. To make this plot, fill in the missing data in the next cell and have Mathematica evaluate the result.

ParametricPlot3D[{r Cos[t], 
        r Sin[t], __},  {r, __, __},  {t, __, __}]

For a second practice example, have Mathematica plot the 1-sheeted   hyperboloid given by the function f(x, y) = (Sqrt[-1 + (x^2) + (y^2)]).  Do this by filling in the missing data in the next cell and have Mathematica evaluate the result.Again, be careful when specifying the domain so that the square root makes sense.

ParametricPlot3D[{r Cos[t], 
        r Sin[t], __},  {r, __, __},  {t, __, __}]

The Saddle

Polar coordinates are useful even when the function is not radially symmetric.  Consider the saddle function f(x, y) = (x^2) - (y^2).  In polar coordinates, we have:
	\(TraditionalForm\`f(x, 
          y) = \(\((r\ cos\ t)\)\^2 - \((r\ sin  t)\)\^2 = \(\(r\^2\)(\(cos\^2\) t\  - \(sin\^2\) t) = r\^2\ \(cos(2  t)\)\)\)\),
where we used the double angle formula for cosine in the last equality.

Graph the saddle in polar coordinates by having Mathematica evaluate the next cell.

ParametricPlot3D[{r Cos[t], r Sin[t], r^2 Cos[2  t]},  {r, 0, 
        2},  {t, 0, 2  Pi}]

For practice, change the function in the ParametricPlot3D input above so that the resulting plot is a saddle with three instead of two pairs of upward and downward pointing directions.  Have Mathematica plot the result. This graph is also known as a Monkey Saddle.

Exercise: Ripples on a Pond

Invent a function in polar coordinates that describes ripples on the surface of a pond, as shown in the graph below.  The waves radiate outwards from the origin, moving at a constant speed.  Thus, the wave height depends only on the distance to the origin.  Specifically, assume that the amplitude (height) of the waves decreases in proportion to (1/(5 + r)), where r is the distance from the origin.

Plot your function describing the ripples by filling in the missing data in the next cell and hving Mathematica evaluate the result.

ParametricPlot3D[{r Cos[t], 
        r Sin[t], __},  {r, __, __},  {t, __, __}]

Level Curves and Gradients

Contour Lines

A contour line is the set of points on a surface where the height is constant.  If the surface is described by z = f(x, y), then a contour line is the set of points satisfying the equation z = f(x, y) = c.  Visually, you can think of this as the intersection of the surface with the horizontal plane corresponding to the constant function z = c.  We will use the Show function to combine the graph of the surface and the plane in order to examine the points of intersection.

For example, the z = 1 contour line of the saddle f(x, y) = (x^2) - (y^2) is the intersection of the saddle surface with the plane in this graph:

Show[
      Plot3D[x^2 - y^2, {x, -2, 2}, {y, -2, 
          2}], 
Plot3D[
        1, {x, -2, 2}, {y, -2, 2}]]

Vary the height of the plane to see how the contour lines change as you go from the top to the bottom.

Level Curves

A level curve is the projection of a contour line down onto the xy-plane.  A contour plot is a 2D graph showing multiple level curves of a function.  This gives you an alternative method of looking at a function of two variables.  If you have gone hiking, you may have seen a topographical map.  A topographical map is just a contour plot of the height function, which lets you add elevation information to an otherwise 2D map.  Another example of a contour plot is a weather map showing isobars, lines of equal barometric pressure.  In this, case the function value is not height, so it is not as natural to graph it in three dimensions.

Mathematica can draw level curves using the ContourPlot command.  To  draw the level curves of the saddle function f(x, y) = (x^2) - (y^2) for -2 ≤ x ≤ 2 and -2 ≤ y ≤ 2 have Mathematica evaluate the next cell:

ContourPlot[x^2 - y^2, {x, -2 , 2 }, {y, -2, 2}, 
      AspectRatio -> Automatic]

Note that Mathematica shades the areas between the level curves, using lighter shades for regions of higher function values.

Plot the level curves for f(x, y) = sin(x) + sin(y) by filling in the missing data in the next cell and have Mathematica evaluate the result.

ContourPlot[__, {x, __ , __ }, {y, __, __}, 
      AspectRatio -> Automatic]

Gradients

Not only can Mathematica graph functions in two and three dimensions, but it can also perform symbolic differentiation.  To tell Mathematica to differentiate, you write D[f,x], which is Mathematica's way of saying (df/dx). 
Compute the derivative of cos(x), sin(x), (e^(-10 (x^2))), and (x^x) using the D function by successively filling in the missing data in the next cell and having Mathematica evaluate the result.

D[__, x]

You can also use these derivatives in more complicated expressions.  Recall that if f(x, y) is a function of two variables, its gradient is the vector field ∇f=∂(f/∂)xi+∂(f/∂)yj.  We can define a Mathematica function to compute the gradient vectors in the next cell. Hav eMathematica evaluate it.

Grad[f_, x_, y_] := {D[f, x], D[f, y]}

Note that in Mathematica, variables can represent not only numeric values, but also graphs, functions, other variables, etc.  In the above function definition, the variable f is used to represent the function to be differentiated.  Variables x and y are used to represent the variable of differentiation.  Thus, to find the gradient of the saddle function f(x, y) = (x^2) - (y^2), have Mathematica evaluate:

Grad[x^2 - y^2, x, y]

Compute the gradient of f(x, y) = sin(x) sin(y) and g(r, s) = sin(r s) by filling in the missing data in the next cell and having Mathematica evaluate the result.

Grad[__, __, __]

Vector Fields

Once you have a vector field such as the gradient, you can have Mathematica plot it for you using the function 
 PlotVectorField.  Unlike the Mathematica functions we have seen so far, the function 
 PlotVectorField is not in the standard Mathematica package.  To use this add-on function, we need to first load the Graphics`PlotField` package.  (Note the use of backquotes.) To do so, ask Mathematica to evaluate the next cell.

<< Graphics`PlotField`

Once we have loaded the package, we can use the PlotVectorField function to plot our vector fields.  Like what we have seen in ParametricPlot3D, we need to tell Mathematica to evaluate the Gradient before plotting the vector field. Tell Mathematica to evaluate the next cell in order to obtain a picture of the gradient of (x^2)-(y^2)

PlotVectorField[
      Evaluate[Grad[ x^2 - y^2, x, y]], {x, -1, 1}, {y, -1, 
        1}]

Examples

We can also draw both the level curves and gradient vector field on the same graph.  Use the Show function to combine the level curves and gradient vector field of the saddle f(x, y) = (x^2) - (y^2). To do this first have Mathematica evaluate the next cell to define f(x,y) as (x^2) - (y^2).

f[x_, y_] := x^2 - y^2

Then have Mathematica evaluate

Show[ContourPlot[f[x, y], {x, -2 , 2 }, {y, -2, 2}], 
      PlotVectorField[
        Evaluate[Grad[f[x, y], x, y]], {x, -2, 2}, {y, -2, 
          2}]]

Plot the level curves and gradient vector field of the paraboloid f(x, y) = (x^2) + (y^2)which takes its minimum value at the origin:

f[x_, y_] := x^2 + y^2

Show[ContourPlot[f[x, y], {x, -2 , 2 }, {y, -2, 2}], 
      PlotVectorField[
        Evaluate[Grad[f[x, y], x, y]], {x, -2, 2}, {y, -2, 
          2}]]

The graph of f(x,y)=-(Sqrt[1 + (x^2) + (y^2)]) is a downward pointing hyperbola. To plot its  level curves and gradient vector field, have Mathematica evaluate the next two cells

f[x_, y_] := -Sqrt[1 + x^2 + y^2]

Show[ContourPlot[f[x, y], {x, -2 , 2 }, {y, -2, 2}], 
      PlotVectorField[
        Evaluate[Grad[f[x, y], x, y]], {x, -2, 2}, {y, -2, 
          2}]]

By the way, note how these plots illustrate several important facts about the gradients and level curves:
1.	The gradient points in the direction of steepest ascent along the graph.
2.	The gradient is always orthogonal to the level curve.
3.	Where the level curves are close together, the gradient vectors are longer because the graph is steeper there. Conversely, where the graph is flatter, the level curves are far apart and the gradient vectors are shorter.
4.	At a critical point, the gradient vector is zero. Near a critical point, the gradient vectors point towards a local maximum or away from a local minimum. At a saddle point, you have gradient vectors both pointing towards and away from the point.
5.	Near a local maximum or minimum, the level curves close up.

Exploring

Plot the graphs as well as the level curves of some of the functions below or use your own functions.  You can copy the Mathematica cells above and paste them in the space below.
	f(x, y) = sin x sin y		f(x, y) = sin (Sqrt[(x^2) + (y^2)])
	f(x, y) = 
        cos (Sqrt[(((x - 1))^2) + (y^2)]) + 
          cos (Sqrt[(((x + 1))^2) + (y^2)])
	f(x, y) = (1/(1 + (x^2) + (y^2)))		f(x, 
          y) = ((1 - (x^2) - (y^2))/((((1 - x))^2) + (y^2)))
	f(x, y) = (e^x) cos y		f(x, y) = (e^(-((x^2) + (y^2))))

Note this lab has been just for your own enjoyment and review - there is nothing due - have fun studying for the final!

Harvard University, Mathematics 21a 1999-2003