First Steps with Mathematica

Mathematica is a general-purpose tool for symbolic and numerical calculations, modeling, visualization, and more.

A Very Expensive Calculator

The simplest way to use Mathematica is like a calculator. You just type in some numbers and operations and hit “Shift-Enter,” like this:

2+2

On keyboards with a numeric keypad, you can usually just hit the second “Enter” key instead of the “Shift-Enter” combo.

2*3+4+5*9

Notice that Mathematica recognizes the order it should do things. It multiplies 2 and 3 before adding the result to four, and it multiplies 5 and 9 before adding them in. If you leave a space between two numbers, Mathematica assumes you want to multiply them and even adds in a little gray “x” to remind you.

149 2013

Look what happens if you divide two numbers:

3/4

You get a fraction, instead of 0.75. In Mathematica, numbers are not reduced to decimals unless you specifically ask. There are lots of ways to do that. Let me show you a few; we’ll explain them later.

3./4

N[3/4]

3/4//N

If you want to do something like take 7 and raise it to the 63rd power, you do it like this:

7^63

Just about every known function is built into Mathematica, and can be accessed with the right name. If you want the Cos of π/4 radians, it’s

Cos[(π/4)]

Mathematica defaults to radians when you use trig functions. If you wanted the Tan of 30 degrees, you have to specify that:

Tan[30 Degree]

Notice that the name of the function always starts with a capital letter (“Cos,” not “cos”) and the argument always goes inside square brackets, not parentheses. This is standard Mathematica syntax and it works this way for everything.

How many ways can you choose 7 things out of 20?

Binomial[20,7]

I can never remember whether the 20 comes first or the 7 comes first. If you can’t remember something basic about a function, type a “?” followed by the function name (no space) and hit “Shift-Enter.”

?Binomial

Mathematica shows you a little usage message. If you click the “>>” double arrow it takes you to a more extensive help page.

And extensive is definitely a good description of Mathematica’s built-in documentation. It has thorough help with lots of examples -- if you printed it all out the result would be thousands of pages long.  Just click on the “Help” menu and select “Wolfram Documentation”.

The creators of Mathematica have spent a lot of time thinking about how best to explain it to new users. I strongly recommend taking the time to work through the first few sections of “An Elementary Introduction to the Wolfram Language.” (That was a link; you should click it.)

More Than a Calculator

Of course, this is all stuff you can do on a calculator. Mathematica has a lot more to offer than that.  What if you want to solve an equation?

Solve[(x^3)-7(x^2)+12x-4==0,x]

What if you want to solve two equations?

Solve[{(x^2)+2y*x+4x-3y==0,(y^2)-3x*y==0},{x,y}]

By the way, notice that the powers in those equations look a little different than the 7^63 I typed in the last section.The basic Mathematica notation is pretty easy to get the hang of, but it doesn’t always look like what you would write on a piece of paper. Mathematica lets you enter stuff that way, as well. If I want to raise 7 to the 63rd power I type "7" followed by “CTRL-^” and then “63.” You get this:

(7^63)

If you want to take a square root you can raise a quantity to the 1/2 power, or type “CTRL-2" to bring up a square-root with a blank space in it:

36^(1/2)

(36^(1/2))

(Sqrt[36])

There is usually more than one way to represent the same operation in Mathematica. (By the way, parentheses in Mathematica work like parentheses in algebra. They mean “do the stuff inside the parentheses first.”) Besides keyboard shortcuts, you can also enter common notation using “Palettes.” Just click on the Palette menu and select something like “Basic Math Assistant.” Clicking its buttons will insert expressions into your document, ready to be filled in:

(7.4^(1/4))

(∑_(k=0)^n)Binomial[n,k]

What else can Mathematica do? It can evaluate derivatives and integrals:

D[(x^7)-5(x^6)+13(x^4)+3(x^2)-7,x]

Integrate[(Sqrt[(x^2)+a]),x]

Notice the syntax: The name of the operation, a square bracket, the function, a comma, the name of the variable, then you close the square brackets. For a definite integral you add in lower and upper bounds:

Integrate[(x/(Sqrt[(x^2)+4])),{x,0,3}]

What about differential equations?

DSolve[(x^2)y''[x]-2x*y'[x]-4y[x]==0,y[x],x]

The “C[1]” and “C[2]” are constants.

You can also plot things:

Plot[Sin[x],{x,-π,π}]

Or multiple things:

Plot[{Sin[x],Sin[2x],Sin[3x],Sin[4x],Sin[5x]},{x,-π,π}]

You can plot in 3-D:

Plot3D[(x^2)+x*y-(y^2),{x,-2,2},{y,-2,2}]

And make things like contour plots:

ContourPlot[(E^(-x+y))*((x^2)+y-3),{x,-1,1},{y,-1,1}]

You can plot vector fields, too.

VectorPlot[{-y,x},{x,-2,2},{y,-2,2}]

Functions

Many of the things you want to do with Mathematica are already built-in. But often you’ll want to define your own functions.

Like I said before, Mathematica’s built-in functions all start with a capital letter (and often have other capital letters in their names) and use square brackets for the argument(s). If I just pick a letter and add square brackets and a variable, Mathematica assumes it’s some function. So, for instance, if I type “f[x]” and hit Shift-Enter, it just gives me back f[x].

f[x]

If I take its derivative, Mathematica returns f'[x]:

D[f[x],x]

But what if I want to make a specific function named “f[x]”? I do that by typing the name, followed by square brackets, then “:=”, and finally what I want for the function.

f[x] := (x^2)+17x-3*(E^x)

Once I hit Shift-Enter, Mathematica knows about the function.

D[f[x],x]

(Notice that Mathematica uses “E” instead of “e” for 2.71828...)

Integrate[f[x],x]

What happens if I try to plug in something else? Can I evaluate my function at the point x=4?

f[4]

f[y]

What happened? Why didn’t Mathematica evaluate my function?

When I defined the function I told Mathematica "There's this function f, and its argument is x, and here's what it does with x." If I want a function that can accept anything besides the letter “x” as its argument, I have to tell Mathematica that anything can go in the argument.  Let me start over by “clearing” the function, so I can use the name again.

Clear[f]

To define a function that can take any argument, I add an underscore “_” after the variable in the argument, like so:

f[x_]:= (x^2)+17x-3*(E^x)

Now I can plug in anything I want:

f[x]

f[y]

f[4]

The underscore tells Mathematica that it should take whatever shows up in the square brackets, and use it to do the stuff I defined on the right-hand-side of the “:=” assignment.

Once you define a function, you can do whatever you want with it:

D[f[z],z]

Plot[f[x],{x,0,3}]

Equal Signs

There are lots of different equal signs in Mathematica. You saw one a moment ago, “:=” that means “when someone types f[x] or f[4] or f[goat], do the following operations with the argument.”

f[goat]

You can use a regular equal sign to assign a value to something. If I’m doing a calculation and I want the quantity M to have the value 2, I type

M=2

From now on, M is always immediately replaced by 2, anywhere it appears.

(M^3)

f[M]

So this use of “=” means “always replace the thing on the LHS with the RHS.” If I don't want it to be 2 anymore, I clear it:

Clear[M]

The other equal sign that you’ll see a lot is the double-equal “==.” This shows up when you are solving equations. Mathematica interprets the statement “LHS == RHS” as “LHS is equal to RHS.”

Solve[(x^2)==4,x]

If I use it on its own, it returns True or False.

2==3

2==2

This takes some getting used to. You’ll use “=” when you mean “==” a lot, and you’ll find yourself trying to use parentheses instead of square brackets for functions.

Lists, Vectors, and Matrices

Mathematica uses “lists” for anything that comes in multiples. That could be a set of numbers, or a bunch of data points, or a vector or matrix. Lists begin and end with curly brackets; the entries are separated by commas.

data1 = {1,6,4,3,8,6,5,9,10,14,12,8,5,2,2,1};

The semicolon at the end tells Mathematica it shouldn’t print the list I just typed. Whenever you add a semicolon at the end, the command is executed but no output is shown.

Here's a plot of those numbers. The vertical axis is the number, and the horizontal axis is its spot in the list.

ListPlot[data1]

Once you have a list, you can grab a particular entry by adding double-square brackets at the end, with a number that refers to the part of the list you want.

data1[[1]]

data1[[15]]

Notice that all lists have a “zeroth” part, an ID that Mathematica uses to identify the object as a list.

data1[[0]]

What if I want to plot a bunch of (x,y) data points? I just make a list of lists. That is, I make a list with as many entries as data points, and then I make each entry a list with an x-value and a y-value:

data2 = {{4,7},{2,-8},{3,6},{1,11},{5,-4},{9,11},{12,1}}

I didn't add the semicolon, so Mathematica repeated the list to me. Now if I plot the list (using the ListPlot command -- notice the capital “L” and “P”) I get an x-y plot:

ListPlot[data2]

If I want to see my list as a column, I use the “Column” command with the name of the list as its argument:

Column[data2]

Another way to do this is what is called “Postfix” notation. You type the argument -- the thing you want to do something with -- followed by a double slash // and then the name of the command.

data2//Column

A vector is just a list in Mathematica. Here is a vector with three components:

A={4,-3,5}

And here is another:

B={7,11,-2};

If I want to take their dot product I use... a dot.

A.B

A vector’s magnitude is the square root of its dot product with itself:

(Sqrt[A.A])

(Sqrt[B.B])

What is the angle between A and B, in radians?

ArcCos[((A.B)/((Sqrt[A.A])(Sqrt[B.B])))]//N

What is it in degrees?

((%)/Degree)

In this last expression I used “%” as a stand-in that means “the last piece of output.”

A Matrix is also a list of lists. If it’s a square matrix, then each of its n entries is also a list with n entries. For example, a list of 3 lists that each has 3 entries, or a list of 2 lists that each has 2 entries.  Here is the 2x2 matrix that rotates a vector counterclockwise by an angle θ.

R[θ_]:= RotationMatrix[θ]

I thought typing “RotationMatrix” was a little unwieldy, so I made a new function R with a single argument θ. I typed “θ” by pressing ESC-q-ESC. Try it yourself -- you can enter most greek letter this way.

If I want it to look like a matrix I can use the “MatrixForm” command.

R[θ]

MatrixForm[R[θ]]

R[θ]//MatrixForm

Here are unit vectors that point in the x- and y-directions:

xh={1,0};
yh={0,1};

If I apply R[(π/4)] to these vectors using matrix multiplication, I get the expected result:

R[(π/4)].xh

R[(π/4)].yh