Search code examples
pythonuncertainty

variable uncertainty corresponding to nominal value


I just startet using the uncertainties module found at http://pythonhosted.org/uncertainties/index.html#

Suppose we have an experimental setup with a temperature sensor which was calibrated. Now consider that the calibration yielded a variable measurement error, e.g. a linear increase from ± 1 K @ 0 °C to ± 4 K @ 100 °C. Is it possible to define a custom function to be used when defining a variable with the uncertainties module?

Example:

>>> from uncertainties import ufloat
>>> def err_fun(temp_in_C):
..:     return 1 + 3 / 100 * temp_in_C
>>> temp_meas = ufloat(10, err_fun, 'tag')
>>> print temp_meas
10+/-1.3

If so, does the uncertainty of the variable change, when its nominal value is changed?

Example:

>>> print temp_meas
10+/-1.3
>>> temp_meas.nominal_value = 50
>>> print temp_meas
50+/-2.5

Solution

  • The uncertainty can only be a real number. Thus, when you change the nominal value with temp_meas.nominal_value = 50, you only change the nominal value (not the uncertainty).

    The simplest solution in your case is probably to create a temperature with uncertainty on the fly:

    def temp_with_uncert(temp_in_C):
        return ufloat(temp_in_C, 1 + 0.03 * temp_in_C)
    

    gives:

    >>> temp_with_uncert(10)
    10.0+/-1.3
    >>> temp_with_uncert(50)
    50.0+/-2.5