A Minimalist Introduction to Mathematica

For many things Mathematica behaves much like the calculators that you are likely accustomed to using. The first thing you need to know is how to tell it to run a calculation. Below I have put the input 1+1. To get Mathematica to compute the answer, click in the box where it is written and hit the Shift and Enter keys together. Unless you use the number pad, just hitting Enter will only move you to the next line of input, like a carriage return on a typewriter, and so it is essential to also hold the shift key.

1+1

Try a few calculations of your own using subtraction, multiplication and division operations. You will notice that if you divide whole numbers in Mathematica that it will return the rational form of the division and not the decimal one. To get a decimal expression you can add a decimal point after one or both numbers, e.g. 4./5 or 6/7. Try it.

35-4

3*4

4/5

For this lab the main tool you will use is lists.  A list is any collection of numbers (or other things like text) lined up in a row. If you’ve never done any programming the only tricky piece in any computer coding is using the right `syntax’. That is, the correct symbols to tell the computer instructions. In order for a computer to know what you want it to do, you have to tell it precise instructions with precise symbols. If you use different symbols than it expects, even if those symbols usually mean the same thing in ordinary everyday usage, it won’t work. I mention this here, because lists are an example of this. To enter a list in Mathematica you have to use the curly brace { to open the list, commas to separate its elements, and a curly brace to end the list }. If you use parenthesis it won’t work. Here’s a list that I’ve given the name data:

data= {0,1,2,3,4,5}

Enter this list as a command to Mathematica. Now, simply type “data” as a command and see what happens. You can do operations on whole lists at once. Try 2*data.

The most important tool you have to learn to use Mathematica is the documentation built into the program. Go to the help menu at the top of your screen. Click on it and select “Wolfram Documentation”. A new window will open with a search bar near its top. Click in this bar and search for List. The documentation page will explain the syntax (exact symbols you need to enter a list) and give you several examples. Whenever you need more information about how to use commands go to this essential documentation. Another important syntax note is that unlike many other computing languages Mathematica keeps track of capital vs. not capital letters.

You can plot lists:

ListPlot[data]

What is the meaning of the x-axis in the plot you just made?

You can make lists where every element of the list is a list:

data2={{0,0},{1,1},{2, 4},{3,9},{4,16},{5,25}}

If you plot this list it will treat the first number in the pair as the x value and the 2nd as the y value of the point (x,y). Try it:

ListPlot[data2]

This is very useful for plotting experimental data.

Mathematica can also tell you the equation of a line that fits a set of experimental data. Let’s try this. Here’s another data set:

data3={{0,0},{1,1},{2,2},{3,3},{4,4},{5,5}}

Before proceeding predict the slope and y-intercept of the line that best fits this data. Now, ask Mathematica to find it using the “Fit” command:

line=Fit[data3, {1,x},x]

A few remarks: First I’ll explain the syntax. After you tell Mathematica what data you want to fit, data3 in our example, you have to tell it what kind of function you want it to try to fit to the data. In our case we want to try to fit a straight line with the equation y=m x + b. The list {1, x} tells Mathematica first that you want it to allow a constant term in the fit, represented by the 1 in the list. This will allow it to find the y-intercept, which we called b in the equation of the line. Second, the x in the list tells it that you also want to allow for a linear growth with x. Including this term tells Mathematica to try to find the slope, m, of our equation. Finally, after the list {1, x}, you put a comma and x again. This final x tells Mathematica what to think of as the variable in the equation.

One thing you might be surprised by in the answer that Mathematica gives in its best fit equation is that the y-intercept is not exactly zero. This is because computers can’t do numerical calculations to infinite precision. Instead they always round the numbers at some point. Unless you change its default configuration Mathematica rounds off numbers to the nearest (10^(-16))th. For our immediate purposes you can treat any result that Mathematica spits out of this size as exactly zero. It’s an interesting topic in numerical methods to figure out why computers usually have this round off error.

We all appreciate seeing the fit and the data on the same plot. Here’s how to do it. First make a plot of the data using the ListPlot command you learned above and give it a name, here I’ve used dataplot:

dataplot=ListPlot[data3]

Next make a plot of the function that came out of your line fit. You can do this two different ways. First you could copy the function directly and paste it into the Plot command:

lineplot=Plot[-1.812986607347358`*^-16+1.` x, {x,0,5}]

Alternatively, since we named the fit when we made it, we can just put the name into the Plot command to get the plot:

lineplot=Plot[line, {x,0,5}]

Note that I used the same name for both plots. When you do this, Mathematica only remembers the most recent assignment to that name. That is, it will only remember the result of the second plot command. Along these lines, if you just want to name the output of a Plot command (or any other command) but don’t want to see the plot you can put a semicolon at the end of the command:

DontShowThisPlot=Plot[(x^2),{x,-2, 2}];

The list at the end of the Plot command tells Mathematica what the variable is, x here, and the range of values to take x through to make the plot. Now that you’ve made both the dataplot and the lineplot you can overlay them using the Show command:

Show[dataplot,lineplot]

If you would like to look at your data in a table format, you can do this with the Grid command. Try these two commands.

LabelData={{"Children", "Insanity"},{0,0},{1,1},{2,4},{3,9},{4,16},{5,25}}

Grid[LabelData,Alignment->Center,Spacings->{2,1}, Dividers->{False,{True,True,False}}]

Armed with the ability to do calculations, make plots, fit data, and make tables you have everything that you need for this first lab. Of course, feel free to direct any questions that you have to me, or to our tutors. And again, the help documentation is one of the best features of Mathematica, use it constantly.