Introduction to Mathematica for MathILy

Mathematica is a computer algebra system (CAS) that is very powerful computationally, graphically, and for doing symbolic math.  It’s modeled on LISP and functional programming.

First, execute the below command by clicking on it (to move the cursor to that line) and then press SHIFT and ENTER.

2+3

Helpfully, Mathematica does ordinary arithmetic.  You can change the 2+3 above to a different expression and execute it (by doing SHIFT+ENTER) to get a new answer (try it!).  

Next you’ll make a new cell: mouse just below this text until the cursor turns horizontal, and then click.  The result should be a blank evaluation cell.  Type something like 2^3*3 and evaluate it to see how this works.  (Notice that Mathematica understands the usual order of operations.)

(* There are a couple of ways to write notes to yourself or other readers of your notebook.  *)
(* The one you're reading now is a note inside an evaluation cell.  Anything surrounded by open parentheses-asterisk ... asterisk close parentheses tells Mathematica that you're making a comment rather than issuing a command.
The other one appears at the top of the page (and elsewhere),outside of evaluation cells.  To create a text cell, click on the plus + sign that you see when making a new line and choose "Text" *)

Evaluate the following two cells and notice the difference between the inputs and the outputs:

284/16

284/16.

In the first case, Mathematica gives an exact answer, and in the second case it gives a decimal approximation.  Why?  When you give Mathematica exact numbers, it does symbolic calculations.  When the number “16” is written as “16.”, that’s a cue to Mathematica that you want an approximate number, so the calculation should be done numerically instead of symbolically.

Make a new cell or two and evaluate Log[3] exactly and also get a decimal approximation for log(3).  (By the way, Log[3] is the way Mathematica denotes ln(3).  To get a base-10 logarithm of 3, you’d write Log[10, 3].)  ALSO NOTE:  Mathematica is case sensitive!  You must capitalize all commands, including  Sin[x] and Log[x].  Why not try to get a decimal approximation for (Sqrt[2])?  (You can use the command Sqrt[2] or get the square-root symbol from the Basic Math Assistant Palette from the Palette menu.)

There are two other ways you can get decimal approximations (try them):

N[Pi]

Pi//N

The “N[ ]” is the standard “make the answer numeric” command.  The “//N” version is a short-cut way to tell Mathematica to wrap the previous stuff with the N command.  It comes in handy.

Those are lovely (and notice that Mathematica knows the value of π), but what if you want more decimal places of accuracy?

N[Pi,50]

On the other hand, sometimes decimal approximations can get you in trouble.  Check these two evaluations out:

Sin[N[Pi,40]]

Sin[Pi]

A couple of other things Mathematica can do for you:

FactorInteger[2014]

Factor[x^2-y^2]

Notice that Mathematica treats variables very specifically.   For example, xy is viewed differently than x y, as you will see if you evaluate the cells below:

xy

x y

Subtle, isn’t it?

If you assign variables integer values, you don’t have to declare them as variables first:

m=3
n=4
m*n

Notice how I put multiple commands in that evaluation cell.  Mathematica does them one-at-a-time and gives the output for each.  If I wanted to supress some of the outputs, put a semicolon at the end of the line:

m=3;
n=4;
m*n

Heck, let’s do some other math.  Examine the following commands and figure out what they’re doing; then issue some similar commands of your own.

Plot[x^2+1,{x,-1,1}]

Range[20]

Range[2,11]

T[x_,y_,z_,w_]:={x+2y, x-3z+w,2y+3z+4w}

T[0,1,2,0]

Riffle[{1,2,3,4},{5,6,7,8}]

Plot3D[Sin[x]*Cos[y],{x,-Pi,Pi},{y,-Pi,Pi}]

Manipulate[Plot[Sin[a/x],{x,-3,3}],{a,-4,4}]

To learn more:
Go to the Help menu and choose “Documentation Center.”  This will open a window where you can find tutorials for Mathematica OR you can just enter a command or topic into the search bar and it’ll give you the manual page for the command or give you a list of options for the topic.  The manual pages have LOTS of examples.