physics/src/uncertainly.py
2025-09-18 19:35:03 +08:00

36 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from math import sqrt
class Uncertainly:
__slots__ = [
"average",
"length",
"standard_deviation",
"type_A_uncertain",
"type_B_uncertain",
"total_uncertain",
"data"
]
def __init__(self, data: tuple, type_B_uncertain: float, param_tp: float):
self.data = data
self.average:float = sum(self.data) / len(self.data)
self.length = len(self.data)
# def cal_standard_deviation(self):
data_cut = [(datum-self.average) ** 2 for datum in self.data]
self.standard_deviation:float = sqrt(sum(data_cut) / (self.length - 1))
self.type_A_uncertain:float = param_tp * self.standard_deviation / sqrt(self.length)
self.total_uncertain = 2 * sqrt(self.type_A_uncertain**2 + type_B_uncertain**2)
self.type_B_uncertain = type_B_uncertain
def output_std(self):
print(f"平均为:{self.data}/{self.length}={self.average}\n")
# print(f"{self.average}\n")
print(f"标准差:{round(self.standard_deviation,4)}")
print(f"3S区间[{round(self.average - 3 * self.standard_deviation,4)},{round(self.average +3 * self.standard_deviation,4)}]")
print(f"B类不确定度确认{self.type_B_uncertain}\n")
print(f"总不确定度2 * ✓{round(self.type_A_uncertain,4)}^2 + {round(self.type_B_uncertain,4)}^2={round(self.total_uncertain,4)}")
print(f"结果表达式为:{round(self.average,4)}+-{round(self.total_uncertain,4)}")