Example 01
Mathematica as a Simple Calculator

Copyright „ 1993 by Bill Titus, Carleton College, 
Department of Physics and Astronomy, Northfield, MN  55057-4025
September 6, 1993

Topics and Skills

1.   Launching MMA.
2.   Opening a MMA notebook from within MMA.
3.   Brief definition of the kernel and the notebook front end.
4.   MMA as a calculator.
5.   Simple arithmetic operators.
6.   Basic information on input and output cells.
7.   Editing contents of cells.
8.   Special MMA objects (atomic types).
9.   Saving a MMA notebook.
10. Opening a MMA notebook from a file name or icon.

Problem

A ball is thrown vertically upward at  20 m/s  from the top of the Olin Hall of Science.  What's the ball's velocity and position at the time  to =  3.0 s  after launch?

The basic free fall equations, assuming the x-axis points vertically upward and that the origin of the coordinate system is at the launch point, are:

v[t] = v[0] - gt
 x[t] = v[0]t - gt2/2

The initial information is then  v[0] = 20 m/s,   to = 3.0 s, and  g = 9.8 m/s2.

Solution

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

Comment 1

1.  The kernel is the mathematical engine.
2.  The notebook front end is for input and output.

Step 2 - Use the MMA menu system to located and open the file  Ex01.ma

Step 3 -  You'll first use MMA as a calculator and find  v[to], assuming all the numbers are integers.  Take  g = 9  and use   *   for multiplication and  -  for subtraction.  Move the cursor below this cell.  When it becomes a horizontal bar, click the mouse and type in the appropriate numbers.  Press  <Enter>  to send the results to the kernel.

Comment 3

1.  The first time you enter any result, it takes what seems like a long time for MMA to respond.  Why?
2.  To activate a cell, you can use either  <Shift><Return>  or  <Enter>.

Step 4 - Now calculate  x[to],  again assuming all the number are integers.  Use  ^  for  power  and  /  for division.

Comment 4

1.  Arithmetic operators:  addition, subtraction, multiplication, power, and division.
2.  Special objects (atomic types):   Integers and Rationals.
3.  Input and output cells:  the  In[] and  Out[]  notation.
4.  Cells:  definition, boundaries, brackets, cursor, and creation.
5.  Standard use for parentheses to group numbers.

Step 5 - In a cell below, repeat the calculations for  v[to]  and  x[to]  with reals, now using  g = 9.8.  Use parentheses to make the order of the evaluation clear.

Comment 5

1.  Atomic type:  Real.
2.  Real and Integer types can be mixed, but the result is a Real.

Step 6 -  Copy the last input cell for  x[to]   to a new cell below by highlighting the old cell's bracket, pressing  <Command>-c, clicking the cursor below Step 6's cell when you see the cusor become a horizontal bar, and then pressing <Command>-v.  Now replace the two  *  with spaces and enter the calculation.  Note that your cursor can be anywhere in the input cell when you press  <Enter>.  What do you find?

Comment 6

1.  Use standard commands to edit the contents of cells.  If you forget what to use, go to MMA's  Edit  menu.

Step 7 - Editing old output.  Go back to the previous input, change  g  to  9.81, and enter the calculation.   What do you observe about the reference numbers for  In[]  and  Out[]?

Comment 7

1.  What's the index order for the  In[]'s  (input)  and  Out[]'s (output)?
2.  Ways to reference cells:
    a.  %,  %%,  %%% ...
    b.  %n
    c.  In[n]  or  Out[n];  n  can be negative, so  In[-1]  is equivalent to %.

Step 8 - Check for the presence of the missing input and output cells using some of the cell reference options discussed in Comment 7.

Step 9 -  You're now going to calculate  v[to]  and  x[to]  using reals along with symbols for units.  Right after your numbers, type a space and then type in unit(s) using the symbols  m  for meters  and   sec  for seconds.  Use parentheses to make the groupings clear.

Comment 9

1.  Error message :  MMA is warning you that there's a MMA symbol very similar to yours.  Error messages are meant to be helpful.  Sometimes they are and sometimes they're not.
2.  Go back to the last input, stick in an extra left parenthesis somewhere, and enter the results.  What happens?   
3.  Atomic type:  Symbol:  m,  sec
     a.  MMA defines its own symbols with upper case letters.
     b.  Start your symbols with lower case letters.
     c.  Don't start symbols with numbers.  What do you think  5sec  means?
4.  In your calculation, did you really need a space between the number and its symbol?
5.  Atomic type:  Complex:  5.0 + 6.5I.   Complex distinguishes between Integer, Rational, and Real.
6.  Atomic type:  String:  "my string".

Step 10  -  Use  Save As  in MMA's Notebook menu to save this notebook in your directory under myEx01.  Then quit MMA.  Look in your directory to see whether the file was indeed saved.  What extension to you see on the file?

Comment 10

1.  MMA file extension  ma  is added to the file under NeXTStep, but not on Macs.
2.  Under NeXTStep, there's also a  .mb  file which contains, among other things, formating information.  You can usually eliminate the  .mb  file without undue harm.

Step 11 - Reload your file  myEx01  into MMA by double-clicking on the file's name or its icon.

Comment 11

1.  Notice that the  In[]  and  Out[] aren't saved.  Why?

Practice Problems

1.  Calculate the area of a circle of radius  2.5 m,  keeping the unit for the meter in your calculation.

2.  Use the unit conversion method to find the number of seconds in a year, starting with  60 sec = 1 min.  Use parentheses to make the conversion factors and calculation clear.  Type all the input on one line; the input won't wrap around.  When you enter the calculation, you may get an error message, depending on what symbols you use for the various units.

3.  Repeat practice problem 2, but break the line between two parentheses using the return key.  What happens when you now enter the calculation?  Why?  How can you avoid this problem?

Comment

1.  It's ok to break up a command with a  <Return>  provided what's on a single line can't be executed by itself.

4.  Cube the complex number  4 + 5I.  Now take its reciprocal.  Repeat with the complex number  4 + 5.0I.   Why do the results differ?

Save Your Notebook

In order to conserve on memory space, remove all the output cells from this notebook by highlighting the output cell bracket and pressing  <Command>-x.  Then select  Save  to store your notebook under the name  myEx01.