Phys 218 : Getting Started With Mathematica

I have collected some suggested links to help you get started at our course web page:

Hyperlink[
"http://workbench.lafayette.edu/~doughera/courses/phys218/phys218-links.html"]

I particularly recommend the "Getting Started" screencasts, available here:

Hyperlink[\!\(\*
StyleBox["\"", "Input"]\)http://url.wolfram.com/5kVeH2w"]

I also recommend working through at least the first few sections of the “Core Language” tutorials in the on-line help.  See the following link:

Hyperlink["HowTos","paclet:guide/HowToTopics"]

Note that Mathematica notebooks can contain a mix of commands and text, and can be organized into sections and subsections with appropriate titles.  Click on the cell selection bar at the far right of a cell, and then select Format -> Style to change the style.  Check the various cells in this notebook to see some examples.

Getting Started

Mathematica remembers definitions, even if you delete them from the notebook.  This can sometimes be confusing, so it’s often a good idea to simply clear everything and start anew.  This is especially true if you use the Evaluation -> Evaluate Notebook menu item.

Clear["Global`*"] (* Clear all variable and function definitions *)

(Text entered between (* and *) is a comment.  It is ignored by Mathematica, but is useful to give the reader helpful hints.)

Variables

The assignment operator is the ‘=’ sign.

x = 7 (* Assign the value 7 to the varaiable x *)

The next odd-looking expression takes the value of x (7), adds one to it, and then assigns the result back to the variable x:

x = x + 1

Now x is 8.

x

Variables can be more than one letter.   All Mathematica functions start with an upper case letter (e.g. Cos, Sin, Log, Sqrt) so by convention, user variables usually start with lower case letters.

mass = 0.080

Mathematica prints out the result automatically.  You can suppress that by ending a line with a semicolon:

k = 4;

You test for equality with a double equals sign, ‘==’

x == 7

x == 8

You can also use Greek letters such as ω and Δ.  Use the menu item Palettes -> Classroom Assistant to open up the Classroom Assistant Palette, which has handy ways to enter a variety of mathematical quantities.  If you let the mouse hover over any item, Mathematica will also tell you the keyboard shortcut.

Lists

A list is a collection of items in curly braces, separated by commas.  (Search for the “Work with Lists” HowTo at the link above, and for the  “Making Lists of Objects” Tutorial in the on-line Help.)

Here is a list of five items:

list1= {1, 2, 3, 4, 5}

One handy way to create a list is with the Table[ ] function.  The first example creates a list of successive values of ‘i’, where ‘i’ goes from 1 to 5.  The second goes from 0 to 1 in steps of 0.2.  See many more examples in the online help for Table.

list2 = Table[i, {i, 1, 5}]

list3 = Table[i, {i, 0, 1, 0.2}]

Nested Lists

Each item in a list can also be a list.  Suppose we want to calculate position x as a function of time t.  Each of our data points will be a list:

p1 = {t1, x1} (* t1 and x1 are the initial time and position. *)

p2 = {t2, x2} (* t2 and x2 are the second time and position. *)

We can then build up our data as a list of data points:

data = {p1, p2}

Sometimes, it is easier to visualize if you use TableForm.

TableForm[data]

Finally, here we make a list of data for the function x = Cos[t], where t runs from 0 to 1 in steps of Δx = 0.2.

data = Table[{t, Cos[t]}, {t, 0, 1, 0.2}]

TableForm[data]

Functions

Functions are rules that act on patterns.  Functions in Mathematica act on arguments that are enclosed in square brackets.  Here is a simple function that calculates the square of a number.  On the left hand side, use the underscore ‘_’ to indicate a pattern, and use the colon-equals combination  := to use the ‘SetDelayed’ feature.  Again, see the HowTo for more details.

Clear[x] (* Erase the old value of x to avoid confusion *)

f[x_] := x^2

f[x]

The thing in the square braces can be anything.

f[6]

f[banana]

f[♪]

If you haven' t defined a function yet, Mathematica simply echos it back:

g[x]

A function knows how many arguments it is supposed to have.  If you call it with the wrong number of arguments, Mathematica assumes you meant a different function that you just haven’t defined yet, and so just echos it back.

f[a, b]

Simple Plots

If you have a list of data points, it is easy to plot them.

data1 = Table[{t, Cos[t]}, {t, 0, 1, 0.1}];
TableForm[data1]

ListPlot[data1]

If you have more than one data set, you can plot them both on the same graph.  This is often convenient when comparing calculated values with a theoretical prediction.

data2 = Table[{t, Sin[t]}, {t, 0, 1, 0.1}];
TableForm[data2]

This next command plots both together.  The first argument to ListPlot is always the data to plot.  In this case, the first argument is a list (in curly braces).  That list contains two data sets,
data1 and data2.  Each data set is, in turn, its own list of {t, x} pairs.

ListPlot[{data1, data2}]

It can be confusing to read such a graph unless you have a legend telling you which color goes with with data set.  The PlotLegends option allows you to do just that.  It takes a list (enclosed in curly braces) of names to apply to each data set.

ListPlot[{data1, data2}, PlotLegends -> {"First list", "Second list"}]

Similarly, you can overlay two function plots.  To compare the sine and cosine, for example, you could
do

Plot[{Sin[x], Cos[x]}, {x, 0, 1}, PlotLegends -> {"Sine", "Cosine"}, AxesLabel -> {"x", "Trig Functions"}]

Exercise #1

Create a graph comparing Sin[x] to the first two terms in the Taylor series expansion of Sin[x] over the range [0, 1.5].  Use a spacing of Δx = 0.1, and put labels on your graph so that it is clear which is which.