Mathematica Notes 1.0: The Basic Environment

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Mathematica Notes 1.

The Basic Environment


• The Notebook: The document you open and do your work in is known as
a notebook. Saved notebooks end with a ‘.nb’ extension.

• Cells: The basic organizational unit within a notebook is known as a cell.


The calculations that occur within a cell are grouped together by cell
brackets drawn on the right margin of the window.

Basic Operations

addition +
subtraction −
multiplication *
division /
exponentiation ˆ
factorial !
assignment =
equivalence ==

• A single space between quantities is also interpreted as multiplication -


thus ω t is the product “ωt”.

• IMPORTANT: To you and I, ab is the product ab. To Mathematica, it


is a placeholder for a quantity called “ab”. Make sure you place a space
between a and b if they are separate quantities to be multiplied together.

Test Spin
• Place the cursor over your open notebook and type the following 45/7
<enter>. Apart from the appearance of a cell-bracket on the right, noth-
ing happens.

• Place the cursor over “45/7” and type <shift enter>. Your input is
tagged as such, and the output, also duly-tagged, appears below. The
‘execute’ command in Mathematica is <shift enter>.

• The form of the output might be a little less than exciting. Try another
one - below the output line, type 45/6 and execute (note the incremented
tags). Mathematica is evaluating the input - it will generally try to give
you the most accurate representation of the answer.

• Type N[45/7] and execute it. N[...] returns the value of the argument
as an approximate numerical result.

1
• Type 45/7 //N and execute it. Many commands have a postfix form that
allow them to be used almost as an afterthought.

Built-in Values
• For fun, execute the following two commands: a) N[pi], b) N[Pi]. It may
be all Greek to you and I, but internal values (constants and function
names) within Mathematica all begin with upper-case letters.

• You shouldn’t be too surprised, then, that E is Euler’s number, I is −1,
π
Degree is 180 , and Infinity is ∞.

• Most of the fundamental constants are available in their more familiar


form in one of the specialty palettes.

Output Format
• NumberForm[x, n] prints x to n significant figures.

• ScientificForm[x, n] prints x in scientific notation to n significant fig-


ures. Omitting n will result in default precision.

• $MachinePrecision returns the number of decimal digits of precision


used for machine-precision numbers.

• Precision[x] returns the number of digits of precision in the number x.


(Fun: Evaluate Precision[Sqrt[2]]).

Holding things together


Parentheses are for algebraic grouping (a + b)
Square brackets hold the arguments for functions f [x]
Squiggly brackets contain lists {ax , ay , az }
Double brackets index list elements r[[i]]
Comments within a calculation cell ( * increment i * )

Common Functions
• Sin[x], Cos[X], Tan[x], Csc[x], Sec[x], Cot[x]

• ArcSin[x], ArcCos[X], ArcTan[x], ArcCsc[x], ArcSec[x], ArcCot[x]

• Sinh[x], Cosh[X], Tanh[x], Csch[x], Sech[x], Coth[x]

• ArcSinh[x], ArcCosh[X], ArcTanh[x], ArcCsch[x], ArcSech[x], ArcCoth[x]

• Sqrt[x], Exp[x], Log[x], Log[base,x], Abs[x], Round[x]

2
Variables
• Variables must start with a letter (not a number) and may consist of any
number of alphanumeric characters, Greek letters, even subscripts (use the
basic-input palette). Though there are certainly reasonable exceptions,
user-defined variables should start with a lower-case character (to avoid
conflicts with internally-defined functions). Note that I, E, N , D, C, and
O all have special meanings within Mathematica and should not be used
as variable names.

• Values may be assigned to variables using immediate assignment (=) or


delayed assignment (:=). The distinction is rather subtle, we’ll come back
to this.

• You should get in the habit of clearing assigned values when they are no
longer needed (to avoid confusing future calculations). To clear the value
of a, you may execute Clear[a] or a = . .

• Variables do not need to be assigned values! One of Mathematica’s great


strengths is its ability to do symbolic calculation/manipulation. (Fun:
Execute Expand[(x + y)6 ] )

Immediate Assignment (=)


• Immediate assignment evaluates the right-hand side of the expression and
assigns the value to the variable on the left.

• Enter a = 5, b = 7 and d = a + b + c and execute. Immediate calculations


are done and d is assigned the value 12 + c.

• Enter c = 1 and d and execute. Note that the last line is not an as-
signment. It is asking you to evaluate the expression stored in d. Not
surprisingly, the result is 13.

• Enter c = 5 and d and execute. The result should be 17.

• Now, Enter Clear[b] and d and execute. You should still get 17! Why?
(Hint: What is stored in d?)
• It would probably be a good idea to Clear[a,b,c,d] now.

Delayed Assignment (:=)


• Suppose you want d to pick up the most current values of a, b and c with
each invocation. What you really want to do, then, is to tell the computer
the right-hand side of the assignment to d needs to be re-evaluated with
each use. In that case, you would use a delayed-assignment (:=).

3
• Enter a = 5, b = 7 and d := a + b + c and execute. Immediate calculations
are done for a and b (totally appropriate). No calculation is performed for
d (since delayed assignment tells the computer to perform the calculation
with each invocation).

• Enter d and execute. You should see 12 + c

• Enter Clear[b] followed by d and execute. You should see 5 + b + c.

• It would probably be a good idea to Clear[a,b,c,d] now.

Equivalence (==)
• Equivalence is used to state that one expression is equal to another (eg.
x2 + y 2 == 25). It does NOT make any assignments.

Multi-Element Assignments
• The list r = {x,y,z} creates a 3-vector ~r with components equal to the
values stored in x, y and z, respectively.

• The nested list m = {{a,b,c}, {d,e,f}, {g,h,i}} creates a 3 × 3 matrix m


with the first row given by the values stored in a, b, c, the second row by
the values stored in d, e, f and the third row by the values stored in g, h, i.

• r[[2]] picks out the second element in r (in this case, y).

• s = r[[{2, 1}]] (Note the list inside the double-bracket.) This will set s to
{y,x}.

• t = m[[2, 1]] (Note the absence of a list inside the double-bracket.) This
sets t to the first element in the second sublist of m (in this case, m2,1 , or
d)

Common Vector Operations


cr multiplication of ~r by scalar c
r.s scalar product of ~r and ~s
Cross[r,s] vector product ~r × ~s

4
Common Matrix Operations
cm multiplication of matrix m by scalar c
m.n multiplication of matrix m by matrix n
Inverse[m] inverse of matrix m
Det[m] determinant of matrix m
Transpose[m] transpose of matrix m
Tr[m] trace of matrix m
Eigenvalues[m] eigenvalues of matrix m
Eigenvectors[m] eigenvectors of matrix m
IdentityMatrix[n] n × n identity matrix
DiagonalMatrix[list] square matrix with the elements of list along the diagonal
MatrixForm[list] displays list in matrix form

Serendipity
• Most of these will be addressed in greater detail later (but are useful to
have now). Use the online help system to expand on these and see what
else you can do!

% holds the value of the previous result


%% holds the value of the second previous result
%n Rholds the value of the result in Out[n]
Integrate[f ,x] R xfhi(x)dx
Integrate[f ,{x, xlo , xhi }] f (x)dx
Rxxlohi Ry
Integrate[f ,{x, xlo , xhi }, {y, ylo , yhi }] xlo
dx ylohi dyf (x, y)
D[f ,x] ∂f /∂x
D[f ,{x,n}] ∂ n f /∂xn
D[f ,x,y] ∂ 2 f /∂x∂y
D[f ,{x,n}] the n’th partial derivative
Dt[f ] returns the total differential, df
Dt[f ,x] df /dx
Expand[f ] Expand products and positive integer powers
Factor[f ] Factor the function f
Simplify[f ] Simplify f

Help
• ?cmdname should give you help on the particular command “cmdname”.
??cmdname may give you even more. (Execute ?N to learn a little more
about N[. . . ]).

• The ‘Find In Help Browser’ (under the ‘Help’ menu) is also a great re-
source.

You might also like