MathLite: enough Mathematica for an introductory physics laboratory

from Essential Mathematica for Students of Science
© James J. Kelly, 1998
Last revised: January, 2006

Introduction

In this notebook we introduce enough of the features of Mathematica for a student in an introductory physics laboratory to analyze data and prepare lab reports.  It is impossible in a single session to present or to absorb a mathematical software system that is so vast.  A full semester course on Mathematica is available at Essential Mathematica for Students of Science in tutorial format.  Further information and many practical examples can be found there or in the online documentation available through the Help Browser.  Hopefully, by the end of this session you will be sufficiently familiar with the basic structure of Mathematica to make efficient use of those resources.

The notebook interface

I assume that you are reading this document on a monitor coupled to a computer capable of running a notebook interface.  If so, you will see that the present document is divided into a number of sections with clearly identifiable section headings; some sections are also divided into subsections.  The details of the appearance of these headings will depend upon the particular computer system that you are using and perhaps upon some of the options that were selected when Mathematica was installed.  Nevertheless, the logical structure should be unmistakable.

The descriptive sections of this notebook assume that you are using a Microsoft Windows system.  Notebook interfaces for other systems are generally quite similar, but some of the shortcut keys and details of the appearance may vary between interfaces.  We hope that it will not take long to become accustomed to these differences.

Each section consists of a collection of cells, of which there are several types.  The present paragraph sits in a text cell.  Input intended for transmission to the kernel should occupy an input cell and the results returned by the kernel will be placed in an output cell.  Depending upon the style options selected for a particular document, there will usually be several other cell types available, such as title, section, subsection, etc.  The vertical extent of a cell is generally delineated by a bracket that appears at the right side of the display.  The innermost bracket identifies an individual cell, while outer brackets indicate increasingly inclusive logical groupings of cells.  A cell or group of cells can be selected by using the mouse to click on the appropriate bracket, which should then change to a bolder appearance.

Notebooks are often saved with the cells closed — the logical structure of the notebook is then revealed by the organization of its section headings while the details are hidden.  Closed cells are identified by dark hooks at the base of the cell bracket.  To open a cell or group of cells, double click of the appropriate cell bracket.  Repeating that operation then closes the cell.  Alternatively, you can select the cell and use the keystroke sequence Ctrl { to open or Ctrl } to close all subcells within your selection.  Notice that many shortcuts are designed for easy memorization; in this case, an open-bracket opens a set of cells while a closed-bracket closes the cell.

At the top of the screen you should see a familiar menu bar with the headings File, Edit, Format, etc.  Under Edit you see the usual copy, cut, paste, and related actions together with suggested keystroke alternatives.  The most common editing functions have the same keystroke shortcuts found in most other Windows applications, such as CtrlC for copy, CtrlX for cut, or CtrlV for paste.  The copy and cut functions act either on text highlighted by repeated mouse clicks or dragging the cursor or on cells selected by clicking on the appropriate cell bracket.  The paste function inserts text into an existing cell at the current insertion point, indicated by the cursor in the form of a long vertical bar.  Note that the insertion point may not be visible on the screen if you have used page buttons or scroll bars, but you can move it to the desired location by clicking within the desired cell.  Similarly,  by moving the cursor between cells and clicking when you see a horizontal bar, you create an insertion point for a new cell and can paste a selection into it.  By default, new cells are created with the input type, but you can select a different type either with the menu activated by the right mouse button.

Take a few minutes to practice these operations and familiarize yourself with the appearance of the cursor at various positions in the notebook.

Create a new notebook using the File menu.  Activate the toolbar for the new notebook: under the Format menu click ShowToolbar to toggle its status (checked means on).    The box on the left can be used to choose the cell type for a new cell or to change the cell type for a selected cell.  Write a title and subtitle for your new notebook into cells with those types.  Type some text into a cell type.   Then save the notebook using the File menu (use Save As to name it) .  Close the notebook using either the File menu or clicking the x in the top right corner.  Re-open your notebook.

Help!

Help Browser

Probably the most important source of help is the Help Browser provided with Mathematica itself.  The help browser can be accessed from the menu bar and its use should be self-explanatory.  Simply type the name of the function or keyword in the dialog box and search either the appropriate resource index, usually Built-in Functions or The Mathematica Book itself, whose entire text of more than 1000 pages is available online.  Many parts of the Help Browser include statements which you can modify or execute without actually changing anything in your system.  Don't worry — you won't break it!  You can also copy cells from the browser into your own notebook.

Find the examples of Integrate in the reference guide.  Execute some of the input statements by clicking within the cell and pressing Shift Enter simultaneously.  The Enter key from the numeric keypad should also work.

Use the Help Browser to see the syntax for Plot and play with a few of the examples provided.

We also provide hyperlinks to either the Reference Guide or to an appropriate section of the Mathematica Book are provided whenever a new function is introduced or used in a new manner.  Simply clicking on the highlighted button takes you to the appropriate documentation for that feature.  The Help Browser will then provide the definition of the function, its options, and details of its syntax and properties.  In many cases there will also be examples provided and links to related information throughout the Mathematica Book.  Using this method it will not be necessary to provide such details within these notebooks themselves.  For example, by clicking on this hyperlink, Range, one finds that Range[nmin,nmax] produces a list of integers between nmin and nmax (Try it!).

Information escape

Quick help on a built-in function or on a symbol that you have created can be obtained by typing ?symbol or, for more information, ??symbol.

?Fibonacci

?? Fibonacci

Assuming that you already know the definition for Fibonacci numbers or polynomials, this method provides the information needed to use the built-in function.  However, if you do not know that definition, more complete information can be obtained using the Help Browser.  A suitable hyperlink is often included with the output.

Information about the available functions can be obtained by using wildcards.  Thus, we find the following symbols with names beginning with Plot.  Notice that clicking on any of these links provides a brief explanation of the function and a link to more detailed information.

?Plot*

?*Plot*

Input conventions

Symbols

First note that Mathematica is case-sensitive, meaning that a symbol named y is distinct from a symbol named Y.  All built-in symbols are assigned names that begin with capital letters.  Mathematica generally names its symbols quite descriptively with names closely related to the function of each symbol.  For example, the function designed to simplify algebraic expressions is called Simplify.  Sometimes the names become very long, but most users find it easier to type a verbose name than to remember abbreviations which can be chosen almost arbitrarily.  To avoid confusing your variables with names reserved by Mathematica it is good practice to choose names that begin with lowercase letters, but if you also choose long and descriptive variable names that is usually no longer necessary.

Symbol names must begin with a letter and cannot include punctuation marks (_,@,!, etc.), operators (+,-,/, etc.), or embedded spaces.  Numerals may be used anywhere except the first character.  Names cannot duplicate any of those reserved by Mathematica, which include

N	numerical evaluation, confused with number
	I	imaginary unit (Sqrt[-1])
	E	exponential constant e (base of natural logarithms), confused with energy
	C	constant of integration 
	D	partial derivative
	Pi	π

Assignments

A value is assigned to a symbol by symbol=value.  Several symbols can be assigned the same value in a single line: symbol1=symbol2=symbol3=value.  To evaluate a Mathematica statement, click in the cell or select its cell bracket and press ⇧⌤.

a = b = c = Pi

Notice that Mathematica performs some automatic typesetting, replacing Pi by π but does not display a numerical value because it recognizes that a decimal representation would only be an approximation because π is an exact number, even if it is irrational, and can in principle be evaluated with arbitrary precision.  Nevertheless, if you can a decimal representation, the numerical evaluation function N will provide it

N[a]

N[a, 100]

to any desired precision.

Be aware that assignments remain in force for an entire Mathematica session, until reset or cleared, and are not confined to the notebook in which they are made.  One of  the most common errors is to reuse a symbol whose previous assignment has been forgotten.  For example, if we try to solve the quadratic equation symbolically

Solve[a*(x^2) + b*x + c == 0, x]

we obtain rather specific, and clearly unwanted results, because assignments have already been made for the coefficients.  Therefore, we must clear those assignments first!

Clear[a, b, c];

    Solve[a*(x^2) + b*x + c == 0, x]

Notice that == is used to indicate an equation instead of an assignment.  The typeset form is obtained using Esc==Esc.  Similarly, the typeset form of the exponent x^n is obtained using Ctrl^.  Finally, a space between two symbols indicates multiplication.

a = 2.34;
b = 5.67;
a b

Several statements, separated by semicolons, can be included in a single input cell.  Here only the final result is printed because the semicolon suppresses output.  Note that Mathematica aficionados rarely type * for multiplication, although it is allowed and it sometimes improves readability.

Clear[a, b]

Replacements

One of the most powerful features of a symbolic manipulator is its ability to replace one expression with another using replacement rules defined by the user.  A replacement rule is given in the form OldExpression -> NewExpression; a prettier version the arrow,  -> , is entered as Esc->Esc.  The usual syntax for applying a replacement rule to an expression is to use the replacement operator /. after an expression.  Many Mathematica functions return their results in the form of replacement rules.  For example, the roots of the quadratic equation

sol=Solve[a*(x^2) + b*x + c == 0, 
        x]

are returned as a list of replacement rules for the independent variable x.  Thus,

Simplify[a (x^2) + b x  + c  /. sol]

substitutes those solutions into the original expression and the simplify function confirms that both solutions are indeed roots.  One should use replacement rules to avoid making unnecessary assignments.  For example, if we define the expression

quad = a (x^2) +  b x  +  c;

numerical values can be substituted using

quad /. {a -> 2, b -> -2, c -> 4} /. 
      x -> 0.5

without making explicit assignments to the parameters a,b,c or the variable x that would alter the meaning of quad.

Many Mathematica functions accept options in the form of replacement rules.  For example,

Factor[(x^2) + 1]

is returned unevaluated because this expression cannot be factored using just integers.  There is an option, though, that does enable factorization

Factor[(x^2) + 1, GaussianIntegers -> True]

by including integer multiples of i = (Sqrt[-1]).  The function Options provides a list of available options.  Some functions have many more options and you would  need to consult the Help Browser for information about those whose names are not self-explanatory.

Options[Factor]

Delimiters

Traditional mathematical notation can be ambiguous.  For example, should f(a + b) be interpreted as a*f + b*f or as a function f(x) evaluated for the argument x -> a + b?  Humans that understand the context usually have little difficulty parsing such a statement, but machines are much less tolerant of ambiguity.  Therefore, Mathematica assigns rigid meanings to delimiters.  Parentheses are used only for grouping expressions while square brackets are used only for packaging the arguments of a function.  Thus,

f (a + b)

satisfies the commutative law of multiplication.  Using the built-in function Expand with an argument enclosed by square brackets

Expand[f (a + b)]

then produces a fully expanded result.  The table below provides a more complete list of the available delimiters.

({{Delimiters}, {({{delimiter,  , purpose, example}, {parentheses, ( ), grouping, (a + b)*c}, {brackets, [ ], functions, Sqrt[a]}, {braces, { }, lists, {a, b, c}}, {double brackets, [[ ]] , part of expression, list[[2]]}, {comma, ,, separate elements, {a, b, c}}, {semicolon, ;, separate expressions, a*b;  b - c}, {quotes, " ", text strings, "Have a nice day!"}, {comment,   , documentation,   }})}})

Functions

Standard mathematical functions

Practically all standard mathematical functions (arithmetic, trigonometric, exponential, logarithmic, hyperbolic, etc.) are provided.  The names are capitalized and are either standard abbreviations, such as Sin, or are spelled completely, such as Factorial.  A brief summary of these functions is given in the accompanying table.  Representative argument sequences have been included for some functions, but we have not attempted to be exhaustive at this stage.  A vast array of special functions is also available — consult the Help Browser for any function that you might need and it will probably be available!

({{Standard Mathematical Functions}, {({{exponential, Exp[x]}, {natural logarithm, Log[x]}, {logarithm to base b, Log[b, x]}, {trigonometric , Sin, Cos, Tan, Sec, Csc, Cot}, {inverse trig , ArcSin, ArcCos, ArcTan, }, { , ArcSec, ArcCsc, ArcCot}, {hyperbolic trig, Sinh, Cosh, Tanh, Sech, Csch, Coth}, {inverse hyperbolic trig, ArcSinh, ArcCosh, ArcTanh, }, { , ArcSech, ArcCsch, ArcCoth}})}})

Compute (Subscript[log, 10]) e and check the result.

Evaluate sin( (tan^(-1))(x) ).  [Remember to use Mathematica input conventions.]

By default, trigonometric functions assume that the argument is expressed in radians.  For those of us who think in degrees (who doesn't?), the built-in symbol Degree converts from degrees to radians and can be included as a factor in the argument.

Sin[30 Degree]

Use Degree to obtain the number of radians in 32.5°.

Creating functions

You will need to create your functions, but there are a few subtleties that must be understood.  Mathematica evaluates expressions by using pattern recognition to compare an expression with tables of definitions and rules.  The obvious definition

f[x] = (x^2) Exp[-(x^2)/2]

does not work quite as its author probably intends.  For example,

f[2]

does not return the same result as

f[x] /. x -> 2.

The problem is that we provided a definition for the argument x but not for any other argument, such as

f[y]

For a system based upon pattern recognition, there is not preventing us from supplying a completely independent definition

f[y]  =  Sin[(y^3)];

for an independent argument.  The definitions associated with the symbol f can be displayed using

?f

If we want our function to substitute any argument into a specific place in its definition, we need to formulate the definition using a blank pattern which will match any argument presented.  Thus,

g[x_] = (x^2) Exp[-(x^2)/2];

will produce the desired behavior.

g[2. ]

g[y]

g[a x  +  b y]

Appending an underscore to the argument x_ on the left-hand side identifies the argument as a blank pattern that will be named x on the right-hand side.  (Do not use the underscore on both sides.)

Sometimes it is necessary to postpone evaluation of the right-hand side until an argument is presented.  For example, the function

h[x_, y_] = D[x, y]/x

attempts to define the logarithmic derivative

h[x, y] =  (1/x) ∂(x/∂)y

but does not work correctly because the partial derivative of x with respect to y appears to vanish because we do not know yet how x will be related to y.  An alternative version

h[x_, y_] := D[x, y]/x

h[Sin[x], x]

does work correctly because the := operator, standing for SetDelayed, delays evaluation of the right-hand side until the arguments are presented.

Postfix, Prefix, Infix

Mathematica provides a vast library of functions and you will soon create many of your own.  There are several methods for supplying input to functions.  The most obvious method, which was used above, is to use the form function[args,opts] where args represents the list of positional arguments and opts represents a subset of the available options.  Positional arguments are interpreted according to the order they appear in the function definition and must agree in type.  Options may follow the positional arguments, but are optional (in other words, they may be omitted).  Omitted options will be assigned default values.

For example, Expand expands an algebraic expression.

ClearAll["Global`*"];
Expand[ (a + b x) (c x  -  d (x^2)) (e  +  f  +  
            g) ]

However, the expression we wish to expand often arises as the result of an earlier evaluation.  If we then want to use this method to apply another operation to that result, we would need to wrap a new function around the previous result.

Factor[ 
      Expand[ (a + b x) (c x  -  d (x^2)) (e  +  f  +  
              g) ] ]

Although this obviously works, complicated algebraic manipulations may require many steps.  It then becomes difficult to read nested functions from the inside out.  I prefer to use the Postfix form in which f[x] is represented by x//f where // is referred to as the postfix operator.  For the example, above we pass an expression first into Expand and then into Factor.

(a + b x) (c x  -  d (x^2)) (e  +  f  +  g)  // 
        Expand // Factor

As we develop our calculation, we may attach other functions or replacement rules at the end until we achieve the desired final form.  Postfix form is especially convenient for tacking numerical evaluation, simplification, or formatting functions onto the end of an expression.  I find this style easier to read and to understand than the nested representation, but of course that is merely a matter of taste.

Alternatively, it is sometimes useful to employ the Prefix form based upon the prefix operator @, such that f[x] is represented by f@x.  The example above would then be expressed as

Factor @ Expand@(a + b x) (c x  -  
          d (x^2)) (e  +  f  +  g)

An important consideration in the use of postfix versus prefix notation is the precedence of operators.  The postfix operator is assigned a very low precedence so that the entire preceding expression is treated as single entity that is to be passed into the function.

Clear[f]

x + y // f

Because arithmetic and logical operators take precedence over postfix, the sum becomes the argument rather than the last term.  This behavior is to be contrasted with the prefix operator, which is assigned a higher precedence than arithmetic or logical operations.

f @ x + y

Thus, with prefix notation the first term becomes the argument.  Note that Mathematica has its own rules for ordering the terms upon output.  Therefore, one must be very careful to consider precedence when using prefix notation.  I find the prefix form to be more prone to error than the postfix form.  However, even with postfix operations it is often necessary to use parentheses to ensure that the evaluation proceeds in the proper sequence.

f@(x + y)

Finally, Infix notation can be used for functions which take two arguments.  In this case, f[x,y] is represented as x~f~y.

x~f~y

Infix notation also takes precedence over arithmetic or logical operators.

a + b~f~c + d

Lists

Creating and manipulating lists

Lists play a central role in data analysis.  Suppose that we create a simple list using Range[min,max,increment], such as

alist = Range[0, 1, 0.1]

The following operations should have obvious results.

First[alist]

Last[alist]

Length[alist]

Drop[alist, 2]

Drop[alist, -2]

Reverse[alist]

Take[alist, {3, 6}]

Drop[alist, {3, 6}]

A specific element of a list is obtained by double square brackets.  The typeset forms [[]] are written using Esc[[Esc and Esc]]Esc

alist[[3]]

alist[[3]]

A multidimensional list can be created using Table.

blist = Table[i (j^2), {i, 1, 10}, {j, 1, 5}]

Dimensions[blist]

blist[[2, 3]]

MatrixForm displays a two-dimensional list in the form of  matrix.

MatrixForm[blist]

Transpose[blist] // MatrixForm

Listable functions

Many Mathematica functions are listable, meaning that they take arguments in the form of lists.  Listable functions with two arguments, such as Plus or Times, can receive either a scalar and a list or two lists of the same structure.  Given a scalar and a list, a listable function of two arguments is evaluated for each element of the list paired with the scalar.  Given two lists of the same dimensions, a listable function of two arguments is evaluated using corresponding members of the two lists.  In both cases the output is a list of results.

Predict the output of each of the following operations before evaluating them.  Be sure to understand each result before moving on to the next!

list1 = {1, 2, 3, 4, 5};
list2 = {a, b, c, d, e};

list1*x

list1*list2

list2/list1

(list1^2)

(Sqrt[list2])

(list2^list1)

(((list1 + list2))^2)

Application of a function to a list

A function can be applied to a list as a whole using Apply[f,list] or f@@list.  For example, the sum of squares for the first five counting numbers is

Plus @@ Range[5]^2

Alternatively, a function can be mapped onto the individual elements of a list using Map[f,list] or f/@list.  For example, the first five square roots are

Sqrt /@ Range[5]

Contraction

The Dot product between two vectors of  the same length is given by

Dot[list1, list2]

list1 . list2

Similarly, multiplication between a matrix and a vector with compatible dimensions is also performed using Dot.  The dot products can be described as contractions because the number of indices needed to enumerate the result is one less than the number of indices for the input.

{a, b, c, d} . {e, f, g, h}

{{a, b, c}, {e, f, g}} . {x, y, z}

{x, y} . {{a, b, c}, {e, f, g}}

example: variance

Suppose that we create a set of pseudodata (fake data) based upon a known function but with random fluctuations applied.  The following cell creates the data set, using Mathematica comment delimiters to explain each step.

f[x_] = (x^2) 
          Sin[x];

  

    xdata = 
        Range[-0.9, 0.9, 
          0.1];

  


    ydata = 
      f[xdata] + 
        Table[Random[Real, {-0.1, 0.1}], {Length[xdata]}]

The variance is defined by

(σ^2)  =  
      (1/n) (∑_i)((((Subscript[y, i]) - f[(Subscript[x, i])]))^2)

and can be evaluated in Mathematica style as follows.

residuals = ydata - f[xdata];
variance = residuals . residuals/Length[residuals]

σ = (Sqrt[variance])

Here residuals is a vector containing the difference between observed and predicted values; it is contracted with itself (sum of squares) and divided by the number of elements to obtain the mean-square deviation between measurements and predictions.  The same result can be obtained in other ways, such as using Sum, but this style is more appealing to the Mathematica purist.  Also, note that you might want to save typing by abbreviating the name for residuals to r, but the longer name will be more readable if you revisit a notebook after some time.

Creating ntuples

Suppose that we want to combine a list of {(Subscript[x, i])} and a list of {(Subscript[y, i])}, each containing n elements into a list of pairs {{(Subscript[x, i]), (Subscript[y, i])}, ⋯}.  The solution is to use Transpose to transform the 2×n list {x, y} into an n×2 list.

data = Transpose[{xdata, ydata}]

Dimensions[data]

Similarly, if we already have a list of pairs, vectors can be extracted using

{a, b} = Transpose[data];

and below we verify that the original data are recovered.

a == xdata  &&  b == ydata

Packages

One of the most important features of Mathematica is that it is an extensible system.  In addition to the built-in functions available to any Mathematica session, one can add new features by loading a package that contains the necessary definitions.  A package is a special kind of Mathematica file that contains the definitions and specifies the rules for manipulating symbols and functions relevant to a particular purpose.  However, it is important to load the package before you attempt to use the functions it contains — failure to load the package first creates global symbols that override the name of the function that you want.  If you inadvertently invoke a function before its package is loaded, use Remove to get rid of the nonfunctioning symbol and then load the package; the desired functions should then work properly.

The best way to load a package is to use the Needs function, which reads the package if it is not yet on the context path.  The argument is a string of the form "Category`Package`", where the first part identifies the folder and the second the file.  Note that the back-quotes are essential.  Many categories provide a master package that provides access to all of the functions in all of its related packages without loading each package individual.  Loading the master package does not burden the system memory unnecessarily because the associated code is not loaded until it is needed.

Before we load any packages, let's ask what types of plots and probability distributions are available as standard built-in functions.

?*Plot*

?*Distr*

We have a relatively small number of plotting functions (some of the symbols are options, not functions) and no probability distributions available at this time.  Prior to Version 5.1, the following cell would load packages

Needs["Graphics`Master`"];
Needs["Statistics`Master`"];

while with later versions it is sufficient to enter

Needs["Graphics`"];
Needs["Statistics`"];

to obtain a expanded arsenal of graphical and statistical functions.  (Note that we disabled the evaluation of the early version.)  The lists of available plots

?*Plot*

and probability distributions

?*Distr*

are now much larger.  We will not need most of  these, but some will be quite useful for our work.  Clicking any of the links will provide more information on a specific symbol and a link to online documentation for further information.   (Try it!)

Initialization section

Most of my notebooks begin with a section named Initialization that contains cells which select some useful options and load the packages needed by that notebook.  The cells below provide a somewhat delayed example — normally they would be at the top, but would have been mysterious to new users of Mathematica.  The first cell clears all global variables, giving a clean start, and turns off spell-checking warnings that I find annoying; you might want to omit that if your typing is error prone.  The next cell appends the directory containing this notebook, a sample data file gep.dat, and a new package Curfit.m to $Path, which is a default list of directories used to find external files.  This will save typing very long directory names, but you will need to modify this directory name for your own system.  The next cell affects the fonts used in graphics output.  Finally, we load the packages needed by this notebook.  Sometimes this section might include other packages or definitions for some of the functions and rules that will be used extensively later on.  The contents of this section will depend upon your preferences and could be more extensive, but will usually begin with something similar to this example.

ClearAll["Global`*"];
Off[General::spell, General::spell1]

AppendTo[$Path, "C:\Documents and Settings\Jim\My Documents\My \
eBooks\EssentialMathematica\Data analysis"];

$DefaultFont = {"Times", 12};
$TextStyle = {FontFamily -> "Times", FontSize -> 12, 
          FontSlant -> "Italic"};

Needs["Graphics`"];
Needs["Statistics`"];

Plotting

Curves

Obviously, Mathematica provides a large assortment of plotting functions.  We only have time to introduce a few of the most useful and refer you to the Help Browser or to plotting.nb for further information.  The basic syntax for plotting a function of a single variable as a curve is

Plot[f[x],{x,xmin,xmax,opts]

where f[x] is any built-in or user-defined function which evaluates to a real number given a numerical argument x between xmin and xmax, and where opts is a sequence of options given in the form of replacement rules.  Thus,

Plot[ArcSin[x], {x, -1, 1}];

produces a basic plot while

\(\(Plot[ArcSin[x], {x, \(-1\), 1}, Frame -> True, 
        FrameLabel -> {"x", \*""\!\(sin\^\(-1\)\)x""}, 
        GridLines -> Automatic, 
        PlotLabel -> "My First Plot"];\)\)

adds frame, with a list of labels given as strings, adds gridlines, and prints a title.  Many more options are available.

Often it is necessary to use Evaluate to prepare a function for plotting.  For example,

Plot[D[ArcSin[x], x], {x, -1, 1}];

fails because x is substituted before the derivative is evaluated, while

Plot[Evaluate[D[ArcSin[x], x]], {x, -1, 1}];

succeeds because the function is evaluated first and is ready to receive numerical values and produce numerical results for plotting.  Using Evaluate will correct most errors that say "not a machine-size real number".  If not, make sure that substitution of a number into your function does produce a valid numerical result — often one forgets to substitute numerical values for parameters in a function and symbols appear in the output.

Data

Next, suppose that we wish to plot a set of data {{(Subscript[x, i]), (Subscript[y, i])}, ⋯}.  First we create such a set by modifying a previous cell.  The comments were removed and Transpose was used to package the data as a list of pairs.  Also, now we use the Gaussian probability distribution that came with the statistics package to add a more normal set of fluctuations.  The parameters are the mean, here zero, and the standard deviation, σ, for the fluctuations.  The plot is made with ListPlot and an option is used to make the points large enough for aging eyes.

f[x_] = (x^2) Sin[x];
xdata = Range[-0.9, 0.9, 0.1];
ydata = 
        f[xdata] + 
          Table[Random[
              NormalDistribution[0. , 0.1]], {Length[
                xdata]}];
data = Transpose[{xdata, ydata}];

ListPlot[data, PlotStyle -> PointSize[0.02]];

Next, we want to plot the theoretical curve together with the data.

DisplayTogether[ListPlot[data, PlotStyle -> PointSize[0.02]], 
        Plot[f[x], {x, -1, 1}], PlotRange -> All];

The simplest way to add error bars to the data points is to use ErrorListPlot with data of the form {{(Subscript[x, i]), 
          (Subscript[y, i]), δ (Subscript[y, i])}, ⋯}.  Here we assume that δ (Subscript[y, i]) = σ = 
          0.1 for all points.

data3 = 
      Transpose[{xdata, ydata, Table[0.1, {Length[ydata]}]}]

DisplayTogether[ErrorListPlot[data3], Plot[f[x], {x, -1, 1}], 
        PlotRange -> All];

Sometime we need to plot several data sets with different symbols or with error bars in both variables, but the options offered by ErrorListPlot are quite limited.  A more versatile function for plotting data is MultipleListPlot[dataset1,dataset2,⋯, options] where each data set should have the form {{point,error},⋯} where point is an {x,y} pair and error is an ErrorBar object of the form ErrorBar[yerr] or ErrorBar[xerr,yerr].  Each error is either a single value representing a symmetric uncertainty or an interval about zero of the form {negerr,poserr} which can be used for asymmetric error bars.

To illustrate this function we create two data sets based upon the same function.  First we construct grids for the independent variable x and compute the predicted values for (Subscript[y, i]) = f[(Subscript[x, i])].  We then add random measurement errors based upon the normal distribution to both (Subscript[x, i]) and (Subscript[y, i]).  Different standard deviations are used for each data set.  Next, we package the data points and their two-dimensional error bars in the form required for MultipleListPlot.  Finally, we plot both data sets with different symbols and add the theoretical curve.

xdata1 = Range[-0.9, 0.9, 0.2];
xdata2 = Range[-0.8, 0.8, 0.2];

ydata1 = 
        f[xdata1] + 
          Table[Random[
              NormalDistribution[0, 0.1]], {Length[
                xdata1]}];
ydata2 = 
        f[xdata2] + 
          Table[Random[
              NormalDistribution[0, 0.05]], {Length[xdata2]}];

xdata1 = 
        xdata1  +  
          Table[Random[
              NormalDistribution[0, 0.03]], {Length[
                xdata1]}];
xdata2 = 
        xdata2  + 
          Table[Random[
              NormalDistribution[0, 0.01]], {Length[xdata2]}];

data1 = Transpose[{xdata1, ydata1}];
data2 = Transpose[{xdata2, ydata2}];

edata1 = 
        Table[ErrorBar[0.03, 
            0.1], {Length[xdata1]}];
edata2 = Table[ErrorBar[0.01, 0.05], {Length[xdata2]}];

set1 = Transpose[{data1, edata1}];
set2 = Transpose[{data2, edata2}];

DisplayTogether[
        MultipleListPlot[set1, set2, 
          SymbolShape -> {PlotSymbol[Triangle, 4], 
              PlotSymbol[Diamond, 4, Filled -> False]}, 
          SymbolStyle -> {RGBColor[0, 1, 0], RGBColor[1, 0, 0]}], 
        Plot[f[x], {x, -1, 1}], PlotRange -> All, 
        Frame -> True, 
        FrameLabel -> {"put xlabel here with units", "put ylabel \
here with units"}, PlotLabel -> "put plot label here", 
        GridLines -> Automatic];

Reading a data file

For the present purpose we assume that data are listed in a file that contains simply two or three columns of numbers.  The spacing between those columns does not matter and need not be uniform, but we assume that every line contains the same number of numerical entries.  Although there are methods in Mathematica for handling headers, comments, variations in file structure, and other variations, it is simpler for now to edit the data file so that it has the simplest possible structure.  The command Import[file] then reads the contents of file into a list where each member of the list is a list of the numbers on one line of file.  The file name is a string.  If you have not properly adjusted $Path, you will need to edit the following example to specify the correct file location for your system.  Once successful, the statement

gepdata = Import["gep.dat"];

reads a data for the electric form factor of the proton, (Subscript[G, Ep]), into the symbol gepdata.  Below we plot the data as a function of (Q^2), the square of the momentum transfer for scattering a high-energy electron from the internal charge distribution of the proton.  The charge density is related to the Fourier transform of the form factor.

\(\(ErrorListPlot[gepdata, Frame -> True, 
        FrameLabel -> {\*""(Q^2) [(GeV/c)^2]"", \ \*""(Subscript[G, Ep])""}, 
        PlotLabel -> "Proton Electric Form Factor"];\)\)

More detailed instructions on reading files with more general formats can be found in files.nb or in the Mathematica reference guide.

Fitting data

Mathematica provides several functions for fitting data, but here we discuss only NonlinearRegress because it is the most general and subsumes the other functions as special cases.  The basic syntax and list of options are given in the reference guide, obtained by following the highlighted link.  For use in this function, we provide data in the form {{(Subscript[x, 1]), (Subscript[y, 1])}, ⋯, {(Subscript[x, N]), (Subscript[y, N])}} and a separate list of weights \(TraditionalForm\`{σ\_1\^\(-2\), ⋯, σ\_N\^\(-2\)}\).  The fitting function is given explicitly, followed by a list of independent variables and a list of parameters.  The output is returned as a list of replacement rules.  Note that when the fitting function is a nonlinear function of the fitting parameters, it is often important to begin the search with reasonable initial guesses for those parameters.  Initial values are provided by giving the list of parameters in the form {{a, (Subscript[a, 0])}, {b, (Subscript[b, 0])}, ⋯} where a, b, ⋯ are the parameters and (Subscript[a, 0]), (Subscript[b, 0]), ⋯ are their initial values.

We illustrate this function by fitting the data for (Subscript[G, Ep])[(Q^2)] read in the previous section.  A simple parametrization is the rational function

\(TraditionalForm\`G\_Ep[
        Q\^2]\  = \ \(∑\+\(μ = 0\)\%m\( a\_μ\) Q\^\(2  μ\)\)\/\(∑\+\(ν = 0\)\%\(m + 2\)\(b\_ν\) Q\^\(2  ν\)\)\)

where the symmetry of the problem limits the expansions to even powers.  A rational function of this kind is sometimes called a Padé approximation.  Recognizing that (Subscript[G, Ep])[0] = 1 because the charge must be one elementary unit, we can constrain (Subscript[a, 0]) = (Subscript[b, 0]) = 1, but it may be useful to vary (Subscript[a, 0]) if the systematic normalization uncertainties are nonnegligible.  Finally, there are theoretical reasons based upon the quark model to require

\(TraditionalForm\`Q\^2 -> ∞\ \ ⟹\ \ G\_Ep ∝ Q\^\(-4\)\)

This is the reason that we include two more terms in the denominator than in the numerator.

The cells below perform such a fit.  First we define the fitting function and then we define a list of parameters and their initial values; note that the initial guesses do not have to be especially precise for this problem.  The uncertainties are then extracted from the input data and listability is used to specify the weights (Subscript[w, i]) = 1/((Subscript[σ, i])^2) succinctly.  The output of NonlinearRegress is a fairly detailed report containing the fitted parameters, their standard errors (SE), and other statistics in the form of replacement rules.

pade[
          Q2_] = ((1 + a[1] Q2)/(1 + b[1] Q2  +  b[2] (Q2^2) + b[3] (Q2^3)));
start = {{a[1], 0}, {b[1], 2}, {b[2], 0}, {b[3], 0}};

{x, y, dy} = Transpose[gepdata];
gepresults = 
      NonlinearRegress[Transpose[{x, y}], pade[Q2], Q2, start, 
        Weights -> (dy^(-2))]

Next we compare the fit with the data using the replacements rules to substitute the best-fit parameters into the fitting function.

\(\(DisplayTogether[
ErrorListPlot[gepdata, 
          Frame -> True, 
          FrameLabel -> {\*""(Q^2) [(GeV/c)^2]"", \ \*""(Subscript[G, Ep])""}, 
          PlotLabel -> "Proton Electric Form Factor"], 
Plot[
          Evaluate[pade[Q2] /. \((BestFitParameters /. gepresults)\)], {Q2, 
            0, 6}]
];\)\)

The theory of nonlinear least-squares is explained in a separate notebook, DataAnalysis.nb, which also develops and tests several data analysis programs.  Below we load the package containing those programs, request help for the syntax, and repeat the fit using Curfit.

Needs["Curfit`"]

?*fit*

gepresults2 = Curfit[gepdata, pade[Q2], Q2, start]

These results are equivalent to those from the built-in function, but the availability of a custom program offers us the opportunity to understand its algorithm in detail and to modify it as the need arises.  For example, the fitting functions provided by Mathematica do not permit uncertainties in the independent variable whereas Linfit and Curfit from this user package do.  On the other hand, NonlinearRegress provides more detailed statistical analyses and can handle data with several independent variables.