对于直接测量量的A类不确定度求解
total_1 = 0
total_2 = 0
def f(x=[], y=[]):
global total_1, total_2
total_1 = 0
total_2 = 0
for i in range(len(x)):
total_1 += x[i]
for i in range(len(y)):
total_2 += y[i]
return total_1 / len(x), total_2 / len(y)
def g(x=[], y=[]):
mean_x, mean_y = f(x, y)
sum_of_squares_x = 0
sum_of_squares_y = 0
for i in range(len(x)):
deviation_x = x[i] - mean_x
print(f"第{i + 1}个数据的均差为:{deviation_x}")
sum_of_squares_x += deviation_x ** 2
for i in range(len(y)):
deviation_y = y[i] - mean_y
print(f"第{i + 1}个数据的均差为:{deviation_y}")
sum_of_squares_y += deviation_y ** 2
print(f"列表 x 的均差平方和为: {sum_of_squares_x}")
print(f"列表 y 的均差平方和为: {sum_of_squares_y}")
n_x = len(x)
n_y = len(y)
if n_x > 1:
u_a_x = (sum_of_squares_x / (n_x * (n_x - 1))) ** 0.5
print(f"列表 x 的A类不确定度为: {u_a_x}")
else:
print("列表 x 数据量不足,无法计算A类不确定度。")
if n_y > 1:
u_a_y = (sum_of_squares_y / (n_y * (n_y - 1))) ** 0.5
print(f"列表 y 的A类不确定度为: {u_a_y}")
else:
print("列表 y 数据量不足,无法计算A类不确定度。")
x = [9.604, 9.601, 9.600, 9.604]
y = [0.821, 0.824, 0.819, 0.823]
result_1, result_2 = f(x, y)
print(f"列表 x 的元素均值为: {result_1}")
print(f"列表 y 的元素均值为: {result_2}")
g(x, y)
传递不确定度的计算
import math
def calculate_value_and_uncertainty(a, b, c, d, da, db, dc, dd):
value = (a * b) / (c * d)
relative_uncertainty_squared = (da / a) ** 2 + (db / b) ** 2 + (dc / c) ** 2 + (dd / d) ** 2
uncertainty = value * math.sqrt(relative_uncertainty_squared)
return value, uncertainty
a = 9.602
b = 0.822
c = 589.3
d = 15
da = 0.097
db = 0.003
dc = 0.1
dd = 0.2
value, uncertainty = calculate_value_and_uncertainty(a, b, c, d, da, db, dc, dd)
print(f"函数值: {value}")
print(f"传递不确定度: {uncertainty}")