Introduction to Mathematica

This guide will introduce some of the basic features of Mathematica along with some helpful tips. So what is Mathematica? It is probably best to think of it as Wolfram Alpha on steroids (or Wolfram Alpha as Mathematica Lite). It can do all all sorts of things from statistics to large scale scientific computing to 3d printing. For us, it will mainly be a helpful tool for visualization and an incredibly powerful calculator. Before we start, click on the evaluation menu up top and then click “Evaluate Initialization Cells.” This step is just for this tutorial; you won’t have to do this with your own notebooks.

Mathematica Notebooks

This document is an example of a Mathematica notebook. It is the basic way to interact with Mathematica. To start an interaction, click on a blank space and hit “enter” to create a new cell. Cells are the places we enter code into Mathematica. In the empty grey cell below, type “4*6” and hit “Shift”+“Enter” to evaluate the cell.

You should see a new cell just below yours that gives the output. Using cells, we can perform all of our basic arithmetic. As usual, * is multiplication and ^ is exponentiation. If you need to use e, you can either type “E” or hit “esc”, type “ee”, and hit “esc” again. To use π, you can either type “Pi” or hit “esc”, type “pi”, and hit “esc” again.

Your Turn

Create a cell directly below this text and use it to evaluate (e^(.5)) and (e^((1/2))). To calculate both in one cell, enter them on separate lines (“enter” makes new lines, like usual), then hit “shift”+“enter” to evaluate the cell like before. Don’t forget the parenthesis. Your cell will likely have a white background, so don’t worry if it looks different than the one above.

What did you notice? If everything went right, you should have a nasty decimal and the square root of e. This is because Mathematica treats special numbers like e and π a bit differently from most numbers. It has long lists of rules for how they interact with integers, fractions, exponents, trig functions, etc. It doesn’t have rules for how they interact with decimals, so it writes them as decimals first, then does the entire computation with decimals.

Run the cell below to see why this difference might matter for us.

Integrate[E^(x^2),{x,0,1}]
Integrate[E^(x^2.0),{x,0,1}]

The first output is a representation of the integral as a special function. If you look up the definition of the special function, it is defined as the integral of (e^(x^2))with some constants thrown in. In other words, Mathematica really can’t do anything with it, so it spits out exactly what you entered, just translated into its own internal language. The second output is it’s best numeric approximation of the integral, which is all we can hope for in this case.

The Take Away :

If you keep getting a decimal, but you need an exact answer, make sure you don’t have any decimals in your input. If Mathematica seems to freeze up or spits out what looks like nonsense when you ask it to do something, it might not have an exact value, so adding a decimal (multiplying by 1.0 works) will at least get you a numeric approximation.

A Couple of Quirks :

There are a few things that Mathematica does that don’t match up with the way we write math on paper. First, and most important, Mathematica treats parenthesis “(“ and “)”, brackets “[“ and “]”, and braces “{“ and “}” as different things. So, where we might hand write (([x+(((y-2))^2)])^(1/2)), using the brackets to help keep things organized, Mathematica will be very unhappy with that expression.

Brackets

Mathematica is unhappy because it uses brackets exclusively to tell it what you are plugging into a function. Run the cell below to see the difference.

Sin[π]
Sin(π)

Confused? The top expression evaluated to 0 as expected. The bottom expression is less obvious. Since Mathematica doesn’t see any brackets there, it assumes Sin isn’t a function, so it treats it like a variable named “Sin” and multiplies it by π. But, because there probably isn’t a variable named “Sin”, Mathematica just multiplies the word “Sin” by π. It tries its best, but, if you forget to use brackets, it’s best will still ruin your computation.

Parenthesis

Mathematica uses parenthesis the same way your calculator does - to determine the order of operations. This means that we need to be careful. If you type e^1/2, Mathematica will interpret it as ((e^1)/2)which is likely not what was intended. You probably meant e^(1/2), which computes (e^(1/2)). You can also type (x+3)(x-4) instead of (x+3)*(x-4), which can make code look a little tidier.

You Try

Create a cell below and evaluate (Sin^2)(4·(π^(1/2))).

Braces

Likely the most confusing of the three, Mathematica reserves braces for lists. Mathematica loves lists. It loves them so much that basically everything in Mathematica (other than functions and numbers) is a list. When you make Mathematica plot something, it draws a pretty picture, but, it’s secretly a list (I’m not kidding). While we won’t take full advantage of the power of lists in Mathematica (though I encourage anyone with an interest in functional programming to use the help files to learn to play with lists), we will need them for a few things.

Graphs and Integration

When we have Mathematica draw plots and compute integrals, we need to tell it which letter is the variable, and we need to tell it what values to plot or what the limits of integration are. Mathematica wants this information in list form. The format might vary a little, but, usually, {x,1,2} tells Mathematica that x is the variable and that we want to go from x=1 to x=2.

In the cell below, change the code to make Mathematica plot from x=-1 to x=1.

Plot[Sin[x],(* Type your list here *)]

Later, when we start doing plots in 3d and double/triple integrals, we will need to give functions multiple variables and ranges. For example, we might want to plot over x=-1 to x=1 and y=2 to y=10. To do this, we just write one list for each variable and separate them with commas. Change the code below to make Mathematica plot over x=-5 to x=5 and y=-5 to y=5.

Plot3D[Exp[-Sin[(x^2+y^2)/4]],(* Type your lists here *)]

You should get a nice graph like the one below. You can click and drag on the graph to rotate it.

Plot3D[Exp[-Sin[(x^2+y^2)/4]],{x,-5,5},{y,-5,5}]

Vectors

The other reasons we will need lists are vectors and parametrizations. We won’t get into it now because we haven’t covered enough material to do much with them, but, we can at least write what the syntax is. When we write vectors on paper, we usually write (OverVector[v])=a(OverVector[i])+b(OverVector[j])+c(OverVector[k]) or (OverVector[v])= ⟨a,b,c⟩. Mathematica prefers this as a list, so we type {a,b,c}. The code below is an example of this sort of thing; the first list is a vector function, and the other three specify the plot’s domain.

VectorPlot3D[{-y,x,z},{x,-5,5},{y,-5,5},{z,0,5},VectorScale -> Medium, VectorPoints -> 5, VectorColorFunction->Hue]

Capitalization

The built in functions in Mathematica capitalize the first letter of each word. So, instead of sin(x), we have Sin[x]. For longer function names, we’ll have something like VectorPlot3D or RegionPlot.

Useful Functions

Here we will list some common functions and their Mathematica syntax. 
The built in help files are fantastic. If you run into problems, look there first.

Trig Functions

Sin[x], Cos[x], Tan[x]  (* x is an angle measured in radians *)
ArcTan[x], ArcSin[x], ArcCos[x]

Logarithms and Exponentials

Log[x] (* This is the natural log *)
Log[b,x] (* This is the log with base b *)
Surd[x,n] (* This gives the nth root of x *)

Solving Equations

Factor[f] (* Factors the polynomial function f *)
Expand[f] (* Expands a factored polynomial f into long form *)
Solve[f[x] == 0, x] (* Solves the equation f[x] = 0 and returns a list of roots *)
FullSimplify[f] (* Tries to simplify the expression f using identities *)

Calculus

D[f,x] (* Takes the derivative of f with respect to x *)
D[f,{x,n}] (* Takes the nth derivative of f with respect to x *)
Integrate[f,x] (* Find the indefinite integral of f with respect to x *)
Integrate[f,{x,a,b}] (* Find the definite integral of f from x=a to x=b *)

Plotting

Plot[f,{x,a,b}] (* Plot f[x] from x=a to x=b *)
ParametricPlot[{x[t],y[t]},{t,a,b}] (* Plot the parametric function r[t]=<x[t],y[t]> from t=a to t=b *)
RegionPlot[l<f[x,y]<u, {x,a,b},{y,c,d}] (* Fill in the region in the plane where f[x,y]>l and f[x,y]<u over the region from x=a to x=b and y=c to y=d; more complicated variations on this will be usefull later *)

This should be enough to get you started using Mathematica. The next steps are defining functions, making plots look fancy, and making interactive plots. These should be introduced throughout the semester, either during recitation or via further prepared notebooks.