# Automatic Error Propagation With Mathematica

There is an "Application Library" for Mathematica called Experimental Data Analyst (EDA). It offers a number of tools for the analysis of experimental data. This document discusses one set of tools in EDA which provide the ability to automatically propagate experimental errors.

First we will need to load the EDA library. In the following command the In:= is the prompt from Mathematica; you will not type that part. Also, in the command there are no apostrophes ', but two graves `.

In:= Needs["EDA`"]

## Simple Calculations

In order to alert Mathematica to the fact that we want to do calculations including error propagation, we need to wrap the value and the error in a Datum construct:

In:= x = Datum[ {1.51, 0.03} ]
Out= Datum[ {1.51, 0.03} ]

This tells Mathematica to interpret x as a variable with value 1.51 and error 0.03. Mathematica has echoed the value of x back to the screen with Out= Datum[ {1.51, 0.03} ].

We similarly define a variable y:

In:= y = Datum[ {3.75, 0.08} ]
Out= Datum[ {3.75, 0.08} ]

Now we can combine x and y using all the standard arithmetic operations. For example:

In:= x + y
Out= Datum[{5.26, 0.085}]

Thus (1.51 ± 0.03) + (3.75 ± 0.08) is (5.260 ± 0.085), which you can easily verify by hand.

Similarly:

In:= x - y
Out= Datum[{-2.24, 0.085}]

In:= x * y
Out = Datum[{5.66, 0.17}]

In:= x / y
Out= Datum[{0.403, 0.012}]

In Mathematica a^b gives a to the power b. This case is handled too:

In:= x^3
Out= Datum[{3.44, 0.21}]

Sqrt is the square root operator:

In:= Sqrt[x]
Out= Datum[{1.229, 0.012}]

You can also use virtually every transcendental function, such as Sin (sine), Cos (cosine), Log (natural logarithm), etc.

In:= Log[x]
Out= Datum[{0.412, 0.02}]

Say you have a number of different value,error pairs that you wish to combine. The Data construct handles this case. In the examples below we will "recycle" the variables x and y. You should also notice that in the definition of x and y the value,error pairs are surrounded by curly braces {}, and the whole list of these is also surrounded by another pair of curly braces.

In:= x = Data[ {{17.3, .4}, {0.034, 0.0005}, {123, 9}} ]
Out= Data[{{17.3, 0.4}, {0.034, 0.0005}, {123, 9}}]

In:= y = Data[ {{23.5, .7}, {0.123, 0.0014}, {412, 15}} ]
Out= Data[{{23.5, 0.7}, {0.123, 0.0014}, {412, 15}}]

In:= x + y
Out= Data[{{40.8, 0.81}, {0.157, 0.0015}, {535., 17.}}]

In:= x * y
Out= Data[{{407., 15.}, {0.004182, 0.000078}, {50700., 4100.}}]

Data can handle all the cases that Datum can.

## Complex Calculations

Although the Datum and Data constructs work well for simple calculations, imagine you are attempting something more complex such as:

2 x^2 / z - y z / x

As discussed in the First Year Physics Laboratory Manual, such a combination requires that one takes the partial derivatives of the expression with respect to x, y and z, multiply each derivative by the corresponding error in the variable, and combine these terms in quadrature, i.e. the square root of the sum of the squares. The Datum and Data constructs are fairly clever, but not clever enough to do this.

However, EDA supplies a routine CombineWithError that does all the steps described above. For example:

In:= x = {{17.3, .4}, {0.034, 0.0005}, {123, 9}}
Out= {{17.3, 0.4}, {0.034, 0.0005}, {123, 9}}

In:= y = {{23.5, .7}, {0.123, 0.0014}, {412, 15}}
Out= {{23.5, 0.7}, {0.123, 0.0014}, {412, 15}}

In:= z = {{11.3, .8}, {.345, .023}, {75, 11}}
Out= {{11.3, 0.8}, {0.345, 0.023}, {75, 11}}

In:= CombineWithError[ 2 x^2 / z - y z / x ]
Out= {{37.6, 6.1}, {-1.24, 0.12}, {150., 130.}}

Of course, CombineWithError can handle simple cases too. Here we repeat the first calculation we did above in the Simple Calculations section, which was adding 1.51 ± 0.03 to 3.75 ± 0.08:

In:= x = {1.51, 0.03}
Out= {1.51, 0.03}

In:= y = {3.75, 0.08}
Out= {3.75, 0.08}

In:= CombineWithError[x + y]
Out= {5.26, 0.085}

There is one small caveat in using this routine. We can use the Datum and Data constructs directly without defining variables such as x and y:

In:= Datum[{1.51, 0.03}] + Datum[{3.75, 0.08}]
Out= Datum[{5.26, 0.085}]

CombineWithError must contain the actual symbols so that it knows how to take the partial derivatives of the expression. Thus the following command generates an error message and fails:

In:= CombineWithError[ {1.51, 0.03} + {3.75, 0.08} ]
CombineWithError::unequallength: The number of datapoints in the variables are not all equal or an unexpected specification of variable names was encountered.
Out= \$Failed

You may wish to know that EDA is a commercial application, marketed by Wolfram Research Inc., the inventor and vendor of Mathematica. It was written by David Harrison, Department of Physics, University of Toronto, in 1995-1996. This brief introduction to using EDA for propagation of errors was written by David Harrison, September 1998.