SyntaxHighlighter

4 March 2013

Formulating problems (3)

Object-oriented programming and systematic errors

In the previous post (March 3, 2013), I showed how to use GTC when both random and systematic errors contribute to the overall measurement uncertainty.

The key is that an estimate of each measurement error in the model must be associated with one, and only one, uncertain number.

In the last post, we saw that the idea of matching one uncertain number to one error estimate offers a straightforward way of using GTC. However, it becomes hard to keep track of all the different uncertain numbers as problems become more complicated.

In this post, I will show how to define a class of objects to represent the voltmeter used earlier. This class of objects will encapsulate the data and data processing needed by the voltmeter error model, making data processing much more straightforward.

A simple voltmeter

An equation for the meter reading \(x_\mathrm{dis} \) in terms of the applied voltage \( V \) and instrument errors is
\[
x_\mathrm{dis} = (1 + e_\mathrm{gain} + e_\mathrm{ran}) V + e_\mathrm{zero}
\]
where, \( e_\mathrm{gain} \) is the residual error in the gain setting, \( e_\mathrm{zero} \) is the residual error in the zero offset and \( e_\mathrm{ran}\) is an error due to system noise.

To carry out data processing, we invert this equation and replace the errors by their estimates (indicated by the hat symbol). In this way we can estimate the applied voltage given a reading
\[
\widehat{V} = \frac{x_\mathrm{dis} - \widehat{e}_\mathrm{zero}}{1 + \widehat{e}_\mathrm{gain} + \widehat{e}_\mathrm{ran}}
\]
Remember, \( e_\mathrm{gain} \) and \( e_\mathrm{zero} \) are systematic errors: they endure from one measurement to the next. Whereas \( e_\mathrm{ran} \) changes at every meter reading. So we usually estimate \( e_\mathrm{gain} \) and \( e_\mathrm{zero} \) just once, but need a fresh estimate of \( e_\mathrm{ran} \) for every reading.

I'll now define a Python class to implement the required uncertain-number data processing.

A voltmeter class

In object-oriented languages like Python, the structure and behaviour of new types of objects can be defined by a 'class'. An object of a given class can have data (that other objects of the same class do not share) and functions that implement certain behaviour (shared among all objects of the same class).

When objects of the Meter class are created, their data is initialised by the __init__ function. We see here that uncertain numbers for the residual gain and zero setting errors are created during initialisation. The standard uncertainty associated with random noise is also saved.

The voltage function uses the object-data to return an uncertain number for an applied voltage estimate. When voltage is called, an uncertain number associated with the random error is created first. This is combined with the uncertain numbers already defined to estimate the applied voltage.

Resistance measurement with one meter

As in the previous post, I will calculate an estimate of the unknown resistance \( R_\mathrm{x} \) from measurements of the voltage ratio \( V_\mathrm{x} / V_\mathrm{s} \) and an estimate of \( R_\mathrm{s} \)

To do this, I create an object of the Meter class by supplying numbers for the standard uncertainty parameters: u_e_gain, u_e_zero and u_e_ran:

m = Meter(u_e_gain=3E-6,u_e_zero=1E-6,u_e_ran=1E-7)

Then use the meter object to obtain a pair of uncertain numbers for the voltage readings

Vs = m.voltage(5.0100,tag='Vs')
Vx = m.voltage(4.9885,tag='Vx')

The code to calculate the unknown resistance and display the results (apart from the Meter class definition) now looks like this

This simple and intuitive behaviour is due to the way that uncertain numbers are created during object initialisation. When one Meter object is initialsed, a pair of uncertain numbers are created; but when two objects are initialsed, each has its own pair of uncertain numbers representing independent estimates of the different gain and zero setting errors in each meter.

Hence the definition of a Meter class is able to capture the creation of uncertain numbers we need in a natural and intuitive way that matches the model of the measurement system.