Graphing Curves and Surfaces in Mathematica
Math 211  Prof. Leise

Work through the examples and then do the exercises at the bottom of the file. Save your file and email to tleise@amherst.edu. If you worked as a pair, be sure to state both your names in the email message.

Introduction to Mathematica

The purpose of today's lab is to get acquainted with Mathematica by doing some basic calculations and plotting.

Three rules of thumb to remember about Mathematica:

1) Commands and functions like Sin[x] and Sqrt[16] begin with capital letters.  Also, constants like Pi are capitalized.

2) Use square brackets with commands and functions (not parentheses).  For example, Cos[x] or Exp[x^2].

3) Press shift-enter to evaluate a group of commands (see brackets to far right).

Work through the examples below to get the hang of Mathematica.

Example 1.  Evaluate the command line below by putting the cursor anywhere on that line and pressing shift-enter.  Note that saying 2 Pi means 2 times Pi.  This command plots the graph of y=cos(x) for x from 0 to 2π.

Plot[Cos[x],{x,0,2 Pi}]

Example 2.  Put your cursor just below this cell so that a long horizontal line appears, then click.  This  creates a new command line.  Have Mathematica multiply 345 by 29.   Press shift-enter to evaluate.  To multiply negative numbers or do other arithmetical grouping, use parentheses: (-345)(29+12).

Example 3.  Find the square root of 4489 by using the command Sqrt[4489].  Also find the square root of 4500.  Note that Mathematica gives the answer in exact form.  If you want an decimal approximation, use N[number] (N stands for numerical approximation), e.g., N[Sqrt[4500]].

Example 4.  Mathematica can do some algebraic and trigonometric simplification.  For example, see what happens if you ask for the cosine of the arcsine of x:  Cos[ArcSin[x]].

Example 5.  To evaluate (e^x), write Exp[x]; to find the natural logarithm, use Log[x].  Try finding decimal approximations for (e^3) and for ln4 (natural log of 4).

Example 6.  One of the major purposes for using Mathematica in Math 211 is for plotting curves and surfaces.  Imitate the plotting command in Example 1 to plot the following curves:

(a) sin(x) for 0≤x≤4π

(b) ln(4x) for 1≤x≤5 (remember to use Log)

(c)  (e^x) for -2≤x≤2 (you can use Exp[x] or  (e^x))

Example 7.  To plot multiple graphs on the same set of axes, put the functions inside curly braces, as in the following example:

Plot[{Sin[x],Cos[x],1-x},{x,0,2 Pi}]

Plot the square root of x and x-squared on the same plot.

Example 8.  Mathematica can take derivatives; use the command D[f[x],x], for example, D[Sin[x],x].  Try taking the derivatives of Exp[x^2] and of Sin[x]Cos[x].

Example 9.  Mathematica can find some integrals; use the command Integrate[f[x],x] for indefinite integrals and Integrate[f[x],{x,a,b}] for definite integrals. For example, Integrate[Sin[x],x] or  Integrate[Sin[x],{x,0,Pi}].  Try finding the indefinite integral of Log[x^2] and the definite integral of Sin[x]Cos[x] for x from 0 to Pi/2.

Plotting Lines and Surfaces

Explicit equations give one variable in terms of another: y==3x+1 (line)   or   z==2x-3y-4 (plane).  In contrast, implicit equations involves mixes of the variable, for example, 3x-y-1==0 (plane)  or  x^2+y^2+z^2==1 (sphere).  For today, we will focus on graphing explicit equations.

The double equal sign indicates an equation in Mathematica, in contrast to y=3 which sets y to have the value 3.

To plot explicit equations, use Plot and Plot3D, which each take the expression on the right hand side of the equation (not the full equation).

For example, to plot y==3x+1, use the following command:

Plot[3x+1,{x,-2,2}]

To graph multiple curves, make a list in curly braces inside the Plot command:

Plot[{3x+1,-x+4},{x,-2,2},PlotStyle->{Red,Blue}]

Plot3D takes the expression you want to set equal to z:

Plot3D[2x-3y-4,{x,-2,2},{y,-1,1}]

You can grab the 3D plot and move your mouse to see the surface from different perspectives.

To show multiple surfaces in the same plot, remember to make a list in curly braces:

Plot3D[{ x+3 y,3-x-y},{x,-1,1},{y,-1,1}]

Now we’ll look at a few non-planar surfaces that will be important in Math 211.

Exercise 1. Graph the surface described by z==x^2+3 y^2.  This type of surface is called an elliptic paraboloid (cross-sections with z fixed are ellipses and cross-sections with x or y fixed are parabolas).

Exercise 2. Graph the surface described by z==x^2-y^2.  This type of surface is called an hyperbolic paraboloid (cross-sections with z fixed are hyperbolas and cross-sections with x or y fixed are parabolas).

Exercise 3. Graph the surface described by x^2 + 4 y^2 + 2 z^2 == 1 (you’ll need to solve this for z and graph both solutions).  This is an ellipsoid.  Be sure to choose intervals that show the entire surface with good resolution (no sharp corners or cutoff parts).

For the remaining exercises, you may work by hand or in Mathematica, but please record your answers in this file. While there are other geometrical ways to answer these exercises, please use them to practice working with vectors.

Exercise 4.  Use 3D vectors to determine the length of the diagonal of a unit cube.

PolyhedronData["Cube"]

Exercise 5.  Use the dot product to determine the angle between the diagonal of a unit cube and an adjacent edge.

Exercise 6.  Use 2D vectors to determine the coordinates of the vertices of a regular hexagon with sides of unit length, assigning the center to be the origin.  Use the fact that the unit vector making an angle θ with the positive x-axis is <cosθ,sinθ>.