Math 21a: Multivariable Calculus, Summer 2003, Oliver Knill
Mathematica Assignment

Support

Welcome to the Mathematica computer assignment. In case of technical problems with this assignment, please consult first the  page at http://www.courses.fas.harvard.edu/~maths21a/lab.html . If the problem is not listed in the frequently asked questions there and you are still stuck, send e-mail to maths21a@fas.harvard.edu. Please be aware that Mathematica is a  complex program demanding a lot of resources from computers, whether it is a Unix or Linux workstation, a Mac or a PC. Therefore, it is a good idea to save the notebook frequently during your work.  The actual assignment is at the very end of this notebook.

Cells

Mathematica notebooks are subdivided into cells, each of which is marked by a blue bracket at the right of the window.  These cells come in several different types.  For instance, the text you are currently reading is in a "text" cell, while the expression below is in an "input" cell.

[Graphics:Images/index_gr_1.gif]
[Graphics:Images/index_gr_2.gif]

You can tell the different types of cells apart by the font they use: text cells useTimes font, while input cells use a boldface Courier font.  Text cells usually contain instructions, descriptions, or explanations.  If you want Mathematica to evaluate a mathematical expression, you must put it in an input cell.  To create a new cell, position the cursor between two existing cells or after the last cell.  The cursor will then change from a vertical I-beam to a horizontal I-beam.  If you click there, Mathematica will draw a horizontal line across the window.  If you then start typing, Mathematica will create a new input cell for you.  Any new cell you create will automatically be an input cell.

Evaluating Expressions

All commands are entered into Mathematica as text.  To evalute an expression, click anywhere in the expression and press the <Enter> key on the keypad (not the one on the main keyboard) or hold down the <Shift> key and press <Enter>.  Mathematica will then think for a moment and write the answer below the expression.  Your input and Mathematica's output are labeled "In[#]: and Out[#]:" respectively, and Mathematica draws the input in boldface to distinguish it from the plain text output.  For example, to add the following two numbers, click the sum below and press <Enter>.

[Graphics:Images/index_gr_3.gif]
[Graphics:Images/index_gr_4.gif]

Throughout this Mathematica assignment, there are some expressions to evaluate, along with descriptive text.  Just go through it one cell at a time, reading the text and evaluating each of the expressions one by one.  In this notebook input cells are  boxed in blue for better readability.

Basic Operators

The basic operations are + (addition), - (subtraction), * (multiplication), / (division), and ^ (exponentiation).  Mathematica obeys the usual operator precedence, first performing exponentiation, then multiplication and division, and finally addition and subtraction.  If you want to group your operations differently, use parentheses ().  Try evaluating the following expressions:

[Graphics:Images/index_gr_5.gif]
[Graphics:Images/index_gr_6.gif]
[Graphics:Images/index_gr_7.gif]
[Graphics:Images/index_gr_8.gif]
[Graphics:Images/index_gr_9.gif]
[Graphics:Images/index_gr_10.gif]
[Graphics:Images/index_gr_11.gif]
[Graphics:Images/index_gr_12.gif]
[Graphics:Images/index_gr_13.gif]
[Graphics:Images/index_gr_14.gif]
[Graphics:Images/index_gr_15.gif]
[Graphics:Images/index_gr_16.gif]

If you put two expressions next to each other separated only by a space, Mathematica multiplies them, just like in normal mathematical notation.  This can be confusing if you just put two numbers next to each other, but it comes in handy with more complicated expressions.  Here are some examples:

[Graphics:Images/index_gr_17.gif]
[Graphics:Images/index_gr_18.gif]
[Graphics:Images/index_gr_19.gif]
[Graphics:Images/index_gr_20.gif]
Functions and Constants

Mathematica knows many functions and constants.  Some of the functions we will be using are Sin[x], Cos[x], Exp[x] (the exponential [Graphics:Images/index_gr_21.gif]), Log[x] (the natural logarithm [Graphics:Images/index_gr_22.gif]), Sqrt[x] (square root [Graphics:Images/index_gr_23.gif]), and Abs[x] (the absolute value |x|).  Note that the first letter of all built-in functions are capitalized, and you use square brackets [] to apply them, as opposed to regular parentheses ().  Some useful constants that Mathematica knows are Pi (or π) and E (or e ~~ 2.71828, the base for natural logarithms).  Again, be sure to capitalize the first letter.  Alternatively, you can use the palette of templates and symbols on the right of your screen to type in some of these operators and constants.  (If you do not see the palette, select from the menu File - Palettes - BasicInput.)  Here are some examples.

[Graphics:Images/index_gr_24.gif]
[Graphics:Images/index_gr_25.gif]

Just like in standard mathematical  notation, if you write two things next to each other with only a space between them, Mathematica multiplies them.

[Graphics:Images/index_gr_26.gif]
[Graphics:Images/index_gr_27.gif]

When you evaluated this next expression, note that Mathematica is clever enough to give you the exact answer, rather than a decimal approximation.

[Graphics:Images/index_gr_28.gif]
[Graphics:Images/index_gr_29.gif]

If you prefer a numeric approximation, add //N to the end of your expression.

[Graphics:Images/index_gr_30.gif]
[Graphics:Images/index_gr_31.gif]

or

[Graphics:Images/index_gr_32.gif]
[Graphics:Images/index_gr_33.gif]

You can evaluate expressions with arbitrary precision. To get the first 1000 digits of π for example, try

[Graphics:Images/index_gr_34.gif]
[Graphics:Images/index_gr_35.gif]

By the way, it is believed, that the digits of π occur with equal distribution. The next expression illustrates this
as well as how you can use built in functions to build more complex expressions.

[Graphics:Images/index_gr_36.gif]

[Graphics:Images/index_gr_37.gif]

[Graphics:Images/index_gr_38.gif]
Defining Variables and Functions

Mathematica lets you do both symbolic and numerical calculations using variables and functions.  You can use any string of letters for a variable name.  In addition to the usual variable names x, y, t, you can also use more descriptive names like theta or radius.  You can assign variables a value and then use that value in further computations.  Here are some examples:

[Graphics:Images/index_gr_39.gif]
[Graphics:Images/index_gr_40.gif]
[Graphics:Images/index_gr_41.gif]
[Graphics:Images/index_gr_42.gif]
[Graphics:Images/index_gr_43.gif]
[Graphics:Images/index_gr_44.gif]
[Graphics:Images/index_gr_45.gif]
[Graphics:Images/index_gr_46.gif]

Notice that if you write two variable names next to each other separated only by a space, Mathematica multiplies them, just like in normal mathematical notation.  However, you must take care to put in the space between your variable names.  Otherwise, Mathematica will think that you are talking about a single variable with a long name!  For instance, in the following cell, instead of multiplying a by b, Mathematica assumes you are using a new variable ab:

[Graphics:Images/index_gr_47.gif]
[Graphics:Images/index_gr_48.gif]

To define a function for Mathematica, we write f[x_] := value, where f is the name of the function, x is the variable, and value is the expression for the function.  Note that when defining a function, you again use square brackets [] instead of parentheses. In Mathematica, this distinction is very important; parentheses () are used to group expressions, while square brackets [] are used when applying a function.  When defining a function, you use colon-equal := instead of a normal equal sign, and you put an underscore _ immediately after the variable name.  This tells Mathematica to evaluate the right side only after plugging in the value of the variable.  Note that Mathematica does not give you an output when you define a function, but once a function is defined, you can apply it to any number you want, or even to a variable or expression.  When you are finished typing out your function definition in the input cell, you must type 'enter' on the keypad or hold down 'shift' and press 'enter' on the regular key board to tell Mathematica that your definition is ready. This tells Matheamtica to read the input cell. Give the following functions a try:

[Graphics:Images/index_gr_49.gif]
[Graphics:Images/index_gr_50.gif]
[Graphics:Images/index_gr_51.gif]
[Graphics:Images/index_gr_52.gif]
[Graphics:Images/index_gr_53.gif]

Here we define a function of two variables.  Note that your variable and function names can be as long or as descriptive as you like.

[Graphics:Images/index_gr_54.gif]
[Graphics:Images/index_gr_55.gif]
[Graphics:Images/index_gr_56.gif]

An other way to define the distance is to use vectors and the dot product to do the same thing

[Graphics:Images/index_gr_57.gif]
[Graphics:Images/index_gr_58.gif]
[Graphics:Images/index_gr_59.gif]
Graphing Functions

To graph a function, you need to tell Mathematica the function itself, the name of the dependent variable, and the domain of the function. For instance, to plot the function f(x) = [Graphics:Images/index_gr_60.gif] (called a Gaussian distribution function) in the domain --3 <= x <= 3 you would type:

[Graphics:Images/index_gr_61.gif]
[Graphics:Images/index_gr_62.gif]

[Graphics:Images/index_gr_63.gif]

[Graphics:Images/index_gr_64.gif]

In the Plot command, be sure to write f[x] including the variable name. If you just write f, Mathematica will think that f is a variable name, not a function. As a shorthand, you can skip the step where you define the function separately, and include that in the Plot command itself:

[Graphics:Images/index_gr_65.gif]

[Graphics:Images/index_gr_66.gif]

[Graphics:Images/index_gr_67.gif]
Curves in the plane
Parameterizing a Circle

As we have seen, Mathematica's Plot[] command lets us plot the graph of a function.  However, not all curves in the plane can be described as the graph of a function.  In particular, a function can have only one value of y for each value of x.  Suppose we want to draw the unit circle, given by the equation [Graphics:Images/index_gr_68.gif] + [Graphics:Images/index_gr_69.gif] = 1.  To draw it as a graph of a function, we first solve this equation for y to obtain [Graphics:Images/index_gr_70.gif].  But since there are two possible values for the square root, we cannot draw it as a graph of a single function.

Instead, we can plot the circle by treating the two coordinates [Graphics:Images/index_gr_71.gif] and [Graphics:Images/index_gr_72.gif] as functions of a third variable [Graphics:Images/index_gr_73.gif] called a parameter.  However, before getting to the circle, it is instructive to first plot the graphs of x and y as functions of t separately:

[Graphics:Images/index_gr_74.gif]
[Graphics:Images/index_gr_75.gif]

[Graphics:Images/index_gr_76.gif]

[Graphics:Images/index_gr_77.gif]
[Graphics:Images/index_gr_78.gif]
[Graphics:Images/index_gr_79.gif]

[Graphics:Images/index_gr_80.gif]

[Graphics:Images/index_gr_81.gif]

Now, to plot the circle, combine them both into a single  vector-valued   function [Graphics:Images/index_gr_82.gif].  We  can represent the vector-valued  function in Mathematica as follows:

[Graphics:Images/index_gr_83.gif]

Use Mathematica to compute the vector [Graphics:Images/index_gr_84.gif] corresponding to various values of t:

[Graphics:Images/index_gr_85.gif]
[Graphics:Images/index_gr_86.gif]
[Graphics:Images/index_gr_87.gif]
[Graphics:Images/index_gr_88.gif]
[Graphics:Images/index_gr_89.gif]
[Graphics:Images/index_gr_90.gif]

If you base these vectors at the origin, their endpoints describe a circle as t  varies from 0 to 2π.  In Mathematica, you plot this curve using the ParametricPlot[] command, which requires you to specify a vector-valued  function, the dependent variable, and the domain in the following format:

[Graphics:Images/index_gr_91.gif]
[Graphics:Images/index_gr_92.gif]

[Graphics:Images/index_gr_93.gif]

[Graphics:Images/index_gr_94.gif]

If we plot a predefined vector-valued function such as r[t], Mathematica generates a warning but proceeds with the parametric plot.  To prevent the warning message from being displayed, use the Evaluate[] version of the function r[t] to tell Mathematica to substitute the definition of r[t] into the function to be plotted, as shown in the example below.

[Graphics:Images/index_gr_95.gif]

[Graphics:Images/index_gr_96.gif]

[Graphics:Images/index_gr_97.gif]

Things can be done all at once as in the following example, which shows an ellipse:

[Graphics:Images/index_gr_98.gif]

[Graphics:Images/index_gr_99.gif]

[Graphics:Images/index_gr_100.gif]

To plot a polar graph  r(t) =t, we would do

[Graphics:Images/index_gr_101.gif]
[Graphics:Images/index_gr_102.gif]

[Graphics:Images/index_gr_103.gif]

[Graphics:Images/index_gr_104.gif]

Now play with the cells above to graph a spiral that winds around the origin three times. Here is an other example:

[Graphics:Images/index_gr_105.gif]

[Graphics:Images/index_gr_106.gif]

[Graphics:Images/index_gr_107.gif]
Curves in space

Plotting curves in three dimensions goes very similar:

[Graphics:Images/index_gr_108.gif]

[Graphics:Images/index_gr_109.gif]

[Graphics:Images/index_gr_110.gif]

Here is an other example, the  Slinky™. The  option PlotPoints->400 is added to the command to make Mathematica compute the value of the function at more points than usual, giving us better resolution.

[Graphics:Images/index_gr_111.gif]
[Graphics:Images/index_gr_112.gif]
[Graphics:Images/index_gr_113.gif]

[Graphics:Images/index_gr_114.gif]

[Graphics:Images/index_gr_115.gif]

If there is additional time, here is a task designed especially for fans of late-night infomercials.  Rotato Peeler is an amazing gadget that can peel an orange, apple, potato, or Kiwi in one piece.  It has a blade that sits on the skin of the fruit that slowly moves from the top to the bottom while the fruit turns (courtesy of a hand crank operated by various actors who probably have better things to do than peel fruit).  Assuming the fruit is a perfect sphere, we will draw the spiral path of the blade on the fruit's surface, wrapping around 10 times, using a parametric curve.  

[Graphics:Images/index_gr_116.gif]
[Graphics:Images/index_gr_117.gif]

[Graphics:Images/index_gr_118.gif]

[Graphics:Images/index_gr_119.gif]

Note: This curve involves two separate circular motions. The first is the horizontal motion in the xy-plane. The second is the vertical motion involving the radius in the xy-plane and the vertical height along the z axis. This is just half a circle going from the north pole to the south pole.  Combining the two motions, we obtain a potato peeler.

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 [Graphics:Images/index_gr_120.gif] and the names and range of the two independent variables.

For example, to graph the saddle [Graphics:Images/index_gr_121.gif] over the domain [Graphics:Images/index_gr_122.gif], [Graphics:Images/index_gr_123.gif],  have Mathematica evaluate the following cell:

[Graphics:Images/index_gr_124.gif]

[Graphics:Images/index_gr_125.gif]

[Graphics:Images/index_gr_126.gif]

One of the Plot option in Mathematica is PlotPoints-> n.  This option tells Mathematica to use n points to plot the function.  The default is [Graphics:Images/index_gr_127.gif].  Thus, if we include PlotPoints->30 in the Plot3D function, Mathematica would plot the surface with higher resolution. Here is an example to try:

[Graphics:Images/index_gr_128.gif]

[Graphics:Images/index_gr_129.gif]

[Graphics:Images/index_gr_130.gif]

You can look up other Plot3D options by typing

[Graphics:Images/index_gr_131.gif]
[Graphics:Images/index_gr_132.gif]
[Graphics:Images/index_gr_133.gif]
[Graphics:Images/index_gr_134.gif]

One questionmark alone gives a rough description of the function, two question marks provides you with
more detailed information.

[Graphics:Images/index_gr_135.gif]
[Graphics:Images/index_gr_136.gif]

The function [Graphics:Images/index_gr_137.gif] gives an upward-facing  hyperboloid. To verify, have Mathematica evaluate the following cell:

[Graphics:Images/index_gr_138.gif]

[Graphics:Images/index_gr_139.gif]

[Graphics:Images/index_gr_140.gif]

For practice, change the function in the Plot3D input above so as the bottom of the hyperboloid is [Graphics:Images/index_gr_141.gif] without the changing the shape of the hyperbolic. Tell Mathematica to plot the result. Before using Mathematica to graph the next surface, imagine what the surfaces should look like.  

[Graphics:Images/index_gr_142.gif]

[Graphics:Images/index_gr_143.gif]

[Graphics:Images/index_gr_144.gif]
Parametric Plots

To plot a surface defined by a function in polar coordinates, we will use the ParametricPlot3D function. For example, graph the surface which is given in cylindrical coordinates as [Graphics:Images/index_gr_145.gif] for [Graphics:Images/index_gr_146.gif].

[Graphics:Images/index_gr_147.gif]

[Graphics:Images/index_gr_148.gif]

[Graphics:Images/index_gr_149.gif]

Earlier, we used polar coordinates to describe the surface of a paraboloid [Graphics:Images/index_gr_150.gif] and used the ParametricPlot3D function in Mathematica to plot the surface. You can review the picture by evaluating:

[Graphics:Images/index_gr_151.gif]

[Graphics:Images/index_gr_152.gif]

[Graphics:Images/index_gr_153.gif]

For practice with another function, plot the top half of a sphere in polar coordinates ([Graphics:Images/index_gr_154.gif]).  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.
Polar coordinates are useful even when the function is not radially symmetric.  Consider the saddle function [Graphics:Images/index_gr_155.gif] which is in polarcoordinates x=r cos(t), y=r sin(t) given as
    [Graphics:Images/index_gr_156.gif].
Here is a graph of the saddle in polar coordinates :

[Graphics:Images/index_gr_157.gif]

[Graphics:Images/index_gr_158.gif]

[Graphics:Images/index_gr_159.gif]

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. The last example illustrates waves in pond:

[Graphics:Images/index_gr_160.gif]

[Graphics:Images/index_gr_161.gif]

[Graphics:Images/index_gr_162.gif]
Contour Lines and Level Curves

A contour line is the set of points on a surface where the height is constant.  If the surface is described by [Graphics:Images/index_gr_163.gif], then a contour line is the set of points satisfying the equation [Graphics:Images/index_gr_164.gif].  Visually, you can think of this as the intersection of the surface with the horizontal plane corresponding to the constant function [Graphics:Images/index_gr_165.gif].  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 [Graphics:Images/index_gr_166.gif] contour line of the saddle [Graphics:Images/index_gr_167.gif] is the intersection of the saddle surface with a plane:

[Graphics:Images/index_gr_168.gif]

[Graphics:Images/index_gr_169.gif]

[Graphics:Images/index_gr_170.gif]

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 [Graphics:Images/index_gr_171.gif] for [Graphics:Images/index_gr_172.gif] and [Graphics:Images/index_gr_173.gif] have Mathematica evaluate the next cell:

[Graphics:Images/index_gr_174.gif]

[Graphics:Images/index_gr_175.gif]

[Graphics:Images/index_gr_176.gif]
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, this function  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.)

Once  the package is loaded, 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 [Graphics:Images/index_gr_177.gif]-[Graphics:Images/index_gr_178.gif]

[Graphics:Images/index_gr_179.gif]

[Graphics:Images/index_gr_180.gif]

[Graphics:Images/index_gr_181.gif]

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 [Graphics:Images/index_gr_182.gif]. To do this first have Mathematica evaluate the next cell to define f(x,y) as [Graphics:Images/index_gr_183.gif].

[Graphics:Images/index_gr_184.gif]

Then have Mathematica evaluate

[Graphics:Images/index_gr_185.gif]

[Graphics:Images/index_gr_186.gif]

[Graphics:Images/index_gr_187.gif]

[Graphics:Images/index_gr_188.gif]

[Graphics:Images/index_gr_189.gif]
Implicit Surfaces

To draw surfaces of the form g(x,y,z)=0, we load the package ContourPlot3D first. For example, to draw an ellipsoid

[Graphics:Images/index_gr_190.gif]

[Graphics:Images/index_gr_191.gif]

[Graphics:Images/index_gr_192.gif]

You can produce amazing surfaces like this. Just try to go beyond quadrics

[Graphics:Images/index_gr_193.gif]

[Graphics:Images/index_gr_194.gif]

[Graphics:Images/index_gr_195.gif]
Symbolic calculations

Mathematica can be used as a calculator to do symbolic or numerical calculations. For example, to
look up integrals

[Graphics:Images/index_gr_196.gif]
[Graphics:Images/index_gr_197.gif]

or to calculate integrals numerically:

[Graphics:Images/index_gr_198.gif]
[Graphics:Images/index_gr_199.gif]

Also higher dimensional integrals can be computed. Here is a numerical integration of a two dimensional
integral:

[Graphics:Images/index_gr_200.gif]
[Graphics:Images/index_gr_201.gif]

Here is a numerical integration of a three dimensional integral:

[Graphics:Images/index_gr_202.gif]
[Graphics:Images/index_gr_203.gif]

Differentiation can be done in different ways:

[Graphics:Images/index_gr_204.gif]
[Graphics:Images/index_gr_205.gif]
[Graphics:Images/index_gr_206.gif]

which is a short hand notation for

[Graphics:Images/index_gr_207.gif]
[Graphics:Images/index_gr_208.gif]

To calculate a Taylor series of a function a a point, try

[Graphics:Images/index_gr_209.gif]
[Graphics:Images/index_gr_210.gif]
Graphics for illustration

Software like Mathematica is often also used for doing illustrations in books, articles, reports or the web. Here is an example:

[Graphics:Images/index_gr_211.gif]

[Graphics:Images/index_gr_212.gif]

[Graphics:Images/index_gr_213.gif]

This graphics object can be exported in different formats like Tif, Gif, Jpg or Postscript:

[Graphics:Images/index_gr_214.gif]

Besides polygons, you can use lines, discs or points.

[Graphics:Images/index_gr_215.gif]

[Graphics:Images/index_gr_216.gif]

[Graphics:Images/index_gr_217.gif]
Assignment

To get full credit for this Mathematica assignment, you have to hand in:

        1)  A printout of a graph of a function f(x,y) of two variables of your choice.
        2) A printout of a parametric surface r(u,v) =(x(u,v),y(u,v),z(u,v)) of your choice.
        3) A printout of a curve r(t)=(x(t),y(t)) in the plane  of your choice
        4) A printout of an implicit surface g(x,y,z)=0.
        5)  A numerical calculation for the arc length of an ellipse with semi axes 2 and 3.
        6)  A printout of a graphics object of your choice, which involves discs, lines, polygons or points.
Your examples should be different from any example which appears in this notebook.  If you find something cool during your experiments, feel free to include it also. In order to work on your project it is a good idea to save this notebook first as a different document, do the assignment directly in that notebook and print out the relevant pages at the very end.

Harvard University, Mathematics s21a Summer, 2003, Oliver Knill


Converted by Mathematica      August 15, 2003