Mathematica 1
Mathematica 1
Week 1
Eric D. Black
California Institute of Technology
v1.3
1
the cursor is still in the cell with your expression) hit either your Enter key,
which is usually part of your numeric keypad, or Shift-Return. This will tell
Mathematica to evaluate the expression you have just typed. (Just hitting
Return makes a new line inside the cell.)
2+2
You should see the answer appear below your input, as in the first cell of
Figure 1.
2
whenever possible, as opposed to evaluating it and giving you a numerical
approximation. You can force this evaluation using the N function, with the
thing you want to evaluate enclosed in square brackets.
Notice also that Mathematica has built-in fundamental constants, and
that they are represented with capital letters.
Pi = π ≈ 3.14159
√
I = i = −1
3
Figure 2: Double question marks followed by a function name gives you help
on that function.
4
y = Quantity[8, "Meters"]
which produces y = 8 m, indicating that Mathematica understood what you
meant and is now using the abbreviation for the units you specified. You can
try typing in the abbreviation in the Quantity argument, and sometimes
Mathematica will get what you mean. Most units have the expected names,
and of course Mathematica will offer to auto-complete your typing so you
don’t have to memorize all of the specific unit names.
You can combine units in a logical manner, and Mathematica will under-
stand you.
v = Quantity[17, "Meters"/"Seconds"]
which will produce the output v = 17 m/s.
3.2 Uncertainties
Uncertainties can be incorporated into a number using Around. For example,
x = Around[5, 2]
will tell Mathematica that the value for x should be 5 ± 2.
You can combine the two to assign both units and uncertainties.
v0 = Quantity[Around[17, 2], "Meters"/"Seconds"]
which will produce an output of v0 = (17.0 ± 2.0) m/s.
I’m not going to give you any exercises here on this, but remember it. We
will use it next week, and of course you will find it very useful in analyzing
actual laboratory data.
There are many other things Around can do. You can use it to define
asymmetric error bars, for example, but you can also use it to calculate
the mean and uncertainty of a distribution or a list of numbers. All these
things are useful and interesting, but they are beyond the scope of this week’s
handout. You can look them up if you are interested.
4 Lists
Lists are collections of numbers, and they are the structure you are going to
work with the most when analyzing data. You can store them in variables
5
just as you would a constant, and you can name your variables pretty much
anything you want, as long as you stay away from capital letters.
Exercise 5: Define a list of the first five integers using the following syntax.
firstfive = {1, 2, 3, 4, 5}
You can call the value of one particular element of a list using double
square brackets. Type this in, and verify that it returns the fifth number in
the list.
firstfive[[5]]
You can put numbers directly into lists too. Replace the value of the
second number with π.
firstfive[[2]] = Pi
Type the name of the list into a new cell, and hit Enter to see all its
values.
firstfive
You can make tables of just about anything you can code for. To construct
a two-dimensional list of the first five integers (with perhaps the second one
replaced by π) and their squares, enter
6
Mathematica displays lists in bracketed form by default. If you want to
see a table in a more conventional layout, use the TableForm function.
TableForm[squares]
6 Comments
There are two ways to add comments to a Mathematica notebook, by lines
or by cells. To add a comment line inside an executable cell, enclose the
comment in parentheses and asterisks, (* like this *). To create a text-
only cell, create an empty cell, and with the cursor in the cell hold down
the Command key, type the number seven (<Cmd> 7). You can also set the
format of a cell using the Format -> Style pull-down menu.
7
8 Reference
1. Evaluating a cell - Place the cursor inside the cell you want to evaluate
and hit “Enter” or “Shift-Return.”
2. Evaluating multiple cells - Select the cell (or cells) you want evaluated,
pull down the “Evaluation” menu, and select “Evaluate Cells ...”