This tutorial is a brief slide-show introduction to Mathematica. It gives you the basics you need to start harnessing the power of the world's most sophisticated technical computing system.

You can change the examples in any of the slides, and re-evaluate them in place. Normally changes you make will not be saved. To save them, explicitly use Save As... in the File menu.

The top of each slide has a toolbar with buttons to navigate between slides. After you have closed this tutorial, you can always open it again from the Help menu.

({{ , }})

Do interactive calculations using notebooks

You normally interact with Mathematica through documents called notebooks. This tutorial is an example of a notebook. Notebooks can have many forms. But typically they consist of cells that can contain text, calculations, or graphics.

Cells are indicated by brackets on the right. This cell is a text cell. Input for Mathematica to evaluate goes in input cells.

To create a new input cell, just click outside an existing cell and start typing. When you have finished, press ⇧|⌤. Mathematica evaluates your input and puts the result in an output cell immediately underneath.

Here is a sample calculation. Your cursor can be anywhere in the input cell when you press ⇧|⌤. The In[n]:= and Out[n]= labels are added automatically.

5 + 3

You can edit existing input and press ⇧|⌤ to re-evaluate it. The new result replaces the old one in the output cell.

264/4

Go ahead and change some of the numbers in the input cell above. Then press ⇧|⌤ to redo the calculation.

Get started just like using a calculator

To interact with Mathematica, type in your input, then press ⇧|⌤ to get your output. If you press ⌤ on its own, you just go to a new line. In the example below, the ^ indicates the operation of taking a power.

19^50

If you put an explicit decimal point in your input, then Mathematica gives you an approximate numerical result. Very large and very small numbers are then shown in exponential notation.

19. ^50

Use either a space or an explicit * to indicate multiplication.

Typing a semicolon at the end of your input prevents output from being displayed.

You can use % to stand for the result of the last calculation you did. This works regardless of where that calculation appears—whether in the same notebook or another one.

Sqrt[%]

Choose from over a thousand built-in functions

All built-in functions work the same: their names start with capital letters, and their arguments are in square brackets.

Log[2, 8]

Functions can be nested. You can use I or i to represent (Sqrt[-1]). To enter i, type EsciiEsc, where Esc is the ESCAPE key.

Cos[ArcSin[I x]]

All but the most common functions and commands have names that are spelled out completely. The constant π is represented by the built-in symbol Pi. You can also enter it as π by typing EscpEsc.

IntegerPart[Pi^2]

The power is placed after the function arguments, not before. So the first term here is correct but the second is not.

Sin[Pi/4]^3  +  Sin^3[Pi/4]

Do numerical calculations to any precision

Mathematica gives exact results for integer computations, regardless of how many digits are needed. The  here indicates continuation to the next line.

125!

You can use N to get an approximate numerical result. Recall that % stands for the last result.

N[%]

You can use E or e to represent the exponential constant e. To enter e, type EsceeEsc.

N[E]

You can specify how many digits to generate in a calculation. Here is the value of e to 60 digits.

N[E, 60]

Do symbolic calculations to get formulas

Mathematica works with symbolic expressions as well as numbers. Parentheses control the grouping of arithmetic operations. Note that the product of a and b is written as a b.Without a space ab is a single variable.

Expand[(a b - c d)^2 + (a d + b c)^2]

Factor[%]

The function Simplify uses various methods to reduce expressions to their simplest forms.

Simplify[Cos[x]^4 - Sin[x]^4]

FullSimplify is a more powerful version of Simplify, but it can take longer.

FullSimplify[Gamma[z] Gamma[1 - z]]

Use lists to represent collections of things

Lists are used to represent vectors, matrices, tensors, sets, ranges of integrals and plots, ands groups of arguments in functions. Lists are given in curly brackets and their elements are separated by commas.

A common way to generate a list is with the Table command. Here {x, 2, 10} is also a list, which specifies that x goes from 2 to 10. If x starts at 1, you can use {x, 10} instead.

Table[2^x, {x, 2, 10}]

A matrix is a list of lists. The = sign is used to define mx, a 3 × 5 matrix.

mx = Table[a x^b, {a, 3}, {b, 5}]

Use Part[m, i, j] to get the element in the ith row and jth column of a matrix m. The short notation m[[i, j]] works as well. You can use lists to group multiple results together.

{Part[mx, 2, 3], mx[[2, 3]]}

Mathematical functions automatically get applied to every element in a list.

Sin[Pi/{2, 3, 4, 5, 6, 7}]

Create 2D and 3D graphics

Here is a plot of a function of one variable. The list {x, 0, 4} specifies the range of the plot.

Plot[Sin[Exp[x]], {x, 0, 4}];

Here is a three-dimensional surface plot of a function of two variables.

Plot3D[Cos[x] Sin[y], {x, 0, 2 Pi}, {y, 0, 2 Pi}];

Do integrals and derivatives

Here is the integral ∫(1/((x^4) - 
                (a^4))) dx. The result has no constant of integration.

Integrate[1/(x^4 - a^4), x]

This differentiates the previous result.

D[%, x]

You can use Simplify to get back the original integrand.

Simplify[%]

Integrate also works for definite integrals, as well as for multiple integrals. NIntegrate finds numerical approximations. Here is the definite integral (∫_0^1)(Sqrt[2 - (x^2) - (x^6)])dx.

NIntegrate[Sqrt[2 - x^2 - x^6], {x, 0, 1}]

Give values to variables

Use = to assign values to variables. You can put several commands on one line by separating them with semicolons. The commands are executed in sequence, with the output being the result of the last command.

x = 3; y = x + 1;  Sqrt[x^2 + y^2]

Values you assign to variables are kept until you explicitly clear them—or end your Mathematica session. Here you might have expected an algebraic expansion, but instead you get a number, since x and y still have the numerical values they were assigned above.

Expand[(x + y)^5]

To avoid such unexpected behavior, it is good to clear the values of variables as soon as you have finished using them.

Clear[x, y]; Expand[(x + y)^5]

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

Solve equations symbolically

Equations are written with == notation. The second argument of Solve is the variable to be solved for. Solutions are returned as rules of the form x -> value.

Solve[x^2 + x == a, x]

The = operator is reserved for assigning values to variables. If you use = instead of ==, Solve warns you, but you may still have inadvertently set the value of a variable.

You can solve equations involving more than one variable. In the output, each sublist represents one solution.

Solve[{x^2 - k^2 == 0, y^2 == x^2}, {x, y}]

Solve can also handle equations involving more complicated functions.

Solve[Log[x + Sqrt[a + x^2]] == b, x]

Solve equations numerically

You can use NSolve to find numerical approximations to the roots of a polynomial equation.

solutions = NSolve[x^3 + x + 1 == 0, x]

NSolve returns a list of rules. To get a list of numbers for x instead, you just apply these rules to x using the /. operator.

x /. solutions

NSolve also finds numerical solutions to systems of simultaneous equations.

NSolve[{x + y == 2, x - 3  y + z == 3, 
        x - y + z == 0}, {x, y, z}]

For non-polynomial equations, you can use FindRoot to search for solutions. The argument {x, 1} tells Mathematica to search for a numerical solution starting at x = 1.

FindRoot[3  Cos[x] == Log[x], {x, 1}]

Solve differential equations

Here is the symbolic solution to a simple differential equation with an initial condition.

DSolve[{y'[x] == a y[x] + 1, y[0] == 0}, y[x], 
      x]

Here is a numerical solution to a nonlinear differential equation. The result is a rule for the function y. InterpolatingFunction represents a numerical function containing numerical data indicated by <>.

result = 
      NDSolve[{y''[t] + 0.1 y'[t] == -Sin[y[t]], 
          y[0] == 1, y'[0] == 0}, y, {t, 0, 50}]

You can use /. to apply the rule for the function y. This makes a plot by evaluating y[x] for a range of values of x.

Plot[y[x] /. result, {x, 0, 30}];

Manipulate vectors and matrices

Vectors in Mathematica are represented by lists. The dot product of two vectors u and v is indicated by u.v.

{1, 2, 3}  .  {a, b, c}

Without the dot, the vectors are multiplied component-by-component.

{1, 2, 3} {a, b, c}

Matrices are represented by lists of lists. This gives the inverse of a 2 × 2 matrix.

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

The dot product of a matrix with its inverse gives the identity matrix. Recall that % means the result of the previous calculation.

{{1, 2}, {2, 3}}  .  %

You can easily calculate the eigenvalues or eigenvectors of a matrix.

Eigenvalues[{{1, 2}, {2, 3}}]

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]}

If you use = instead of :=, the right-hand side of your definition will be evaluated immediately, rather than each time the function is called.

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

Import and export data in a variety of formats

The following imports a graphic in JPEG format. Unless the file is in one of the directories Mathematica searches by default ($Path), you will have to specify the full path.

The result is a Mathematica expression for the graphic in the specified file. You can display the graphic using Show.

Show[image];

Export sees the .gif file extension, and exports a GIF version of the image.

Mathematica supports many import and export formats. Import can, for example, get data from a spreadsheet.

Import["data.dat", "Table"]

Manipulate data

This generates a list of 1000 elements containing a periodic signal with random noise added. The function Random[] gives a random real number between 0 and 1. The function ListPlot plots a list of numbers.

data = Table[Sin[n] +  Random[] - 1/2, {n, 0, 20,  .02}];  
    ListPlot[data];

This finds a five-point moving average by convolving the list with a five-element kernel.

newdata = ListConvolve[{1, 1, 1, 1, 1}/5, data]; 
    ListPlot[newdata];

Analyze data

This imports a sound file and displays a visual representation of the sound. Double-click the graphic to play the sound. To stop it, click anywhere outside the graphic.

sounddata=Import["sound.wav", "WAV"];Show[sounddata];

The absolute value of the Fourier transform has several harmonics, with a dominant frequency close to 1000 Hz.

ListPlot[Abs[Fourier[sounddata[[1, 1]]]], 
        PlotJoined -> True, 
        PlotRange -> {{0, 5000}, All}];

Use pattern matching to transform expressions

Mathematica is based on the unifying principle that everything can be treated as a symbolic expression. Expressions are of the form Head[(Subscript[arg, 1]),(Subscript[arg, 2]),…], where the (Subscript[arg, i]) can themselves be expressions. All operations in Mathematica—whether numeric, symbolic, or otherwise—can be thought of as transformations of expressions.

The symbol _ (called “blank”) can stand for any expression. So for example the pattern _^_ stands for any explicit power. The function Cases gives the elements of a list that match a specified pattern.

Cases[{1, x^2, E^3, 2^t^3, 4}, _^_]

You can name a piece of a pattern to pull it out of an expression. In the pattern _^n_ the exponent is named n.

Cases[{1, x^2, E^3, 2^t^3, 4}, _^n_ -> n]

This shows how you can use patterns to transform expressions. Here the pattern x_ + y_ stands for the sum of two terms.

{a + b, a + b + c} /. x_ + y_ -> x^2 + y^2

Extend Mathematica using add-on packages

Many packages written in the Mathematica language are available. Standard ones are included with every copy of Mathematica.

Needs tells Mathematica that it needs to load the specified package if it has not already done so. To type names of packages you have to find the back-quote character (`) on your keyboard; it is not the same as the single-quote (').

Now you can use functions from the package just like built-in ones.

Show[Polyhedron[GreatStellatedDodecahedron]];

Load packages before using their functions

By giving this input you introduce an object named SpeedOfLight.

Mass SpeedOfLight^2

Mathematica’s standard physical constants package defines its own SpeedOfLight object. Mathematica gives you a warning about this, but does not overwrite the version you have already introduced.

This removes your version of SpeedOfLight, so that Mathematica now has only the version from the package.

Now when you type SpeedOfLight, Mathematica knows that you mean the version from the package.

Mass SpeedOfLight^2

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}})

Use the Help Browser to learn more

You now know enough to start using Mathematica. But the examples you have seen represent only a small part of its capabilities. Here are some resources you can draw on to learn more about Mathematica.

Built-in Functions

Click a link to view a list of all built-in functions in that category.

({{Numerical Computation, Algebraic Computation, Mathematical Functions}, {Lists and Matrices, Graphics and Sound, Programming}, {Input and Output, Notebooks, System Interface}})

Help Browser

The Help Browser contains detailed information on all aspects of Mathematica.

({{Built-in Functions, Add-ons  & Links, The Mathematica Book}, {Front End, Getting Started, Tour}, {Demos, Master Index, }})

Web and Email

You can access all documentation on the web at documents.wolfram.com. For news and updates, visit www.wolfram.com/products.

For technical support, email support@wolfram.com. Send your questions and comments to info@wolfram.com.

Enjoy your experience with Mathematica!