Introduction to Mathematica

16.21 Spring 2004 
February 8, 2005

Daniel Jamous
Academic Computing 
Information Services & Technology

jamous@mit.edu
x2-1383

Note: to open the cells below, double-click on the "arrow" icons. To close the cells, double-click again.

First Steps in Mathematica

When launching Mathematica, you'll see on the left a blank "white" window called a "notebook." On the right, you'll see another window called the "palette" window. To do simple calculations, simply click on the notebook window and type what you want to do.

first calculation with Mathematica

2 + 2

To evaluate your expression, press "Shift+Enter" and not just "Enter". Let's see what happens if you just press "Enter":

2 + 2

Nothing happens. The cursor goes to the next line but the expression is not evaluated.

Note: your first calculation will take longer than subsequent calculations because the Mathematica kernel has to start up (Mathematica is made of two parts: a kernel and a front end. The kernel is the part that does the calculations. The front end is the part that handles notebooks and interaction with the user).

other numerical examples

3^50

34*765

1/0.786

Pi

Cos[Pi]

Note: Mathematica built-in functions and symbols start with a capital letter. Function arguments are enclosed in brackets [ ]. So if we try to evaluate this:

cos[Pi]

We get an error message.

Log[1]

Exp[1]

This is, in Mathematica, the symbol for e the exponential constant. One can also use the letter E.

E

To evaluate the numerical value of a constant, one can use the function N.

N[E]

N[Pi]

Mathematica also handles complex numbers.

Sqrt[-1]

This is the symbol for the imaginary unit.

Re[Sqrt[-1]]

Im[Sqrt[-1]]

Im[3 - 5  I]

Im[3 - 2  I]

However, Mathematica is much more powerful than just a calculator. Let's try some examples from the input palette (called the "Basic Input" palette) to the left.

Using the input palette

Mathematica can be used to evaluate algebraic expressions. Example:

∫(1/x) \[DifferentialD]x

(∫_a^b) 2  x + 3 \[DifferentialD]x

(∫_a^b)(2  x + 3) \[DifferentialD]x

Note: use the ⇥ key to move from one placeholder to the next.

Note: as the example above shows, you need to use parentheses.

Other examples:

(∂_x) (x^2)

(∂_x) x - 3  (x^2)

(∂_x) (x - 3  (x^2))

(∑_(i = 1)^n) i

(∏_(i = 1)^n) i

Use of other palettes

Now let's look at other things that Mathematica can do. A good way to do this is to see what "palettes" are available and try them (in File->Palettes). We'll leave the OpenAuthor Tools, CreateSlideShow, International Characters, and Notebook Launcher palettes for now. Also we already saw the basic input palette (the one appearing by default when launching Mathematica).

Algebraic Manipulation

First, let's try the function "Expand". To see what it does, we can either open the "Help Browser" and type in "Expand" in the Go To box or enter Expand from the palette to the notebook, highlight Expand and press the F1 key:

Expand[(((a + b))^5)]

You can do all sort of symbolic algebraic manipulations with this palette. Try some of the other functions.

Basic Calculations

This palette covers most of what Mathematica can do. Look at examples in each of the subcategories. This will allow us to introduce more Mathematica built-in functions

Arithmetic and Numbers

We already saw many of the basic operations. Note the function N[ ] that will be used extensively in class:

N[E]

N[π, 10]

The second argument  specifies the number of digits after the decimal point.

N[π, 50]

Algebra

Many interesting things here.

Solving Equations

\(\(\(Solve[a\ x\^2 + b\ x\  + c == 0, \ x]\)\(
\)
    \)\)

{{x -> ((-b - (Sqrt[(b^2) - 4 a c]))/(2 a))}, {x -> ((-b + (Sqrt[(b^2) - 4 a c]))/(2 a))}}

Solve[(ax^2) + bx  + c == 0,  x]

No answer in the second case because there are no variables x in the equation (the spaces have been shrunk).

Note: you can also use Solve to solve a system of equations:

Solve[{4 x + 5 y == 
            3, -2 x  +  6 y == -2}, {x, y}]

Note: expressions wrapped with curly brackets { } are called "lists." We'll see other examples below.

Polynomial Manipulation

We already saw these functions in the "Agebraic Manipulation" palette. Another example:

Together[1/x + 1/(1 - x)]

Simplification

Complex Numbers

(((1 + 3  I))^2)

Re[%]

Note: % refers to the last output or calculation.

Sqrt[-1]

Conjugate[-8 + 6 I]

Lists and Matrices

Creating Lists and Matrices

(({{1, 0}, {0, 1}}))

A matrix is represented as a "list" in Mathematica. You can nest lists with each other.

(({{1}, {0}}))

(({{1, 0}}))

See the difference between a row or a column vector.

You can use the function "Table" to build up vectors, matrices, and tensors.

Table[i, 5]

Note that the second argument has to be entered as a list.

Table[i, {5}]

This creates a 1x5 row vector of i.

Table[i, {i, 5}]

Table[i, {i, 11, 15}]

Table[i, {i, 1, 20, 2}]

Note the general syntax of a list {i, imin, imax, di} where i goes from imin to imax with increments di. By default imin and di are 1.  Table is handy to create matrices.

Table[KroneckerDelta[i, j], {i, 2}, {j, 2}]

This is the 2x2 identity matrix.

Matrix Operations

(({{1, 2}, {3, 4}})).(({{-1, 2}, {-4, 5}}))

(({{1, 2}, {3, 4}})).(({{-1, 8}, {1, 2}, {4, 6}}))

Transpose[{{1, 0}, {2, 3}}]

Tr[%]

Inverse[{{1, 2}, {0, 3}}]

Trigonometric and Exponential Functions

Standard Mathematical functions here.

Trigonometric

Exponential and Logarithmic

Hyperbolic

Calculus

Common Operations (integrals, derivarives, sums, products, limits, series)

We already saw some of these functions before when trying the Basic Input palette.

Series[Sin[x], {x, 0, 7}]

Taylor series of Sin[x] about 0 to order 7.

Differential equations

DSolve[y'[x] == a x, y, x]

Symbolic Transforms

Numeric Root Finding

Other functions (Integer and Special Functions)

Graphics

Plot[Sin[x], {x, -2 Pi, 2  Pi}]

Plot[{Sin[x], Cos[x]}, {x, -2 Pi, 2  Pi}]

Plot3D[Sin[x] Cos[y], {x, -5, 5}, {y, -5, 5}, 
      PlotPoints -> 50]

Note: you need the package below (RealTime3D) to interact with the 3D plot.

<< RealTime3D`

Basic Typesetting

Note the "keystroke" alias for the characters (visible at the bottom of the palette when putting your pointer over a character).

Complete Characters

More comprehensive version than above.

Functions and transformation rules

You can also assign numerical values to variables and define functions in Mathematica.

Variables and transformation rules

Variables are usually lowercase and entire words can be used to name them. Ex:

x = 5

xvalue = 3

Unless explicitely cleared, values assigned to variables are kept.

Expand[(x + 3)^4]

So if you want an algebraic expression for the above, you need to clear the value of variable x first.

Clear[x]; Expand[(x + 3)^4]

Note: You can put several commands on one line by separating them with semicolons.

In Mathematica, you can avoid all these issues by using transformation rules. The rule x -> 3 represents “x gets replaced by 3”. Here the /. applies this rule to the expression before it—but does not assign a value to x in the process.

1 + x^2 + x^4 + x^5 /. x -> 3

But now:

Expand[(x + 3)^4]

You don't need to clear the value of x in this case.

Define your own functions

The following defines a function f. Note the underscore in x_ and the := sign.

You can give variables, numbers, or symbolic expressions as arguments to f.

{f[x], f[1.1], f[a + b]}

If you use x instead of x_ in defining a function g, the definition will apply only to the literal expression g[x], not to all expressions of the form g[_].

g[x] := Expand[(1 + x)^2]; {g[x], g[1.1], g[a + b]}

Appendices

Summary of keystrokes

to move from one placeholder box to the next: ⇥
power:   Ctrl^
fraction:  Ctrl/
square root: Ctrl2
subscript: Ctrl_
to move cursor out of two-dimensional structure: Ctrl␣

greek letters lowercase: EscaEsc for α
greek letters capital: EscAEsc for Α
etc...for other characters. Look at the CompleteCharacters palette to see the keystroke aliases. They always have the form: EscsomethingEsc

Format
title: Alt1
subtitle:Alt2
subsubtitle: Alt3
section: Alt4
subsection: Alt5
subsubsection: Alt6
text: Alt7
Note: the style by default is "Mathematical Input."

Summary of important Mathematica notation

({{All built-in functions start with a capital letter.,            Abs[-22]}})

({{Use [ ] to enclose function arguments. ,           Mod[7, 3]}})

({{Use {} to enclose list elements.,           {1, 2, 3}}})

({{Use () to indicate grouping of terms.,            a/(b c)}})

({{expr/.x -> y means “in expr replace x by y”.,            p^2 /. p -> 2}})

({{Use = to assign a value to a variable. ,           y = 3.8}})

({{Use == to express equality. ,           Solve[x^2 == 3, x]}})

({{Use := to define a function. ,            f[x_] := x^2}})

({{Use x_ to indicate an arbitrary expression named x.,            f[x_] := x^2}})

Summary of help

In addition to this tutorial, you might try the Wolfram 10 Minute Tutorial accessible through the Display Window at startup.

Otherwise, the help browser is useful too in particular to find the right syntax for a Mathematica function. Simply type the name of the function in the "Go To" box once in the help browser.

In the help browser, you might want to try the various tours/demos/gallery by clicking on the "Getting Started/Demos" button.

For comprehensive information about a topic, click on the "Mathematica book" button.

Mathematica resources at MIT

Mathematica at MIT
http://itinfo.mit.edu/product?name=mathematica
(main IS&T site on how to obtain Mathematica at MIT with documentation, installation instructions etc)
Please note that, under our current license agreement, students can obtain a license at no charge (but for their personally owned machine only).