|
做物理实验后会有一大堆数据要处理,又要计算a类不确定度,b类不确定度,合成不确定度一堆数据,就写了一段简单的python代码和大家分享学习一下
代码:
def phsaver(a):
m = len(a)
for i in range(m):
sum = 0
for num in a:
sum = sum + num
aver=(sum/len(a))
return aver
#physics undecided 求A类不确定度
def phsunde(a):
m = len(a)
ud = []
for i in range(m):
ud.append(0)
for j in range(m):
ave = phsaver(a)
sum = 0
dim = len(a[j])
for num in a[j]:
sum = sum + (num - ave)**2
if dim ==3:t = 1.32
elif dim ==4:
t = 1.20
elif dim ==5:
t = 1.14
elif dim ==6:
t = 1.11
elif dim ==7:
t = 1.09
elif dim ==8:
t = 1.08
elif dim ==9:
t = 1.07
else:
t=1
unde = ((sum/(dim*(dim - 1)))**0.5)*t
return unde
if __name__ == "__main__":
a = input('请输入要计算的表格,同行用,(英文逗号)分隔,换行用;(英文分号)分隔:\n')
a = a.split(";")
s = len(a)
for i in range(s):
a = a.split(',')
n = len(a)
for j in range(n):
a[j] = eval(a[j])
A = phsunde(a)
aver = phsaver(a)
b = input('请输入B类不确定度(查表得)')
c = (float(A)*float(A)+float(b)*float(b))**(0.5)
Er = (float(c)/float(aver))*100
print("这组数据的平均值为", aver)
print("这组数据的A类不确定度为uA=", A)
print("这组数据的合成不确定度为uC=", c, "取一位数字,进位法")
print("相对不确定度Er=", Er, "%,取两位数字,采用进位法")
input()
|
|