Multivariable Calculus Computer Algebra Project

Oliver knill, Harvard University, knill@math.harvard.edu, Math21a, Fall 2008

Welcome to the Mathematica project of Fall 2008! This notebook will be your guide to get started. It also contains the assignement at the very end. If you work on a project, save it frequently and make backups.  Have fun!

Content in a mathematica file is organized as cells. Cells can be evaluated by grabbing the bracket to the right, holding down the shift key and hitting return. Try it out in the next cell, the "Devils graph":

In[1]:=

"lab_1.gif"

Out[1]=

"lab_2.gif"

After evaluating the cell, an output cell has been added to the input cell. The output cell can be made interactive as in the following example:

In[2]:=

"lab_3.gif"

Out[2]=

"lab_4.gif"

Lets get started. I suggest you read through the examples and evaluate the ones you are interested in which are hopefully all. If you are unpatient, grab the most outer bracket to the right which contains the entire notebook and evaluate it. All the cells will be evaluated.

A calculator

Anything you can do on a graphics calculator you can do with a computer algebra system, only better and more accurately. Here is an example to compute a numerical expression

In[3]:=

"lab_5.gif"

Out[3]=

"lab_6.gif"

It did not compute it numerically and left  square roots of integers untouched. To get a numerical value or a numerical value or a value with accuracy 200 digits, access the previous expression (called %) and

In[4]:=

"lab_7.gif"

Out[4]=

"lab_8.gif"

Lets play with this:

In[5]:=

"lab_9.gif"

Out[5]=

"lab_10.gif"

Or look at the list of prime numbers

In[6]:=

"lab_11.gif"

Out[6]=

"lab_12.gif"

or binominal expressions

In[7]:=

"lab_13.gif"

Out[7]=

"lab_14.gif"

Some single variable calculus

One of the most common tasks one does with a computer algebra system  is to differentiate and integrate. Here are several ways to write a derivative:

In[8]:=

"lab_15.gif"

Out[8]=

"lab_16.gif"

In[9]:=

"lab_17.gif"

Out[9]=

"lab_18.gif"

In[10]:=

"lab_19.gif"

Out[10]=

"lab_20.gif"

Or integration

In[11]:=

"lab_21.gif"

Out[11]=

"lab_22.gif"

Here we can manipulate algebraic expressions with the parameter n, the number of derivatives.

In[12]:=

"lab_23.gif"

Out[12]=

"lab_24.gif"

The derivative of a function gives the rate of change. Here is an animation which illustrates that

In[13]:=

"lab_25.gif"

Out[16]=

"lab_26.gif"

Here is an experiment which explores the convergence of p-series:

In[17]:=

"lab_27.gif"

Out[17]=

"lab_28.gif"

A Taylor expansion:

In[18]:=

"lab_29.gif"

Out[18]=

"lab_30.gif"

But lets move to multivariable calculus!

Graphs

We have already seen how to graph functions of one variables. Here is an example, where the graph is filled.

In[19]:=

"lab_31.gif"

Out[19]=

"lab_32.gif"

But lets move to space. With Plot3D, you can plot a function of three variables. After evaluation of the next cell, you get a graphics object, which you can manipulate with the mouse.

In[20]:=

"lab_33.gif"

Out[20]=

"lab_34.gif"

Also here, we can fill the region between the graph and the xy-plane, if we like. Lets fill it with blue water, with opacity 0.9 so that we can look through the water.

In[21]:=

"lab_35.gif"

Out[21]=

"lab_36.gif"

Several graphs at once colored differently and transparent:

In[22]:=

"lab_37.gif"

Out[22]=

"lab_38.gif"

Here is a way to fill the region between two graphs.

In[23]:=

"lab_39.gif"

Out[23]=

"lab_40.gif"

Quadrics

In[24]:=

"lab_41.gif"

Out[24]=

"lab_42.gif"

Parametrized Surfaces

In[25]:=

"lab_43.gif"

Out[25]=

"lab_44.gif"

Here is an version which can be manipulated:

In[26]:=

"lab_45.gif"

Out[26]=

"lab_46.gif"

Want to be closer to the surface. Just change the View angle. Lets make it also a bit more transparent:

In[27]:=

"lab_47.gif"

Out[27]=

"lab_48.gif"

Parametrized Curves

Parametric curves are plotted with the same command than parametrized surfaces. There is only one paremater.

In[28]:=

"lab_49.gif"

Out[28]=

"lab_50.gif"

In[29]:=

"lab_51.gif"

Out[32]=

"lab_52.gif"

Polar Curves

Simple formulas can produce realistic leaf shapes:

In[33]:=

"lab_53.gif"

Out[33]=

"lab_54.gif"

Splines

Here is an example on how points of a curve can be manipulated:

In[34]:=

"lab_55.gif"

Out[34]=

"lab_56.gif"

A more  complicated is by Marcel Schreiber from the Wolfram demonstration project shows how to build more complex interfaces:

In[35]:=

"lab_57.gif"

Out[35]=

"lab_58.gif"

Knots

Some knots are already built in:

In[36]:=

"lab_59.gif"

Out[36]=

"lab_60.gif"

Contour maps of a function

Here we see a bifurcation. By deforming the function the critical point changes

In[37]:=

"lab_61.gif"

Out[37]=

"lab_62.gif"

Implicit surfaces

Implicit surfaces are now plotted with CountourPlot. By default, a contour map is drawn.

In[38]:=

"lab_63.gif"

Out[38]=

"lab_64.gif"

We can specify which contour we want to see by specifying the set of values.

In[39]:=

"lab_65.gif"

Out[39]=

"lab_66.gif"

Again this can be made into a manipulation object.

In[40]:=

"lab_67.gif"

Out[40]=

"lab_68.gif"

We can change the shading

In[41]:=

"lab_69.gif"

Out[41]=

"lab_70.gif"

Having defined a graphics valued function, we can animate it:

In[42]:=

"lab_71.gif"

Out[42]=

"lab_72.gif"

Surfaces in spherical coordinates

Go Crimson!

In[43]:=

"lab_73.gif"

Out[43]=

"lab_74.gif"

Second derivative test

The following procedure and example speaks for itself

In[44]:=

"lab_75.gif"

Out[45]//TableForm=

x y D f_xx Type f
-2 0 -64 0 saddle 0
-1 -1 32 -6 maximum 2
-1 1 32 6 minimum -2
0 0 -16 0 saddle 0
1 -1 32 6 minimum -2
1 1 32 -6 maximum 2
2 0 -64 0 saddle 0
0 -2 -64 0 saddle 0
0 2 -64 0 saddle 0

Lagrange multipliers

mathematica has in general no problems to solve Lagrange problems in with one constraint

In[46]:=

"lab_76.gif"

Out[48]=

"lab_77.gif"

or two constraints

In[49]:=

"lab_78.gif"

Out[52]=

"lab_79.gif"

Surface area

Given a parametrized surface r[u, v], we can calculate its area.The following example finds the surface area of the sphere.

In[53]:=

"lab_80.gif"

Out[55]=

"lab_81.gif"

In the next example, we have a surface, where the integral can not be evaluated symbolically and where we
switch to a numerical computation of the integral with NIntegrate.

In[56]:=

"lab_82.gif"

Out[58]=

"lab_83.gif"

Double and triple integrals

You write integrals in the same order than they appear on paper. Mathematica knows how to change the order of integration.

In[59]:=

"lab_84.gif"

Out[59]=

"lab_85.gif"

Some integrals can not be computed in closed form like:

In[60]:=

"lab_86.gif"

Out[60]=

"lab_87.gif"

you can still compute the integral numerically:

In[61]:=

"lab_88.gif"

Out[61]=

"lab_89.gif"

Triple integrals can be done in the same way. Lets compute the volume of a sphere:

In[62]:=

"lab_90.gif"

Out[62]=

"lab_91.gif"

or the moment of inertia. Of course this would be much simpler in spherical coordinates, but Mathematica can do it the hard way too and even symbolically for a sphere of radius L:

In[63]:=

"lab_92.gif"

Out[63]=

"lab_93.gif"

Vector fields

Plotting vector fields needs an external library. Here is

In[64]:=

"lab_94.gif"

Out[65]=

Graphics:None

Gradient fields can be plotted directly:

In[66]:=

"lab_96.gif"

Out[66]=

Graphics:None

In three dimensions, you get an object which you can turn with the mouse:

In[67]:=

"lab_98.gif"

Out[67]=

"lab_99.gif"

And again there is a gradient field version

In[68]:=

"lab_100.gif"

Out[68]=

"lab_101.gif"

We all know that gradients are perpendicular to the level curves:

In[69]:=

"lab_102.gif"

Out[69]=

"lab_103.gif"

Line Integrals

Given a vector field F and a curve r (t), line integrals can be computed with a command like

In[70]:=

"lab_104.gif"

Out[72]=

"lab_105.gif"

If you wanted to make this into a procedure, you could define

In[73]:=

"lab_106.gif"

and then give a vector field and a path as an argument:

In[74]:=

"lab_107.gif"

Out[76]=

"lab_108.gif"

Flux integrals

"lab_109.gif"

In[77]:=

"lab_110.gif"

Out[79]=

"lab_111.gif"

In the next example, we have a surface, where the integral can not be evaluated symbolically and where we
switch to a numerical computation of the integral with NIntegrate.

In[80]:=

"lab_112.gif"

Out[82]=

"lab_113.gif"

Greens theorem

The curl of a 2 D vector field is a scalar function.We define a Mathematica procedure which takes a vector field as an argument and returns a function :

In[83]:=

"lab_114.gif"

For example:

In[84]:=

"lab_115.gif"

Out[84]=

"lab_116.gif"

Greens theorem assures that the double integral of curl (F) over a region G is the line integral of F along the boundary.Lets check that : first compute the line integral :

In[85]:=

"lab_117.gif"

Out[86]=

"lab_118.gif"

Now compute the double integral of the curl of F over the region :

In[87]:=

"lab_119.gif"

Out[88]=

"lab_120.gif"

These two numbers should agree.To see the error, we ask to give the result with 20 digits :

In[89]:=

"lab_121.gif"

Out[89]=

"lab_122.gif"

Stokes theorem

The curl of a 3 D vector field is a vector field.We define a Mathematica procedure which takes a vector field as an argument and returns vector field which is the curl.

In[90]:=

"lab_123.gif"


For example

In[91]:=

"lab_124.gif"

Out[92]=

"lab_125.gif"

Stokes theorem assures that the flux of curl (F) through a surface S is the line integral of F along the boundary.Lets check that in a special case where S is the south hemisphere.The boundary of this surfaced is a circle in the xy - plane but note the orientation!

In[93]:=

"lab_126.gif"

Out[95]=

"lab_127.gif"

Now compute the double integral of the curl of F over the surface.Note that the parametrization is chosen such that the normal vector points outside.We compute the integral numerically :

In[96]:=

"lab_128.gif"

Out[100]=

"lab_129.gif"

It is your call to see whether the two numbers agree.

Graphics

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

In[101]:=

"lab_130.gif"

Out[104]=

"lab_131.gif"

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

In[105]:=

"lab_132.gif"

Out[108]=

"lab_133.gif"

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

In[110]:=

"lab_134.gif"

Out[120]=

"lab_135.gif"

Or include pictures from the web:

In[121]:=

"lab_136.gif"

Out[129]=

"lab_137.gif"

In[130]:=

"lab_138.gif"

Out[134]=

"lab_139.gif"

We can manipulate the pictures:

In[135]:=

"lab_140.gif"

Out[135]=

"lab_141.gif"

Sound

Lets play a 2000 Herz sound for 5 seconds:

In[136]:=

"lab_142.gif"

Out[136]=

"lab_143.gif"

We can try to manipulate this:

In[137]:=

"lab_144.gif"

Out[137]=

"lab_145.gif"

Objects

Lets draw a snow cube polyhedron, a semiregular polyedron. With the opacity, we can adjust, how transparent the surface is

In[138]:=

"lab_146.gif"

Out[138]=

"lab_147.gif"

Here are all 5 regular polyhedra matched into each other

In[139]:=

"lab_148.gif"

Out[139]=

"lab_149.gif"

This is an example illustrating visualizing data. Here is a horse:

In[140]:=

"lab_150.gif"

Out[140]=

"lab_151.gif"

Prefer a Zebra?

In[141]:=

"lab_152.gif"

Out[141]=

"lab_153.gif"

Want to look through it

In[142]:=

"lab_154.gif"

Out[142]=

"lab_155.gif"

What other objects are built in?

In[143]:=

"lab_156.gif"

Out[143]=

"lab_157.gif"

Chemical Data

There is a large amount of Chemical data available. The first time you use, it it will take some time to process. But once the picture appears you can turn the molecule.

In[144]:=

"lab_158.gif"

Out[144]=

"lab_159.gif"

If you want to know what data are available, you can try:

In[145]:=

"lab_160.gif"

Out[145]=

"lab_161.gif"

To see all molecules available evaluate:

In[146]:=

"lab_162.gif"

Out[146]=

"lab_163.gif"

Try for example:

In[147]:=

"lab_164.gif"

Out[147]=

"lab_165.gif"

Financial Data

Also financial data are available. Here is a plot of the Dow Jones data:

In[148]:=

"lab_166.gif"

Out[148]=

"lab_167.gif"

What happend with the DJI since January 1, 1998?

In[149]:=

"lab_168.gif"

Out[149]=

"lab_169.gif"

What happend with the stock of the company General Motors between January 1, 2006 and November 1, 2008?

In[150]:=

"lab_170.gif"

Out[150]=

"lab_171.gif"

Linguistic

Look up the meaning of a wrod

In[151]:=

"lab_172.gif"

Out[151]=

"lab_173.gif"

For solving crossword puzzles, the following is handy: lookup all words which start with "o" contain "er" and end with "r"

In[152]:=

"lab_174.gif"

Out[152]=

"lab_175.gif"

Lets define a function, which gives us words close to a given word :

In[153]:=

"lab_176.gif"

Which words are in distance 4 from "Harvard"?

In[154]:=

"lab_177.gif"

Out[154]=

"lab_178.gif"

Lets see what is close to  the word "Calculus" and illustrate this

In[155]:=

"lab_179.gif"

Out[156]=

"lab_180.gif"

The Gettyisburg Address :

In[157]:=

"lab_181.gif"

Out[157]=

"lab_182.gif"

See the words of the Gettysburg address:

In[158]:=

"lab_183.gif"

Out[158]=

"lab_184.gif"

Show their frequency :

In[159]:=

"lab_185.gif"

Out[159]=

"lab_186.gif"

Lets see only the numbers, sorted  and reversed

In[160]:=

"lab_187.gif"

In[161]:=

"lab_188.gif"

Out[161]=

"lab_189.gif"

What texts are available?

In[162]:=

"lab_190.gif"

Out[162]=

"lab_191.gif"

We also can get texts from the web : lets get Mark Twains "Adventures of Huckleberry Finn"

In[163]:=

"lab_192.gif"

The 1001'th word is

In[164]:=

"lab_193.gif"

Out[164]=

"lab_194.gif"

The word distribution. A bit more than 1/2 of the words appear once.

In[165]:=

"lab_195.gif"

Out[165]=

"lab_196.gif"

Animations

The following animation is a mechanical system. Run the cell and you will see an animated picture. You can turn around the picture while it moves.

In[166]:=

"lab_197.gif"

Out[179]=

"lab_198.gif"

We can export this animation to Flash to embed it into a website for example:

In[180]:=

"lab_199.gif"

Out[180]=

"lab_200.gif"

Image manipulation

Lets get a picture from the web:

In[181]:=

"lab_201.gif"

Out[181]=

"lab_202.gif"

The following matrix of color vectors contains the image data:

In[182]:=

"lab_203.gif"

We get the (red,green,blue) color values for the pixel at position (3,4) as follows

In[183]:=

"lab_204.gif"

Out[183]=

"lab_205.gif"

Lets do some woodoo on Oliver and invert the colors:

In[184]:=

"lab_206.gif"

Out[184]=

"lab_207.gif"

Assignment

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

1) (2 points) A printout of a parametric surface r (u, v) = (x (u, v), y (u, v), z (u, v)) of your choice.
2) (2 points) A printout of a spherical plot or an implicit surface of your choice.
3) (2 points) Make a molecule-plot, a  stock-data plot or a text word-analysis-plot of your choice.
4) (2 points) Compute numerically the moment of inertia of a body x^4+y^4+z^4<1 spinning around the z axes.
5) (2 points) Produce graphics object of your choice (you can include pictures or work in 3D).
Your examples should be different from any example which appear 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 on a printer.The assignments have to be printed out and turned in the last class.

  Oliver Knill, November 18, 2008, email: knill@math.harvard.edu

Spikey Created with Wolfram Mathematica 6