最近大学物理实验开始了,其中计算不确定度着实让人头疼,这里花费三个小时写个代码计算一下不确定度,顺便开源了,代码如下:
import math
li = []
# 存储t因子
dict_t = {1: 0, 3: 1.32, 4: 1.2, 5: 1.14, 6: 1.11, 7: 1.09, 8: 1.08, 9: 1.07, 10: 1.06, 20: 1.03}
dic_M = {"铜圆柱": 50.24, "金属圆管": 134.82, "钢球的质量": 28.29}
dic_tong_h = [5.152, 5.154, 5.150, 5.152, 5.154]
dic_tong_q = [1.1991, 1.1992, 1.1991, 1.1999, 1.1991]
dict_Ub1 = {"游标卡尺": 0.0012, "千分尺": 0.00013, "米尺": 0.017, "单次测量米尺": 0.1, "天平": 0.007} # 单位cm
dic_jin_D = [5.452, 5.450, 5.452, 5.450, 5.454]
dic_jin_d = [3.014, 3.012, 3.010, 3.014, 3.014]
dic_jin_H = [2.982, 3.022, 2.988, 3.022, 2.986]
dic_gang_R = [1.9056, 1.9055, 1.9056, 1.9050, 1.9052]
aver_gang = (dic_gang_R[0] + dic_gang_R[1] + dic_gang_R[2] + dic_gang_R[3] + dic_gang_R[4]) / 5
aver_tong_d = (dic_tong_q[0] + dic_tong_q[1] + dic_tong_q[2] + dic_tong_q[3] + dic_tong_q[4]) / 5
aver_tong_h = (dic_tong_h[0] + dic_tong_h[1] + dic_tong_h[2] + dic_tong_h[3] + dic_tong_h[4]) / 5
aver_jin_D = (dic_jin_D[0] + dic_jin_D[1] + dic_jin_D[2] + dic_jin_D[3] + dic_jin_D[4]) / 5
aver_jin_d = (dic_jin_d[0] + dic_jin_d[1] + dic_jin_d[2] + dic_jin_d[3] + dic_jin_d[4]) / 5
aver_jin_H = (dic_jin_H[0] + dic_jin_H[1] + dic_jin_H[2] + dic_jin_H[3] + dic_jin_H[4]) / 5
def get_tong_midu(wuti, h, q):
return 4.0 * wuti / (3.14 * h * q * q)
def get_jin_midu(wuti1, D, d, H):
return abs(4.0 * wuti1 / (3.14 * H * (D * D - d * d)))
def get_gang_midu(wuti2, R):
return 6 * wuti2 / (3.14 * math.pow(R, 3))
class buquedingdu:
def __init__(self, n):
self.n = n # 数据个数
self.t = dict_t[self.n]
self.Ua = 0
self.average = 0
def get_average(self):
"""
:return: 返回算数平均值
"""
num = self.n
sum_n = 0
while num:
shuju = float(input("请输入你的数据:"))
li.append(shuju)
sum_n += shuju
pass
num -= 1
print("算数平均值为{}".format(sum_n / n))
self.average = sum_n / n
return self.average
def get_std_cha(self): # 计算标准差
average = self.get_average() # 计算算数平均值
get_sum = 0
for i in range(self.n):
get_sum += math.pow(li[i] - average, 2)
Sx = math.sqrt(get_sum / (self.n - 1))
print("标准差为:{}".format(Sx))
return Sx
def get_std_piancha(self): # 计算标准偏差
self.Ua = self.t * self.get_std_cha() / math.sqrt(self.n)
print("标准偏差为(A类不确定度,取两位有效数字):{}".format(self.Ua))
return self.Ua
def get_Uc(self):
Uc = math.sqrt(math.pow(self.Ua, 2) + math.pow(dict_Ub1[yiqi], 2))
print("C类不确定度为:{}(取一位有效数字,非0即进)".format(Uc))
print(f"测量结果为:\n{self.average}+-{Uc}", "Er相对不确定度为:{}%(取两位有效数字)".format(Uc / self.average * 100))
return Uc
def get_once_Uc(self):
Uc = math.sqrt(math.pow(dict_Ub1["米尺"], 2) + math.pow(dict_Ub1["单次测量米尺"], 2))
print("C类不确定度为:{}(取一位有效数字,非0即进)".format(Uc))
print(f"测量结果为:\n{self.average}+-{Uc}", "Er相对不确定度为:{}%(取两位有效数字)".format(Uc / self.average * 100))
return Uc
def get_gang_buqueding(self):
Um = float(input("请输入质量差值:"))
Ud = float(input("请输入直径差值"))
Er = math.sqrt(math.pow(Um / dic_M["钢球的质量"], 2) + 9 * math.pow(Ud / aver_gang, 2))
Uc = get_gang_midu(dic_M["钢球的质量"], aver_gang) * Er
print("钢球Uc:{}".format(Uc))
print("钢球Er相对不确定度为:{}".format(Er))
def get_tong_buqueding(self):
Um = float(input("请输入质量差值:"))
Ud = float(input("请输入直径差值"))
Uh = float(input("请输入高度差值"))
Er = math.sqrt(
math.pow(Um / dic_M["铜圆柱"], 2) + 4 * math.pow(Ud / aver_tong_d, 2) + math.pow(Uh / aver_tong_h, 2))
Uc = Er * get_tong_midu(dic_M["铜圆柱"], aver_tong_h, aver_tong_d)
print("铜圆柱Uc:{}".format(Uc))
print("铜圆柱Er相对不确定度为:{}".format(Er))
def get_yuanhuan_buqueding(self):
Um = float(input("请输入质量差值:"))
UD = float(input("请输入大径差值:"))
Ud = float(input("请输入直径差值:"))
Uh = float(input("请输入高度差值:"))
Er = math.sqrt(math.pow(Um / dic_M["金属圆管"], 2) + 4 * math.pow(
aver_jin_D * UD / (aver_jin_D * aver_jin_D - aver_jin_d * aver_jin_d), 2) + 4 * math.pow(
aver_jin_d * Ud / (aver_jin_D * aver_jin_D - aver_jin_d * aver_jin_d), 2) + Uh / aver_jin_H)
Uc = get_jin_midu(dic_M["金属圆管"], aver_jin_D, aver_jin_d, aver_jin_H) * Er
print("圆环Uc:{}".format(Uc))
print("圆环Er相对不确定度为:{}".format(Er))
if __name__ == '__main__':
try:
dic_M["铜圆柱"] = float(input("请输入铜圆柱质量:"))
for i in range(5):
dic_tong_h[i] = float(input(f"请输入铜的高度h,还剩下{4-i}次:"))
dic_tong_q[i] = float(input(f"请输入铜的直径d,还剩下{4-i}次:"))
dic_M["金属圆管"] = float(input("请输入金属圆管质量:"))
for i in range(5):
dic_jin_D[i] = float(input(f"请输入金属圆环的大径D,还剩下{4-i}次"))
dic_jin_d[i] = float(input(f"请输入金属圆环的小径d,还剩下{4-i}次"))
dic_jin_H[i] = float(input(f"请输入金属圆环的高H,还剩下{4-i}次:"))
dic_M["钢球的质量"] = float(input("请输入钢球的质量:"))
for i in range(5):
dic_gang_R[i] = float(input(f"请输入钢球的半径R,还剩下{4-i}:"))
aver_gang = (dic_gang_R[0] + dic_gang_R[1] + dic_gang_R[2] + dic_gang_R[3] + dic_gang_R[4]) / 5
aver_tong_d = (dic_tong_q[0] + dic_tong_q[1] + dic_tong_q[2] + dic_tong_q[3] + dic_tong_q[4]) / 5
aver_tong_h = (dic_tong_h[0] + dic_tong_h[1] + dic_tong_h[2] + dic_tong_h[3] + dic_tong_h[4]) / 5
aver_jin_D = (dic_jin_D[0] + dic_jin_D[1] + dic_jin_D[2] + dic_jin_D[3] + dic_jin_D[4]) / 5
aver_jin_d = (dic_jin_d[0] + dic_jin_d[1] + dic_jin_d[2] + dic_jin_d[3] + dic_jin_d[4]) / 5
aver_jin_H = (dic_jin_H[0] + dic_jin_H[1] + dic_jin_H[2] + dic_jin_H[3] + dic_jin_H[4]) / 5
num = input("输入1获取圆柱体、金属圆管、钢球的密度")
if num == "1":
print("铜圆柱密度为{}:".format(get_tong_midu(dic_M["铜圆柱"], aver_tong_h, aver_tong_d)),
"金属圆环密度为:{}".format(get_jin_midu(dic_M["金属圆管"], aver_jin_D, aver_jin_d, aver_jin_H)),
"钢球密度为:{}".format(get_gang_midu(dic_M["钢球的质量"], aver_gang)))
except Exception as erro_msg:
print(erro_msg)
while True:
try:
yiqi = input("请输入你所测量的仪器:")
n = int(input("请输入需要的输入的数据个数(单次测量请输入1,多次测量需输入大于3的数):"))
U = buquedingdu(n)
BU=input("输入1获取钢球、金属圆环、铜圆柱不确定度、输入2获取多次测量计算的不确定度")
if BU=="1":
U.get_tong_buqueding()
U.get_yuanhuan_buqueding()
U.get_gang_buqueding()
if BU=="2":
if n >= 3:
U.get_std_piancha()
U.get_Uc()
elif n == 1:
U.get_average()
U.get_once_Uc()
except Exception as erro_msg:
print(erro_msg)
exit1=input("输入exit或0退出")
if exit1=='0':
break
话不多说了,没有安装python的童鞋们,这里给你们提供了这个代码的exe可执行文件,打开就可以用啦。
链接:https://pan.baidu.com/s/19stkoQRvytlh4dunzmFctA 提取码:iwyj
欢迎大佬指正代码