A Mathematica Crib Sheet (But It's OK, You'll Need One) : The Golden Rule: Try It!
A Mathematica Crib Sheet (But It's OK, You'll Need One) : The Golden Rule: Try It!
Think of this sheet as an emergency road-side manual, dedicated to getting you "up and
running."
Experiment! Try commands out to see what they do. Try commands we haven't talked about.
We promise: the computer won't catch fire, no matter how bad a mistake you make. (As long as
it doesn't involve matches...)
1. To end input you must hit "Enter" or "Shift-Return," not just "Return." Commands to
Mathematica can span multiple lines, so if you just hit "Return" Mathematica thinks you
haven't finished typing the command for that input cell, and waits for you to continue.
2. Mathematica functions and procedures use square brackets around their arguments, not
parentheses!
3. Mathematica is very sensitive to the case a word is typed in. All Mathematica functions
and procedures start with capital letters, but the rest of the letters must be lower-case,
unless they effectively start a new word – for example, FactorInteger.
4. Ending an expression with a semicolon tells Mathematica to suppress that output. If
you're not getting any answers at all, check for semicolons.
1. If you want to know something about a Mathematica function or procedure, just type a
question mark followed by the name of the procedure. Example:
?FactorInteger
To get still more help, type two question marks in front of the name.
2. If you can't remember the full name of a Mathematica function or procedure, but can
remember a few letters at the start, there is a "fill-in" capacity: after typing a few letters,
type command-K (simultaneously press down the "open apple" key, located next to the
space bar, with the letter "K"). Mathematica will either finish typing the name for you – if
there is only one command which starts with those letters – or will present you with a
"pop-up menu," a list of possible matching commnads. By clicking on one of these you
select the complete command.
Assigning Values
Values you assign remain assigned until you change them!! If you set x = 5 and then type
Solve[x^2 - 2x + 1 == 0, x]
(asking Mathematica to solve the quadratic equation for x), Mathematica returns an empty
solution set! You have asked it to solve the equation 5^2 + 2*5 + 1 == 0, and Mathematica
realizes this has no solution.
Moral: to be safe, before using a variable such as x you should always type Clear[x].
Replacements
expr /. x->value Replace every occurrence of x in the expression expr with value. (Read:
"expr, given that x is changed to value.")
expr /. {x- Perform several replacements simultaneously.
>xval,y->yval}
For example, suppose we want to check that Mathematica got the correct roots of the quadratic
equation x^2 - 7x - 3 == 0. Remembering the fiasco where we had set x = 5, we begin with
Clear[x]; Solve[x^2-7x-3==0,x]
Mathematica replies
Out[5]=
7 - Sqrt[61] 7 + Sqrt[61]
{{x -> ------------}, {x -> ------------}}
2 2
The result is a list of transformation rules. Now we type
x^2-7x-3/.%5[[1]]
that is, we ask for the value of x^2 - 7x - 3, given that the first transformation rule in %5 is
applied. To understand the [[1]], read the next section on Lists and Iterators.
{a,b,c} A list (in this case, of the three objects called a, b and c).
{} A list (of no objects!).
a[[n]] The n-th item in the list a. Note the double brackets!!
Sum[a[[n]],{n,3,7}] The sum a[[3]] + a[[4]] + a[[5]] + a[[6]]. Here {n,3,7} is
what is called an iterator, that is, a statement which says to repeat
something; in this case, it means the sum of a[[n]] for n = 3 to 7,
inclusive.
Sum[a[[n]],{n,1,7,2}] The sum a[[1]] + a[[3]] + a[[5]] + a[[7]]. This form of the
iterator says to let n run from 3 to 7, inclusive, incrementing n by 2 at
each step. (That's an easy way to get the sum of all the odd terms, or
all the even terms.)
Plot[f[x],{x,a,b}] Plot the graph of the expression f[x], from x = a to x = b, inclusive.
{x,a,b} is still called an iterator, because x takes on values between a
and b, although of course not all real values between a and b.
Transforming Algebraic Expressions
Defining a Function
f[x_] Define a new function f. Note the underscore on the x on the left side of the
:= x^3- statement! It must be there; it tells Mathematica to treat x as a pattern; thereafter,
x
when you type something like f[a+b], Mathematica will then immediately consider
that to be the same as (a+b)^3-(a+b).
Solve[x^2+x==1,x] Solve the given equation x^2 + x = 1 for x. Note that we must use
the double-equals sign. Also, only an exact solution will satisfy
Mathematica. You can also solve systems of equations, e.g.
Solve[{x+y==1,x^2+y^2==1/2},{x,y}] solves those two
equations for x and y. (Can you do this by hand?)
Roots[f[x]==0,x] Finds the roots of a polynomial equation. Only exact solutions will
be reported.
NRoots[f[x]==0,x] Finds the roots of a polynomial equation, but numerically, i.e.
approximately.
FindRoot[f[x]==0,{x,a}] Find roots of f[x] = 0 numerically, starting at x = a. Works for
non-polynomials, but may fail to find the roots, or all of the roots,
even when they exist.