Files
Calculators-for-University-…/python/lenth.ipynb
T

140 lines
4.7 KiB
Plaintext
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.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 长度直接测量与不确定度评定\n",
"\n",
"本 Notebook 用于对多次长度直接测量数据进行 A 类不确定度、B 类不确定度及合成不确定度计算,并严格按照“四舍六入五凑偶”规则对测量结果进行科学修约与末位对齐。\n",
"\n",
"---\n",
"### 实验原理与数学公式\n",
"\n",
"#### 1. 算术平均值与样本标准差\n",
"对 $n$ 次独立等精度直接测量值 $x_1, x_2, \\dots, x_n$:\n",
"$$\\bar{x} = \\frac{1}{n} \\sum_{i=1}^n x_i$$\n",
"$$s = \\sqrt{\\frac{1}{n-1} \\sum_{i=1}^n (x_i - \\bar{x})^2}$$\n",
"\n",
"#### 2. A 类、B 类及合成不确定度\n",
"* **A 类不确定度**(由有限次测量随机效应引起):\n",
"$$u_A = \\frac{s}{\\sqrt{n}}$$\n",
"* **B 类不确定度**(由测量仪器极限误差 $\\Delta_{\\mathrm{inst}}$ 引起,假定均匀分布):\n",
"$$u_B = \\frac{\\Delta_{\\mathrm{inst}}}{\\sqrt{3}}$$\n",
"* **合成标准不确定度**:\n",
"$$u = \\sqrt{u_A^2 + u_B^2}$$\n",
"\n",
"#### 3. 修约与结果表示\n",
"* 不确定度 $u$ 按照“四舍六入五凑偶”原则保留一位有效数字。\n",
"* 测量平均值末位与不确定度的有效位数对齐。"
],
"id": "1ed33dfddc944ff0"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"from decimal import Decimal, ROUND_HALF_EVEN\n",
"from python.utils import get_decimal_places, scientific_round\n",
"\n",
"print(\"工具函数加载完成。\")"
],
"id": "709179017567e25d"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. 测量数据与仪器参数设置\n",
"> **提示**:直接在下方修改测量数据列表 `raw_data`(字符串或数值)与仪器分度值/允差 `res_str` 即可。"
],
"id": "6e9499ceb295ab05"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 测量数据列表 (字符串格式保留输入精度)\n",
"raw_data = [\"10.25\", \"10.24\", \"10.26\", \"10.25\", \"10.24\", \"10.25\"]\n",
"\n",
"# 仪器分度值 / 仪器误差限 Δ_inst\n",
"res_str = \"0.05\"\n",
"\n",
"print(f\"输入测量数据 (n={len(raw_data)}): {raw_data}\")\n",
"print(f\"仪器误差限 Δ_inst: {res_str}\")"
],
"id": "722fceaefcc9ac13"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. 不确定度计算与科学修约"
],
"id": "376599ba7264087b"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def calc_uncertainty(data_strs, res_str=\"0.05\"):\n",
" data = [Decimal(str(s)) for s in data_strs]\n",
" res = Decimal(str(res_str))\n",
" n = len(data)\n",
" \n",
" # 1. 原始精度平均值\n",
" places = get_decimal_places(data_strs)\n",
" sum_val = sum(data)\n",
" mean = sum_val / n\n",
" mean_final = mean.quantize(Decimal('1.' + '0' * places), rounding=ROUND_HALF_EVEN)\n",
" \n",
" # 2. 样本标准差 s\n",
" variance = sum((x - mean) ** 2 for x in data) / (n - 1) if n > 1 else Decimal(\"0\")\n",
" s = Decimal(str(math.sqrt(float(variance))))\n",
" \n",
" # 3. A 类不确定度\n",
" u_A = s / Decimal(str(math.sqrt(n))) if n > 1 else Decimal(\"0\")\n",
" \n",
" # 4. B 类不确定度\n",
" u_B = res / Decimal(str(math.sqrt(3)))\n",
" \n",
" # 5. 合成不确定度\n",
" u = Decimal(str(math.sqrt(float(u_A**2 + u_B**2))))\n",
" \n",
" return mean_final, mean, s, u_A, u_B, u\n",
"\n",
"mean_display, mean_raw, s, u_A, u_B, u = calc_uncertainty(raw_data, res_str)\n",
"mean_final, u_final = scientific_round(mean_raw, u)\n",
"\n",
"print(\"=\" * 40)\n",
"print(\" 长 度 测 量 不 确 定 度 分 析 \")\n",
"print(\"=\" * 40)\n",
"print(f\"测量次数 n : {len(raw_data)}\")\n",
"print(f\"算术平均值 : {mean_raw:.6f}\")\n",
"print(f\"样本标准差 s : {s:.6f}\")\n",
"print(f\"A 类不确定度 u_A : {u_A:.6f}\")\n",
"print(f\"B 类不确定度 u_B : {u_B:.6f}\")\n",
"print(f\"合成不确定度 u : {u:.6f}\")\n",
"print(\"-\" * 40)\n",
"print(f\"★ 最终修约结果 : L = {mean_final} ± {u_final}\")\n",
"print(\"=\" * 40)"
],
"id": "5fc60b2526bdc755"
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}