Inputting, Plotting, and Fitting Data

Starting Up

The following two commands prevent annoying warnings about the use of symbols similar to those used by Mathematica because Mathematica will not let us change their definitions.

Off[General::spell]; Off[General::spell1];

Initialize and load some extra needed packages.

Needs["Statistics`DataManipulation`"]
Needs["Statistics`DescriptiveStatistics`"]
Needs["Statistics`LinearRegression`"]
Needs["Graphics`MultipleListPlot`"]

Experimental Data

If you have acquired data by hand, you will input the data as lists of values, denoted as {a1,a2,a3...} - note the curly brackets.  You will generally have a set of x values and y values.  For plotting purposes, Mathematica wants a list of pairs {{(Subscript[x, 1]),(Subscript[y, 1])},{(Subscript[x, 2]),(Subscript[y, 2])},...}.  You have 2 options: enter the pairs directly (which can get tedious if you have a lot of data), or enter an x list and y list and use the Transpose function.  Both methods are illustrated below.

data1 = {{1, 1}, {2.5, 4}, {3.9, 8}, {6.1, 
            11.5}}

xlist  =  {1, 2.5, 3.9, 6.1};   (* 
          a semicolon after a line does not display the output *) 
ylist  =  {1, 4, 8, 11.5}

data2  =  Transpose[{xlist, ylist}]

Note that data1 and data2 are identical.  Be sure to use the {} in the Transpose function.

-A sometimes useful command is Length which tells you the length of your vector:

Length[xlist]

Working with a selected set of data

To refer to a specific element use the form [[i,j]] where i is the pair index and j refers to the location within the pair:

data1[[2, 2]]

Here are some other useful ways to get subsets of your data:

data1[[3]]

Take[data1, 3]

Take[data1, -2]

Drop[data1, 2]

You may take some data with the computer but only the a portion looks useable.  It will be most convenient to extract data based on the x values, rather than the positions in the list.  In this case you will use the command  Select.

selecteddata = 
      Select[data1, ((#[[1]] > 2 ) &&  (#[[1]] < 
                  4)) &]

This rather inscrutable command selects the data with  2 <  x < 4.  The expression in parentheses is a logical expression.  #[[1]] refers to the value of the first element of each pair, and #[[1]]>2 tests to see if x is greater than 2.  && is Mathematica's expression for AND.  The last & is just part of how this command is defined. You now have a new data set selecteddata that you can use.

Plotting your data

Mathematica will autoscale your data for you:

ListPlot[data1];

- You can also define the plot region with {{(Subscript[x, min]),(Subscript[x, max])},{(Subscript[y, min]),(Subscript[y, max])}}:

ListPlot[data1, 
        PlotRange  ->  {{0, 8}, {0, 15}}];

You may have uncertainties recorded for the points...

xerror = { .1,  .1,  .1,  .1};
yerror  =  { .1,  .2,  .3,  .4};
errors = Transpose[{xerror, yerror}];

To add error bars to the plot, you need to construct a table of data {{(Subscript[x, i]),(Subscript[y, i])},ErrorBar[(Subscript[σ, (Subscript[x, i])]),(Subscript[((Subscript[σ, y])), i])]} as shown below:

fulldata = 
        Table[{data1[[i]], 
            ErrorBar[errors[[i, 1]], errors[[i, 2]]]}, {i, 1, 
            Length[xerror]}];

dataplot=MultipleListPlot[fulldata](* Use MultipleListPlot to plot error bars...we named a variable for the plot because we will use it later! *)

Simple Data Fitting

Mathematica has built-in linear regression functions. In this case we are fitting a function a + bx+ c(x^2).  The Regress function includes the constant by default.  It outputs the parameter values, the standard deviations, and confidence intervals.  See the Help browser for more info.

Clear[x]; (* Clear is a useful fn .  that clears the variable *)
datafit = 
        Regress[data1, {1, x, x^2}, x, 
          RegressionReport -> {BestFit, ParameterCITable}]

Now plot the fit results:

fitplot=Plot[(BestFit /. datafit),{x, 0, 10},PlotStyle -> RGBColor[1, 0, 0],AxesLabel -> {"x axis",  "y axis"}];(* this shows how to make colors in plotting and labels *)

Now combine the fit and data on one graph (now you know why we named our data plot above):

Show[fitplot, dataplot];(* Show display graphs together *)

This should give you enough to get started in entering data, plotting data, and some simple fitting and error analysis.  If you want to spruce up your plots, add labels,etc. , see  section 2.10.3 Graphics Directives and Options in the Mathematica book accessible through the Help menu.