Introduction to Mathematica

This is a tutorial to introduce you to Mathematica.  It will only scratch the surface of this powerful software, but it should get you going.  

The file you are reading is called a Mathematica "notebook".  Whenever you work with Mathematica, you will need to open or create a notebook, which contains the expressions you ask Mathematica to evaluate (inputs) as well as the results (outputs).  It may also contain text to annotate what you are doing.

You probably have noticed the blue brackets at the right of the Mathematica window.  These blue brackets define cells.  There are many different kinds of cells in Mathematica.  This type of cell is called a "text cell" because (surprise!) it contains text (rather than mathematical operations).  The following cell (with the bold face type) is called an Input cell and contains a command for Mathematica to execute.  Put the cursor anywhere within the cell or click on the cell bracket to the right.  (It should then be highlighted.) Then hit the Shift+Enter keys (that is, hold down the Shift key and hit Enter).  We will call this procedure "activating a cell".

5+11

Notice that Mathematica added an In[1] to the cell and an Out[1] to the cell containing the result of the calculation.  You should also notice that Mathematica added an outer cell bracket to the In and Out cells showing that they are grouped together.

NOTE:  If you simply hit Enter (without the Shift key), Mathematica adds a new line to the cell.  The cell commands are not activated.

Double-click on the outer cell bracket.  You will see that the two cells collapse to one and that the lower part of the cell bracket changes form, letting you know that something is hidden away.  By double-clicking on the  outer bracket again, you can re-expand the cells. Collapsing grouped cells is a good way to make a long notebook look more compact.

NOTE:  You can remove a cell by selecting its bracket by first clicking on it.  Then hit the Del key.  Try that with the Out cell generated above.

NOTE:  Whenever you want to quit your session, simply click on File on the menu bar and then on Exit.  Respond Don't Save to any request for saving changes.  If you want to save the changes you have made, please save them in a file with a different name.

Mathematica as a calculator

You can use Mathematica as a simple calculator.  Let's examine several kinds of operations.  As we do so, we'll learn something about the syntax of Mathematica operations.

Activate (Shift+Enter) each of the following cells to see what happens.  (* indicates a comment that Mathematica ignores in evaluating the expression*)

5-11 (*subtraction*)

5*11(*multiplication*)

5 11

The previous cell illustrates multiplication implied by a space separating two quantities--It works, but it is not recommended! It is too easy to misread and too easy to forget the space.

5/11(*division*)

Perhaps that last result surprised you.  In many cases Mathematica will give you an "exact" result that may not be quite the form you expected.  The next cell has the same division operation, but the "//N" part is a so-called postfix operation that tells Mathematica to give the result in numerical form.  Try it out.

5/11 //N (*division with the result in numerical form*)

5^11 (*exponentiation*)

5-11/10

Did you expect to get what Mathematica gave you for the result? As in most computer languages, Mathematica has a heirarchy of operations: division is done before subtraction for example.  Rather than trying to remember the heirarchy, it is better to use parentheses liberally to group the terms  to implement the operations the way you want them implemented.  Parentheses also make the expression easier to read.

(5-11)/10 (*subtract first, then divide*)
5-(11/10) (*divide first, then subtract*)

Note that you can create a new input cell simply by clicking in the space between two cells.  Move your cursor to the space after this cell.  You will see it turn horizontal.  When that happens, click and a long horizontal line will appear.  You can then start typing in an expression.   Do this to perform some simple arithmetic calculation.

Symbols and variables

Of course in most calculations, we use symbols rather than numbers to represent various quantities.  The cells below show you how Mathematica lets you assign numerical values to various symbols.  The scheme is the same as used in most computer languages.

a=7

Note that Mathematica gives an Out statement that tells you it has the number 7 associated with the symbol "a".

a=6;

Here we have changed a to 6, but Mathematica did not seem to respond.  That's because we used a semicolon (;) at the end of the statement.  The semicolon is used frequently in Mathematica to suppress unneeded Out statements.

To check that indeed Mathematica has changed the value associated with the symbol a, we can use a simple command line with just "a" in it:

a

Now let's define several symbols and then use them for some operations.  Activate the following cells.

b=3;
c=2;
d=4.4;

a+b*c

(a+b)*c

(a+b)^-d

Trigonometric Functions

Mathematica "knows" trigonometric (and many other) functions. These functions can be used in a variety of ways.  This section will introduce you to some of the ways of using Mathematica's built-in functions.

Sin[1]

Sin[1] //N

Note that the first cell didn't appear to do anything.  When the argument is an integer, Mathematica gives us the "exact" (but not directly useful) result Sin[1].  The second cell (with the //N) gives us the numerical value.

If you give the argument as a decimal number, Mathematica gives you the numerical value.

Sin[1.]

Some points to note:
    1.  All of Mathematica's built-in functions begin with an upper-case letter.
    2.  The argument of the function is contained within square brackets [  ], not parentheses.  Parentheses are reserved for grouping symbols and numbers.
    3.  The arguments of the trigonometric functions are in radians.   Mathematica has a simple way of doing a conversion from degrees to radians.  See the cells below.

Sin[45 Degree]

Sin[45 Degree] //N

Sin[a+b]

Sin[a+b] //N

Note that Mathematica has remembered the values associated with "a" and "b" from earlier in our session.

Let's now use some trignometric functions in calculations.

Sin[45 Degree]^2

Note that the exponent is applied to the Sin function, not to the argument.

Sin[(45 Degree)^2] //N (*square the argument*)

Sin[Pi] (*This uses one of the Mathematica symbols for π*)

Tan[45 Degree]

ArcTan[1]

Exercise:  In the cell below type Tan[90 Degree] and then activate the cell to see what happens.  Try some other trigonometric functions, like Sin and Cos for simple angles to see if you get answers you expect.

Functions

Mathematica allows you to define your own functions. As noted, Mathematica's built-in functions all have names that begin with upper-case letters.  It is good practice in defining your own functions to use names that begin with lower-case letters for your "custom" functions.  

Let's begin by defining a simple function and seeing how to use it within Mathematica.  The following cell sets up a function in Mathematica in a way that might seem reasonable, but one that leads to trouble.

y[t]=5.0*t^2

Let's now try to evaluate y at time t = 2.0 seconds.  Activate the following cell.

y[2.]

You probably found that the result was not what you expected, that is, Mathematica did not evaluate the function for t  = 2.  The difficulty is that the form we used for setting up the function was not what Mathematica wants for a general function, one that can be evaluated for whatever values of the argument we want.  The correct function definition requires having the function's independent variable labeled with an underscore _  (obtained by holding down the Shift key and hitting the - key (on the top row of the keyboard next to 0)) on the left-hand-side of the definition.  Once the function has been defined, you can use y[t] in other expressions.  That is, you only need to underscore in the definition.

The next cell illustrates the desired format.  Activate that cell to see what happens. (The Clear statement removes our previous defintion of y.)

Clear[y]
y[t_]=5.0*t^2

Now let's find the y at t = 2 sec.

y[2.]

Now we get the expected result.  

Try the following cell and confirm that the result makes sense.

y[t]^2

Exercise:  Define a function - call it s - that is a sine function with period 1.  Make it a function of the variable t.

Plotting functions

Now, let's plot the function by activating the following cell.

Plot[y[t],{t,0,3}]

Mathematica automatically sets up the graph with appropriate numerical values on the abscissa and ordinate axes.  Note that within the Plot command,  the function argument t appears without the  underscore _  because we are using the function, not defining it.  

The arguments inside the curly brackets within the Plot function specify the range of t values to be used in the plot.

Exercise:  In the following cell type and activate a command that will plot the function y[t] between t = -3 and t = +5.

Exercise:  Plot your function s over a range of t appropriate to be able to determine its period.  Does it have a period of 1, as you expect?  If not, go back and revise your definition of s and replot.

Playing functions as sound

Mathematica also allows you to listen to functions - a feature that is useful if the function is periodic in the audible range.

Let's define a simple periodic function.  Activate the following cell.

func1[t_]=Cos[2*Pi*100*t]

What's the period of this function?  

Exercise:  Make a plot of the function over a range of a few periods.

Now let's play this function over a 3-second time interval.  Activate the following cell.  When the result appears, press the little "play" button in the lower left corner.  If your computer doesn't have speakers, you may need to plug in headphones/earbuds in order to hear the sound.

Play[func1[t],{t,0,3}]

You can also play in stereo.  Define a new function - call it func2 - that has half the period (twice the frequency) as func1.

We'll make func1 play in the left ear and func2 in the right ear.  Here's how:

Play[{func1[t],func2[t]},{t,0,3}]

Note that we put the two functions, separated by a comma, within a pair of curly brackets.  Try changing the frequencies of the two tones to get other consonant or dissonant combinations.

Further Information - Help

You are encouraged to try out the features you have learned about by typing various expressions and evaluating them.  Using Mathematica is an experimental science.  The worst that can happen is that Mathematica freezes up, which it may do from time to time.  If that happens, use Ctrl+Alt+Del to stop Mathematica and to reboot your computer.
	
Mathematica's Help file contains lots and lots of information about Mathematica commands and how to use them.  It is also a useful source of examples of using Mathematica commands, which you can Copy and Paste into your own Notebooks.
	
If you looking at a notebook that someone else has given you and you want to know how a particular function works, simply double click on the function to highlight it, then press F1.  Mathematica will take you to the help file for that function.  Try it; double click on Exp in the following cell and press F1:

Exp[-1]