Below we give a list of Mathematica commands that you may encounter this year. First recall that there are to basic ways to define a function of x. The quick way is to execute, for example:

y = 3 x + 1

The lengthier way, but one that allows for greater flexibility, is:

f[x_] := x^3 + x^2 - 5x - 5

Note both the x _ symbol on the left, and the := defining symbol. Defining f this way allows you to easily evaluate, say, f[2] , f[5 t + 3], etc. In contrast, to evaluate the first definition of y at x = 2 you must type:

y /. x -> 2

Remember that Mathematica tends to do exact arithmetic; for example, carrying the fraction 2358463 / 78932749. This is time-consuming for the computer and one usually prefers the decimal approximation. One way to get the decimal form is to use the N command; for example.

N[2358463/78932749]

Or, you can simply put a decimal point after one of the integers. Try it. If you really need lots of decimal places of p you can type and execute, for example:

goodPi = N[Pi, 50]

Getting Help during a Mathematica session

Suppose that you have used a command before, but have forgotten its exact format. You can simply type, for example:

?Plot

Or if you want more detail, type ??Plot. Try it.

A Warning about variable names

Should you have another Mathematica notebook open (say Project1.ma), the two notebooks will share the same variables. For example, suppose in another notebook you had defined a variable y differently that we have above. Then you come to this notebook and execute the above y definition. When you return to the other notebook y would be changed to the above.

However, if you simply call upon another notebook to refresh your memory on a command, and do no execution, there is no problem.

A Partial List of Mathematica Commands.

The following commands are in alphabetical order. If the command is on a separate line, as in the example for D below, you can execute it if you wish; ASSUMING the above y and f[x] have been executed previously.

Abs---take the absolute value of its argument.

Abs[f[2]]

Clear---is used to "undefine" variables or functions. This is useful when you are changing the definition of f, or things have gotten confused. For example, Clear[f, y] clears above functions.

D---will take the derivative of specified function with respect to specified variable. For example,

D[f[x], x]

Do---allows you to set up a "loop" in order to perform the same (or similar) commands numerous times. The following does six Newton iterations on function t^2 - 5, to approximate Sqrt[5]:

t = 3.0
Do[t = t - (t^2 - 5.0) / (2.0 t), {k, 1, 6}]
t

The variable k is a "counter" that here takes on values 1,2,3,4,5,6.

Expand---expands out in powers of x (or some other expression). Consider these two examples:

Expand[y f[x]]

Expand[Cos[x] (3 - Cos[x]^2)]

Integrate---the first form of Integrate attempts to find an antiderivative of the first argument, with respect to the second.

Integrate[y, x]

The second form computes a definite integral.

Integrate[y, {x, 0, 3}]

(Should this fail, see NIntegrate below).

Factor---will attempt to factor the argument. In polynomials, it only looks for integer factors. For example,

Factor[f[x]]

Limit---will attempt to take the specified limit.

Limit[y, x -> 3]

The following takes the limit from the right (use +1 to get the limit from the left):

Limit[f[x], x -> 0, Direction -> -1]

N---converts the argument to decimal form and shows six figures. More figures can be shown if requested.

N[Pi]

N[Pi/2, 16]

NIntegrate---attempts to do a numerical integration of the first argument. Typically, the result is good to 16 figures. For example,

NIntegrate[y, {x, 0, 3}]

(Also see Integrate above).

Plot---plots one or more functions over the specified domain interval. There are many options (try ??Plot), the most common is perhaps PlotRange. Try this with, and without, the option:

Plot[{y, f[x]}, {x,-4,4}, PlotRange -> All]

To get a label on a plot we can do e.g.:

Plot[{y, f[x]}, {x,-4,4}, PlotRange -> All,
PlotLabel -> "J. Jacobs Project No. 6"]

A carriage return (not "Enter") was used to go to a new line, thereby keeping the command from running off of the screen to the right.
Sign---produces the sign (+1 or -1) of the argument.

Sign[-Pi/2]

This function is useful in producing "step functions"; for example,

g[x_] := Sign[x - 1] - Sign[x - 3]

Simplify---attempts to clean up the expression by looking for common factors, etc.

Simplify[(f[x]- y) / (x + 3)]

Solve---attempts to solve the specified equation (or equations) for the specified variables. Solve is effective primarily for polynomial equations. The following finds x-values at which f[x] and y are equal:

ourRules = Solve[y == f[x], x]

The result of this is a set of "rules", saved in a variable we have called "ourRules", which can be used as follows to evaluate y at these x values:

y /. ourRules

Next we solve a pair of equations for unknowns s and t:

Solve[{3 s + t == 7, 2 s - 3 t == 1}, {s,t}]

Sum---this command allows you to conveniently perform a sum, as a "counter" (k below) takes on a specified range of values. For example, to compute the sum of squares of the first six integers, execute:

sumsq = Sum[k^2, {k, 1, 6}]

Table and TableForm---Table is used, for example, to generate a list of order pairs; and TableForm puts them in a more readable form.

Table[{n, n^2}, {n, 1, 5}] // TableForm

Note that the ordered pairs {n, n^2} are grouped using braces and not the standard parentheses.

Together---puts terms in a sum over a common denominator, and does some simplifying. For example,

(1 + x) y + f[x] / (1 - x)

Together[%]


http://math.colgate.edu/mathlab/commands.html
Revised: March 1, 1996.
Questions to: valente@colgate.edu
Copyright 1996 © Colgate University. All rights reserved.