12
$\begingroup$

I have a problem with writing a Mathematica function. I have been requested to show the results of my experiments with the measurement uncertainty (mu) like below:

measurementValue(mu)  

Both numbers ought to be rounded with precision, given by the two most significant digits of mu.

Example:

16(11), 123.4(1.3), 1230(10), 123.345(40)||123.345(0.040)

Getting proper format of mu is simple:

NumberForm[mu, 2]

But I don't have any idea, how to round properly measurement value. Finally I want to write a function, that returns the proper format of the value and mu as output.

$\endgroup$
5
  • 2
    $\begingroup$ I'd suggest using some inert wrapper (I used UncertainNumber[val, err] once upon a time), and write up formatting rules so that it displays as val(err) on the front end. That is, look up the usual formatting function/boxes. $\endgroup$ Commented Jun 17, 2013 at 0:33
  • $\begingroup$ I am not sure what are you asking about. If it is the table or an array of numbers, what is wrong with the old, good plus-minus notation, like 16plusminus11? May be you need to show that on a Plot? In that case did you see ErrorListPlot and alike? $\endgroup$ Commented Jun 17, 2013 at 8:53
  • 1
    $\begingroup$ @AlexeiBoulbitch x+/-dx or x(dx) is only a matter of convention. Important thing here is to force x and dx forms looks like we want to. $\endgroup$
    – Kuba
    Commented Jun 17, 2013 at 12:14
  • $\begingroup$ There is no difference between both forms. The real problem is to get the same number of digits on the right of comma. We make: NumberForm[mu, 2] That sets proper format of mu. Now, we need to find how many digits is on the right of comma and set proper value formatting. $\endgroup$
    – Zabaweb
    Commented Jun 17, 2013 at 12:37
  • $\begingroup$ @user8082 NumberForm will not help You at least not in such simple form, look at NumberForm[.4, 2] NumberForm[123, 2] NumberForm[.4, {2,2}] NumberForm[.042, {2,2}]. $\endgroup$
    – Kuba
    Commented Jun 17, 2013 at 16:49

3 Answers 3

11
$\begingroup$

I have faced this problem earlier but failed attempts with simple operations based on NumberForm, Round.. have forced me to stop looking for general solution. I have thought my skills in MMA were too low, but also today I am not able to do this in simple way. (haven't I learned anything? :))

This form of expression uncertainty in measurement is described by ISO check § 7.2.2. (link provided by OleksandrR.)

Assumptions

Since we are dealing with some kind of convention it is good to point assumptions to avoid future discussions (here and after x-measurement, dx-uncertainty):

  • dx is taken with 3 most significant digits and rounded to 2, while x is only taken with as many digits as dx imply without rounding.
  • x > dx, or is at least the same order of magnitude as dx.
  • x is given with maximum 15 digits. (so we do not use 1234567891234567 for example)

Function:

I can not supress the feelling that it is an overkill but I wanted to do this.

f[x_, dx_] := Module[{d1, d2, Rdx, Rx},
  Rdx = {{#[[1]], Round[#[[2]] + .1 #[[3]] + .01]}~Join~
   Table[0, {#2 - 2}], #2} & @@ RealDigits[dx, 10, 3];
  Rx = Fold[#2 @@ #1 &,
    RealDigits[x, 10, 15],
    {
     {#1[[1 ;; Max[#2, -Rdx[[2]] + 2 + #2]]], #2} &,
     {Table[0, {-#2 + 1}]~Join~#1, If[#2 <= 0, 1 + Abs@#2, #2]} &
     }
    ];
  d1 = If[Length@#1 == #2, #1, Insert[#1, ".", #2 + 1]] & @@ Rx;
  d2 = If[#2 == 1, Insert[#1, ".", 2], #1] & @@ Rdx;
  Row[Join[d1, {"("}, d2, {")"}]]
  ]

Examples:

data = {{12345, 678}, {12345, 6.78}, {12345, .678}, {12345, .000678}, 
        {123231231321321, 123.12312}};
data3 = {#1/100, #2} & @@@ data;
exp = f @@@ # & /@ {data, data3};

Grid[{{"{x,dx}", "expr.", "{x,dx}", "expr."}}~Join~Transpose@Riffle[{data, data3},exp], 
    Dividers -> {{3 -> True}, {2 -> True}}, Alignment -> {",", Left}]

enter image description here

Function destription: ( later...*)

Extension: If no one show much shorter solution I'll extend this a little bit. For example one could expect:

f[0.00001123,0.000001]
1.12(10) 10^(-5)
$\endgroup$
13
  • 5
    $\begingroup$ For the full text of JCGM 100:2008(E) (GUM), see here. $\endgroup$ Commented Jun 20, 2013 at 13:20
  • $\begingroup$ @OleksandrR. I don't know if it is legal but thank You :) $\endgroup$
    – Kuba
    Commented Jun 20, 2013 at 13:22
  • $\begingroup$ Yes, it's legal--it's right from the ISO website and you can follow links to the same page from where you were. You only have to pay the 210 CHF if you want the printed (or printable) copy of the report. Not sure why the PDF is the same price as the printed copy. $\endgroup$ Commented Jun 20, 2013 at 13:23
  • 4
    $\begingroup$ Regarding your answer (+1, of course), I personally would not round the uncertainty to two places if you are going to present it with three digits. Although some people will complain about "too many significant figures", IMO this objection is rooted in incorrect ideas about uncertainty analysis and can be ignored. $\endgroup$ Commented Jun 20, 2013 at 13:34
  • 3
    $\begingroup$ I'm not sure; personally I just use PlusMinus inside Mathematica, since I don't use it for presentation (just for calculations). In some cases it may be desirable to have different positive- and negative-going errors, or to be more precise about the distribution of the measured values, so I think there isn't a single good approach, which may explain why people are happy to manually format their results for presentation after consciously making this decision. BTW, I noticed that all the GUM PDFs are also available from the BIPM site. $\endgroup$ Commented Jun 22, 2013 at 19:47
3
$\begingroup$

I solved my problem particular and wrote proper function:

f[value_, mu_, k_] :=
   Block[{n},
      n = Ceiling[n /. NSolve[mu == 10^n, n]][[1]];
      N[Round[{value, mu}, 10^(n - k)]]
   ]

Function returns two element list. The first element is proper round value, the 2nd is mu. The "k" call parameter determine how many first digits of mu is important.

$\endgroup$
2
  • 2
    $\begingroup$ Shorter: f[value_, mu_, k_] := N[Round[{value, mu}, 10^(Ceiling[Log10[mu]] - k)]]. $\endgroup$ Commented Jun 17, 2013 at 16:51
  • $\begingroup$ I assume f[123.12, .2, 2] should give us {123.12,.20} insted of {123.12,0.2}. $\endgroup$
    – Kuba
    Commented Jun 17, 2013 at 16:54
2
$\begingroup$
 f[x_, dx_, k_: 2] := Block[{y, dy, n, dn, estep, xi},
   n = RealDigits[x][[2]];
   dn = RealDigits[dx][[2]];
   estep = Abs[Max[n, dn]] + k;
   dy = If[dx >= 1, 
           NumberForm[N[dx], {k, If[-dn + k >= 0, -dn + k, 0]}, 
           ExponentStep -> estep], Round[dx 10^(-dn + k)]];
    Off[NumberForm::sigz, ScientificForm::sigz];

    xi = If[-dn + k > 0, 1, estep];
    y = NumberForm[
          N[x], {If[-dn + k >= 0, -dn + k + 2, k], 
    If[-dn + k >= 0, -dn + k, 0]}, 
        ExponentStep -> If[-dn + k > 0, 1, estep]];
      y = If[n + k > dn, y, 0];
    {y, dy}
     ];
$\endgroup$
1
  • $\begingroup$ Check f[1234567, 4567, 2]. Also it is switching to exponent form quite fast in cases like: f[1234567, .2]. $\endgroup$
    – Kuba
    Commented Jun 24, 2013 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.