Basic Mathematica

Andy Long, Spring, 2016

Thanks to http://www2.math.ou.edu/~amiller/math/mathintro.htm
for getting me started.

Getting Started with Mathematica

This short primer consists primarily of examples intended to illustrate basic features of the mathematical software package Mathematica. For optimal use, it is suggested that you initiate a Mathematica session and attempt to carry out the examples that are discussed.

Getting Help

There is a very extensive and well-organized Help facility built into Mathematica.
You can always right-click on a command name to get help. That’s easiest.

Compiling and Editing

Once you have typed in a command, click on the cell (the brackets to the right of the screen define “cells”) and press
Shift-Enter 
which will send the command to Mathematica.  (Note: “Shift-Enter” means to simultaneously hold down the Shift and Enter keys.)

For example try it on this command, and see what happens:

Expand[(x+y)^2]

Right click on the word “Expand” above, to see the help for that function.

Hopefully you saw a bit of familiar algebra. Try it on this one, and see what happens:

Expand[(x+y)^50]

I’ll bet that you’re glad that you didn’t have to do that one by hand!

While Mathematica is compiling the input, you will see “evaluating” written at the bottom of the screen.  If “evaluating” remains there for a long time you have probably given Mathematica a problem that involves too many calculations. Sometimes you can interrupt the calculation by choosing “Abort Evaluation” from the “Evaluation” menu; and sometimes Mathematica never recovers....

Return (or Enter) starts a new line on your screen but DOES NOT invoke Mathematica.
Useful general rule: If  text is highlighted using the mouse it can be copied using 
Control-c 
and then pasted back with 
Control-v
(after repositioning the cursor where you want to insert the blocked text).

These “keyboard shortcuts” are generally much faster than using the mouse or menu commands.

To prepare a Mathematica session for printing, you will likely want to delete any cells that were incorrect or irrelevant to the problem at hand. You will notice that each cell has an associated bracket shown on the right edge of the screen. If you single click the mouse on one brackets and then type Control-x this will delete the corresponding cells or groups of cells.
Commands and functions in Mathematica always start with a capital letter. And arguments are always enclosed in square brackets “ [ ]”, not round parentheses. For example, try compiling the command

Sqrt[4]

and try

Cos[Pi]

Notice that Pi is also capitalized. 
Then, for comparison, compile the command

Sqrt(4)

Mathematica thinks that you want to multiply something called “Sqrt” by 4. It doesn’t understand that you mean that 4 is the arguement (or “input”) to Sqrt.

Parentheses (round brackets “ ( )”) should be used to make sure that operations are computed in the desired order.For example,compare the outcome of

1/2*2

with

1/(2*2)

The percent sign % can be used as a shorthand to denote the last result generated,and %% is the second-to-last result.

So if you type Sqrt[%] following the previous result of 1/(2*2), you should get 1/2:

Sqrt[%]

Elementary Functions

Either x*y or x y (with a blank space inserted between x and y) denotes multiplication of x by y.  If you write xy without a space between the x and y then Mathematica will attempt to interpret this as the name of a single variable (and it will NOT interpret it as multiplication).

(However, if in the above x is replaced by an actual number then Mathematica will interpret the juxtaposition as multiplication. For example, 5y means the same as 5*y, but x5 will again be interpreted as a variable name.)

4 x y

For exponents,write x^7 for x to the 7th power, and so on:

x^7

The common constants π and e are denoted by Pi and E respectively in Mathematica. Basic elementary functions are invoked by

Sin[x]
Cos[x]
Sinh[x]
Sec[x]
Tan[x]
ArcSin[x]
ArcTan[x]
Sqrt[x]
Log[x]
Exp[x]

The interpretations of these functions should be obvious. Note that Log[x] is used for the natural logarithm function ln(x).

Write

N[Sqrt[2]]

to get a numerical approximation to the square root of 2, or

N[Sqrt[2],30]

to get an approximation accurate to 30 decimal places.

Defining functions of your own

Note that you use square brackets to define your own functions, and that the argument is followed by an underscore: x_

Note also that, rather than “=” alone, we have a “:=”, and that the functions Sin and Log are capitalized.

f[x_]:=Sin[x]*Log[x]
f[1]
f[2]
N[%] (* give a numerical approximation to the previous result. *)

Fundamentals of Graphing

Try having Mathematica compile each of the following to see what happens:

Plot[f[x],{x,-Pi,Pi}] (* Here's a graph of the function we just defined. *)

Plot[Cos[x^2],{x,-Pi,Pi}]
(* This gives the graph of y=cos(x^2) on the interval. You can get Mathematica to enlarge the picture by first clicking on the graphic output and then dragging one of the little black rectangles while holding down the left mouse button. *)Plot[Cos[x^2],{x,-Pi,Pi},PlotRange->{0,1}]
Plot[Cos[x^2],{x,-Pi,Pi},AxesLabel->{"x-axis","y-axis"},Frame->True,PlotLabel->"My graph"]

Try getting help on the Plot command, by right-clicking on the word Plot above, and then selecting “get Help” from the menu.

More details on options for controlling the output of the “Plot” command can be found on the help page.

Algebra and Calculus

Try having Mathematica compile each of the following to see what happens. 

You’ll notice that I’ve added some comments, which you do using the “starred-parentheses” (* *). Whatever is between them in a command cell will not be interpreted as anything but a comment.

Expand[(x+y+z)^2]
Factor[x^3+2x^2-5x-6]
Simplify[Sin[x]^2+Cos[x]^2]

Those were some basic algebra commands; now let’s look at some of the calculus commands that you’ll be using throughout the semester:

Limit[5-(x-1)^2,x->1.5] (* This computes the limit as x approaches 1.5.*)
D[x^2,x] (* This computes the derivative with respect to x of x^2.*)
Integrate[x^5*Sin[x],x] (* this computes the exact integral, whereas the following numerically approximates the integrand.  *)
NIntegrate[E^(x^2),{x,0,3}] (* this is an approximation.... *)

Some fancier stuff:

D[Log[x]*Sin[x],{x,3}] (* This computes the third derivative with respect to x.*)
Solve[x^2+x-1==0,x] (* Note: == is a doubled equal sign,and is always used to represent Logical Equality. *)
Limit[Sin[x]/x,x->0] (* So Mathematica knows L'Hospital's Rule!*)