Tutorial 01
Mathematica as an Expensive Calculator

Copyright © 2003 by Bill Titus, Carleton College,
Department of Physics and Astronomy, Northfield, MN  55057-4025
March 1, 2003

Part 1 - Getting Started

Topics and Skills

1.  Launching Mathematica from the Mathematica icon
2.  Brief definition of the notebook front end and the kernel
3.  Opening a Mathematica notebook from within Mathematica
4.  Saving a Mathematica notebook under a different name

Step 1.  Launch Mathematica's front end by double-clicking the Mathematica icon.

Comment 1

1.  When you open up Mathematica, a blank worksheet will appear with the heading  Untitled-1.
2.  Mathematica comes in two parts, the notebook front end and the kernel.
3.  The notebook front end, which is activated when Mathematica is launched, allows the user to input information into Mathematica and allows Mathematica to output information to the user.  The notebook front end thus serves as a graphical user interface.
4.  The kernel is the mathematical engine.  It consists of the programming code that does all the calculations and manipulations required of Mathematica.  The kernel occupies lots and lots of memory.  It's not loaded when you initially launch Mathematica, but only when you enter information into Mathematica's front end for the first time by pressing the (enter) key in an active cell.

Step 2.  Select  File / Open  from the Mathematica menu and load the notebook  Tutorial01.nb  found in the workshop folder.

Step 3.  Since you're going to edit this notebook, you'll need to make a copy of it and work with that copy.  To do so, select  File / SaveAs  from the Mathematica menu and save the notebook in the workshop folder under the name  myTutorial01.nb.

Comment 3

1.  The  nb  extension is used by Mathematica across all computer platforms so that a given operating system will know that this file was produced by Mathematica and will open the file with the Mathematica application present on that computer.
2.  Once a notebook is saved, you can double-click on its icon and it will open up automatically in Mathematica.
3.  Mathematica can malfunction.  It can crash at the most inconvenient time, and thus it's best to save your results frequently.  To do so, select  Save  from the Mathematica menu.  If the notebook doesn't have a name, you'll be prompted for one; if it has a name, the current changes will be saved without further interruption.  In the past, Mathematica has sometimes had problems saving directly over the network to network accounts, and it might be best to save to the local hard drive and then when you're completely done with the notebook, transfer the file over the network to your account.
4.  In most of the following tutorials, you won't have to do Step 3 unless you want to.

Part 2 - Multiplication and Input/Output Cells

Topics and Skills

1.  The multiplication operator  *
2.  Basic properties of input and output cells
3.  Round parentheses to group numerical operations
4.  Atomic type:  Integer

Problem 1

Beth, the geologist, can walk at a top speed of  6 miles per hour  but can only maintain this speed for about  2 hours.  How far can Beth travel in this time period?

Step 1.  Use Mathematica as a calculator to multiply  6  by  2,  thereby finding the distance traveled by the Beth.  To do so, you'll need to open a cell, type in the numbers (using the symbol  *  for multiplication), and then have Mathematica evaluate the result by sending your information to the Mathematica kernel.

To open a cell (a region in your notebook contained within a cell bracket), move the cursor below this cell until its shape changes to a horizontal bar.  Then click the mouse and type in the appropriate numbers.   As soon as you type in the first number, a cell bracket will appear to the right.  When you finish typing in  6*2,  press  (enter)  to send the results to the kernel.

Comment 1

1.  The first time you enter any result, it takes a little time for Mathematica to respond since it has to load the kernel.  Mathematica only loads the kernel once during a given session.
2.  After you press  (enter),  several things happened.  The input in the old cell is now preceded with  In[1].  This notation indicates that the cell is an "input" type, where results were entered into Mathematica and then sent to the Mathematica kernel.  The number  1  indicates that this is the first such input cell that has been entered during this current session.
3.  The notation  Out[1]  indicates the cell is an "output" type, where results, in this case the number  12,  coming back from the Mathematica kernel are displayed.
4.  The cell brackets for  In[1]  and  Out[1]  are grouped together by another cell bracket so as to indicate that these cells are related to one another.

Step 2.  Open a cell below and repeat the calculation for Beth, but this time use  3  for the time instead of   2.  Then open another cell directly above your last input cell and do the same calculation.  In both cases, press the  (enter)  key to send the results to the kernel.

Comment 2

1.  The numerical value for the index number contained within the square brackets of  In[]  and  Out[]  for your new cells should be  2  and  3,  respectively.
2.  Note that index numbers are not sequential down the page; they represent the order in which input and output occur in real time during the current session.

Step 3.   Mathematica stores all its input and output in temporary memory and the results can be retrieved using the cell index number.  In a cell below, type In[1] and then press  (enter).  Repeat with  Out[1].

Step 4.  Now go back to cell you created in Step 1, highlight the cell bracket containing both the input and output cells, and press the  (delete)  key to make these two cells disappear.  Repeat Step 3 in a cell below and observed that  In[1] and  Out[1]  still exist in Mathematica's temporary memory even though the cell that created them has disappeared.

Comment 4

1.  Even if the cells that you enter no only longer appear in your notebook, Mathematica stores that information in temporary memory.  However, if you quit Mathematica, only the information that visually appears in your notebook will be saved.

Step 5.  Now repeat the original calculation for geologist Beth, but replace the symbol  *  with a space.

Comment 5

1.  You should get the same result as before since Mathematica interprets a "white" space between two numbers as multiplication.  As a matter of fact, you can have any number of white spaces between numbers and Mathematica will still interpret the spaces as a single multiplication.
2.  I would suggest that you not use white spaces for multiplication; sometimes it's difficult to see whether there's a space present or not and you may accidentally produce an error in your calculation if you're not careful.

Step 6.  You can also enclose the numbers in your calculation in "round" parentheses with no  *  between the parentheses.  Try this out by opening a cell below and entering   (5)(3).

Comment 6

1.  Round parentheses is overkill in this example, but you'll often find such parentheses useful to group numerical and algebraic operations together.
2.  The type of quantities you've worked with so far are integers.  Integers are one of the basic "atomic" types in Mathematica, the fundamental quantities on which Mathematica is based.  In the next part of this notebook, you're going to work with two other atomic types:  rationals and reals.

Step 7.  Before you go on to Part 3, practice your Mathematica skill by calculating  11 factorial,  i.e.,  11 × 10 × 9 × ... × 2 × 1  in the cell below.

Comment 7

1.  You should find a numerical value of  39916800.
2.  When Mathematica does integer multiplication, it keeps all the integers in the final result, no matter how many.

Step 8.  Since Mathematica can occasionally crash, it's wise to save your results frequently.  Do so now by selecting  Save  from the Mathematica menu.

Part 3 - Addition and Division

Topics and Skills

1.  Atomic type:  Rational
2.  The addition operator   +
3.  Editing old cells
4.  Atomic type:  Real
5.  The division operator  /
6.  Forcing a numerical evaluation using  //N

Problem 2

George, the old geologist, can only maintain his top speed of  4 miles per hour  for  2/3  of an hour.  How far does he travel in this time period?

Step 1.  Do the appropriate calculation in the cell below.  Use round parentheses to surround the fraction  2/3  so it's clear that you're multiplying the integer  4  by the rational fraction  2/3.

Comment 1

1.  Mathematica considers a rational number (one of the form  n/m,  where  n  and  m  are integers) to be an atomic type and doesn't evaluate it in terms of a real number, i.e., decimal number.  However, Mathematica will simplify the results such the only single numbers appear in the numerator and in the denominator, and those numbers have no common factors.  A result like  8/3  is viewed by Mathematica as having infinite accuracy, as do integers.  
2.  Note that in Step 1, the multiplication of an integer by a rational is expressed in terms of a rational.

Step 2.  Try adding the two rational fractions  1/2  and  4/5  to the integer  5.  Use the symbol   +   for addition and parentheses to surround the individual fractions.

Comment 2

1.  You don't really need the parentheses here, but the parentheses make the order of operations clear, especially if you don't know which operation Mathematica will do first.  It's always wise to be safe rather than sorry.

Step 3.  Suppose you want the real number version of the result in Step 1, i.e., the result expressed as a decimal number instead of a rational number.  One way to do this is to use the atomic type "real" for the numbers involved in the calculation.  In the cell below, do the same calculation as in Step 1, but express  4  as  4.0,   2  as  2.0,  and  3  as  3.0.  Your result should be the decimal number equivalent of  8/3  to six significant figures.

Comment 3

1.  With reals, the symbol  /  implies division.
2.  Go back to the cell you just typed in and see that you don't need to have a zero after the decimal points in order to get the correct result.  To do so, use the edit keys to delete the zeros.  Then, with the cursor anywhere in the cell, press  (enter)  and send the results to the kernel.  I tend to use a zero after a decimal point since I find bare decimal points ugly.  But that's just a personal preference.

Step 4.  See what happens to your calculation if only one of the numbers is real, while the other two are integers.  The result shouldn't be surprising.

Step 5.  Another way to obtain a real result is to convert an integer or rational result to a real using the symbol combination  //N .  Type in   4*(2/3)//N   in the cell below and send the results to the kernel.

Comment 5

1.  In a later tutorial, you'll see that  //N   tacked on to the end of a result applies the built-in Mathematica function  N[]  to the previous result.  The function  N[]  converts the result to a real number.  For now, don't worry about the syntax for this "postfix" notation.  Just remember that if you want to convert an integer or rational to a real, add //N   after the result.
2.  Note that there is no space between the two slashes.

Part 4 - Editing and Warning Messages

Topics and Skills

1.  The use of copy and paste as an editing procedure
2.  Mathematica warning messages

Problem 3

To compensate for his age, George travels at  4 miles per hour for  2/3  of an hour and then at  2 miles per hour  for  4/3  of an hour.  What's the total distance traveled by George?

Step 1.  Carry out the above calculation in a cell below.  Make liberal use of round parentheses to group the proper arithmetic operations together.  Express your final result as a real number.

Step 2.  Suppose you want to repeat the above calculation, but replace  2 miles per hour  by  2.28 miles per hour.  One way to do so is to edit the old input cell.  But if you do, you loose the visual record of the old cell.  You can, however, copy and paste the old cell to a new one, thereby retaining the old input cell permanently in the notebook.  

Copy the last input cell to a cell below.  To do so, highlight the old cell's bracket by clicking the mouse over the bracket.  Then press the key combination  (apple) (c),  click the cursor below your last cell when you see the cursor become a horizontal bar, and finally press  (apple) (v).  Now, in your new cell, replace the appropriate  2  by  2.82  and enter the calculation.

Comment 2

1.  You can use standard commands to edit the contents of any input cell.  If you forget the keyboard equivalent of a command, go to Mathematica's  Edit  menu on the main menu bar where the keyboard equivalent of the command is displayed to the right of the menu item.
2.  Don't try to edit the contents of an output cell.  Sometimes bizarre things can happen if you do.  OK, I know you're going to try this, but be forewarned.

Step 3.  Copy and paste your last input cell to a new cell below.  Then edit out the first parenthesis and see what Mathematica tells you when you enter the results.

Comment 3

1.  It should be fairly obvious from Mathematica's warning message that there's an unpaired parenthesis in your expression.
2.  Mathematica's warning messages are meant to be helpful.  Sometimes they are, but many times they're not.
3.  One way to get a feeling for the various warning messages is to purposely make mistakes.  Feel free to make all the mistakes you want in Mathematica.  It's a wonderful way to learn.

Part 5 - Subtraction

Topics and Skills

1.  The subtraction operator  -

Problem 4

What's the difference in the distances traveled by George (Step 1 of Part 4)  and Beth (Step 1 of Part 2)?

Step 1.  Instead of redoing the calculations for the distances traveled by George and Beth, refer to the outputs of the appropriate cells using the  Out[]  notation.  Then use the symbol  -  for subtraction.

Part 6 - Mathematica Constants

Topics and Skills

1.  Built-in Mathematica constants:  Pi  and  E
2.  Using the input palette for generating special symbols
3.  Another functional notation for  N
4.  The Mathematica command   ScientificForm[]
5.  Deleting cells

Problem 5

George has been captured some mountain people and is required to run about a track at a speed of  15.0 m/s  for  20.0 s.  What must be the radius  r  of the track if George goes around once during this time period?  Remember, the circumference of a circle is  2π r,  where   π   is  3.14  to three significant figures.

Step 1.  Do the calculation in a cell below, assuming  π  is  3.14.  Express the speed and time as decimal numbers.  Be sure to use round parentheses to make the order of the arithmetic operations clear.  You should find that  r  is  47.7707 .

Comment 1

1.  Mathematica has a number of built-in constants, for example., Mathematica uses  Pi  for   π   and  E  for the exponential number  e = 2.71828...

Step 2.  Repeat the calculation in Step 1 in a cell below, but now use the symbol  Pi  for  π.

Comment 2

1.  The results for Step 1 and 2 differ slightly because in Step 1 you approximated  π   to three significant figures.

Step 3.  If you find the symbol  Pi  ugly, you can use the Greek letter  π  directly.  Repeat the calculation in Step 2 in a cell below, but now use the symbol   π  that you generate by selecting the appropriate symbol from the basic input palette to the right of the notebook.

Comment 3

1.  Mathematica won't numerically evaluate its built-in constants unless they are part of a "real" calculation or unless you use the  N  function.  Mathematica assumes its constants have infinite accuracy.

Step 4.  The numerical results you found using the  N  function have all been given to six significant figures.  You might expect that Mathematica is capable of higher accuracy, and indeed it is.  Activate the cell below that evaluates  π  to  50  significant figures.

N[π,50]

Comment 4

1.  This function notation shouldn't look strange.  Here  N  is the name of the function and the square brackets are used to contain the arguments of the function, with multiple arguments separated by commas within the square brackets.  The second argument tell Mathematica how many digits after the decimal point you want, although nothing appears to happen if that number is  16  or less.  Try this assertion out by editing your last cell.
2.  See what happens if you ask for 500 decimal places.
3.  Note that for numerical output that extends over several lines, Mathematica uses the dotted backslash at the end of the line to signify a line break.

Step 5.  Evaluate the exponential number  E  to  5000  significant figures.

Comment 5

1.  Mathematica is capable of infinite accuracy, although in practice the accuracy depends on the amount of computer memory available and the time you're willing to spend.

Step 6.  Sometimes you'd like to delete a cell, like the one above containing  E  to  5000 significant figures.  To do so, highlight the cell bracket with a mouse click and then press either  (apple)(x)  or  (delete).  Use this technique to eliminate the output cell in Step 5.

Step 7.  Scientists tend to quote numerical results using scientific notation (powers of ten), thereby making clear the number of significant figures.  You can do this type of formatting in Mathematica by using the built-in Mathematica function  ScientificForm[].  The following cell illustrates one technique to format the results in Step 1 to three significant figures using scientific notation.  Look over the code and then activate the cell.

ScientificForm[(15*20)/(2*3.14),2]

Comment 7

1.  Note that after applying  ScientificForm[],  the  Out[] notation is changed to indicate the special type of formatting present.
2.  Go back to the cell above and see what happens when you change the integer in the second argument.  The result shouldn't be a surprise.

Part 7 - Power and Symbols

Topics and Skills

1.  The power operator  ^
2.  Atomic type:  Symbol
3.  User-defined symbols
4.  Algebra with Mathematica

Problem 6

George wishes to escape from the mountain people.  To do so, he must travel a distance of  5.0×(10^4)m  to get to the nearest forest.  If he allows himself two days for the journey, what must be his average speed in  m/s?

Step 1.  Do the numerical calculation in a cell below in terms of real numbers; use the symbol  ^  for the power operator.

Step 2.  Now activate the following cell which repeats the calculation in Step 1 but includes symbols for the units.  Mathematica will keep track of the symbols as it proceeds with the calculation.  Here the symbol  m  is used for meters and the symbol  s  for seconds.  Your final result will be expressed in terms of   m/s.

(5.0*10^4 m)/(2*24*3600 s)

Comment 2

1.  A symbol is another atomic type.  The others that were mentioned in this tutorial were integer, rational, and real.
2.  There's nothing magic about using  m  for meters and  s  for seconds.  Go back to the cell in Step 2, replace  m  by  meters  and  s  by  seconds,  and enter the results.
3.  Any symbol Mathematica defines, like  Pi  or  E,  starts with an upper case letter.  It's thus wise to start your symbols with lower case letters to avoid confusion or possible conflicts with Mathematica symbols.  A symbol can't start with a number because Mathematica will interpret the result as multiplication; for example, Mathematica treats  4m  as  4*m.
4.  Go back to the cell in Step 2 and verify that you don't need a space between the symbol and its preceding number.

Step 3.   Instead of using  ^  for power, you can use a superscript.  To create  10  raised to the 4th power in superscript notation,  type  10,  press the key combination  (ctrl)( ^),  type in  4,  and finally get back to the regular line by typing  (ctrl)(spacebar).   Then enter the results.  Do this procedure in a new cell below.

Comment 3

1.  Subscripts are generate by using the key combination  (ctrl)(-).
2.  Division, in the form of  (3.0/4.0),  can be generated using (ctrl)(/).

Step 4.  Mathematica does algebra with symbols.  To illustrate this feature, go back to Step 1 of Part 6 and copy and paste the input cell in a cell below.  Then insert units of  m/s  and  s  where appropriate, and enter the results.  You should find that the final result is expressed in terms of  m.  Mathematica has canceled the units of  s  for you.

Step 5.  Suppose you have a calculation that you decide to break up over more than one line in a cell.  When you do so, you must be careful.  If you have a line of code that Mathematica can evaluate without error, Mathematica will evaluate it.  Activate the code in the following cell for the calculation in Step 2 and see what happens.

(5.0*(10^2) m)
/(2*24*3600 s)

Comment 5

1.  Note there are two output cells.   The first one arises from the calculation on the first line.  The second one is a warning message produced by the "incomplete" calculation on the second line.
2.  Go back to Step 5 and correct the situation, but in a manner so that the calculation is still distributed over two lines.

Part 8 - Save Your Notebook

Topics and Skills

1.  Elimination of output cells before a final save

Step 1.  In order to conserve on memory, it's useful, especially if you have lots of data in your output cells or if your output cells contains plots, to remove such cells from the notebook before you save your notebook.  For example, it's not uncommon for a Mathematica notebook containing graphics to require several MB or more of storage space.  Your input cells are still contained in the notebook, and your output can be easily regenerated by entering the input cells once you reload your notebook.  Recall that you can remove a single cell by highlighting the cell bracket and pressing  (apple)(x).  However this process would be very time consuming to do on all your output cells.  So to accomplish the removal of all output at once, go to the menu bar and activate  Kernel / Delete All Output Cell.  A panel will come up asking whether you really want to do this or not.  Press  OK  and the output cell disappear.

Delete all the output cells in your notebook and then check to see if they are indeed gone.

Step 2.  Save the notebook one last time and quit Mathematica.