LESSON 3
Mathematica  AS 
SYMBOLIC CALCULATOR

Author's notice

This tutorial was written by 

Fred Goodman
Department of Mathematics
University of Iowa
Iowa City, IA 52242
goodman@math.uiowa.edu

This material may be freely copied and used for non-commercial purposes.  Please retain this author's notice, and do not distribute any altered version of this material without the author's permission  (unless  the material is altered beyond recognition).

Assigning values to variables.

As in all general purpose computer languages, you can assign values to  variable.  Mathematica will remember the value until you explicitly  change it or Clear[  ]   it.

The following is an input cell; to do the indicated computation,  Click on the red bracket on the right and hit enter, or shift+return.
Do the same with the subsequent input cells (and observe the output).

Clear[a]
3 a + 5
Sin[a]

a =  .03
3 a + 5
Sin[a]
(a + b)^2

Defining your own functions.

To define your own function in Mathematica, you use the following pattern.  You must follow this pattern exactly.   Notice in particular the underline next to the x on the left side and the colon followed by the equal sign.

Evaluate the following cell now!

f[x_] := (x^5) + 16 (x^2) - 4 x + 3

When you evaluated the input cell above, nothing seems to happen; this is because the  :=   (colon followed by equals)  causes delayed evaluation. However Mathematica now knows the definition of the function f.

f[3]f[y]f[a + b]

Hey, what happened to a?   Mathematica remembered that a still has the value .03, although you may have forgotten.  If you no longer want a to have this value, you have to clear it.   After you clear a, Mathematica will again treat it as a symbol, or variable quantity.

Clear[a]f[a + b]

Warning:  You must follow the syntax exactly for defining a function. You must use the little underline after the variable on the left side, and you must use the delayed assignment operator :=.  If you don't, you can get some wierd and confusing results.

Try the following experiment:

Clear[f]f[x_] := Sin[x]^2 + 5f[3]f[3.]f[y]

(Notice that Mathematica left Sin[3] in symbolic form; we could tell it we wanted a numerical answer by writing 3. = 3.0 instead of just 3.)

Now let's do it wrong:

Clear[f]f[x] := Sin[x]^2 + 5f[3]f[3.]f[y]

Mathematica didn't learn the new function definition because we got the syntax wrong.

Simplifying algebraic expressions.

Most of what you know how to do from algebra, factoring, expanding, combining rational expressions, working with radicals and exponents can also be done in (or by) Mathematica.

Clear[f]f[x_] := -18360 x - 1895 (x^2) - 369 (x^3) + 15 (x^4) + (x^5)

f[x]

Factor[ ]  will factor expressions (when it can).

Factor[f[x]]

Together[  ]  will combine sums over a common denominator and afterwards cancel common factors.

F[x_] := -(((x^3) 2 x)/(((1 + (x^2)))^2)) + ((3 (x^3))/(1 + (x^2)))
F[x]

Together[F[x]]
Factor[Together[F[x]]]

Simplify[  ]  attempts to put an expression in "simplest" form.  Possibly it will not be the form you prefer, however.  In this example, it does just what we want.

Simplify[F[x]]

Other functions for changing the form of expressions are:
Apart[  ],  which is more or less the opposite of Together[  ],
Collect[  ], which collects like terms,
Expand[  ],  which multiplies out factors, and
Cancel[  ],  which cancels common factors.
You can get information on these using the ? operation.

?Cancel

For  working with trigonometric functions, you can use the simplification functions above, but you have to set the option  Trig-> True.

Together[Sec[x]^2 - Tan[x]]
Together[Sec[x]^2 - Tan[x], Trig -> True]

Wish I didn't know now what I didn't know then

Mathematica's memory for previous definitions can often lead to very confusing results.  In particular, if you close one notebook and then begin to work with another, Mathematica still remembers the definitions made while you were working with the old notebook, which probably have nothing at all to do with the new notebook.  Occasionally Mathematica seems not to forget old definitions even when you redefine things.  It's a good idea, which I don't always follow myself, whenever you define a new function or use a new variable, to Clear[  ]  it first.


When you get strange results which are not at all what you expect:

Clear[  ]  functions and variables before using or defining them.

Check that you used the correct syntax for defining functions.

If things get really wierd,  select "quit kernel" from the kernel menu, and then start over.   This will cause Mathematica to forget all that you have told it.

When all else fails,  save your notebook to disk, quit Mathematica, and start over.  This should hardly ever be necessary.