物理の駅 Physics station by 現役研究者

テクノロジーは共有されてこそ栄える

How to use uncertainties to automatically propagate the error.

uncertainties をpipでインストールする

pip install uncertainties

誤差付きの小数の定義はufloat である。

>>> from uncertainties import ufloat

>>> a=ufloat(10,1)
>>> b=ufloat(3,0.1)

>>> a+b
13.0+/-1.004987562112089

>>> a-b
7.0+/-1.004987562112089

>>> a*b
30.0+/-3.1622776601683795

>>> a/b
3.3333333333333335+/-0.35136418446315326

>>> a**b
1000.0+/-378.1785042870416

もう少し複雑な計算

>>> from uncertainties import ufloat
>>> from uncertainties.umath import sin, cos, tan, sqrt, fabs, acos, atan2

>>> rad=ufloat(0.2,0.01)

>>> sin(rad)
0.19866933079506122+/-0.009800665778412416

>>> cos(rad)
0.9800665778412416+/-0.0019866933079506124

>>> tan(rad)
0.2027100355086725+/-0.010410913584959275

>>> sqrt(rad)
0.4472135954999579+/-0.011180339887498949

>>> c=cos(rad)
>>> c
0.9800665778412416+/-0.0019866933079506124
>>> acos(c)
0.2+/-0.010000000000000002

>>> t=tan(rad)
>>> t
0.2027100355086725+/-0.010410913584959275
>>> atan2(t,1)
0.2+/-0.01

最確値と誤差を得る

t.nominal_value
t.n

t.std_dev
t.s