From 664c80166be9029f2788f503eb9a233e94775ae3 Mon Sep 17 00:00:00 2001 From: SOR <3272194354@qq.com> Date: Tue, 22 Sep 2026 16:04:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E6=89=80=E6=9C=89=E5=A4=A7=E7=89=A91?= =?UTF-8?q?=E7=9A=84py=E9=85=8D=E7=BD=AEipynb=EF=BC=8C=E4=BD=86=E6=98=AF?= =?UTF-8?q?=E5=9D=87=E6=9C=AA=E7=BB=8F=E8=BF=87=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/Bridge resistance measurement.ipynb | 187 +++++++++++ .../Bridge resistance measurement.py | 0 python/Hall effect.ipynb | 210 +++++++++++++ Hall effect.py => python/Hall effect.py | 0 python/Newton's rings.ipynb | 161 ++++++++++ Newton's rings.py => python/Newton's rings.py | 0 python/Speed of sound.ipynb | 158 ++++++++++ Speed of sound.py => python/Speed of sound.py | 0 python/Surface tension coefficient.ipynb | 190 ++++++++++++ .../Surface tension coefficient.py | 0 python/Thermal conductivity.ipynb | 216 +++++++++++++ .../Thermal conductivity.py | 0 python/Viscosity coefficient.ipynb | 291 ++++++++++++++++++ .../Viscosity coefficient.py | 0 python/Young's modulus.ipynb | 201 ++++++++++++ .../Young's modulus.py | 0 python/degree.ipynb | 137 +++++++++ degree.py => python/degree.py | 0 python/lenth.ipynb | 139 +++++++++ lenth.py => python/lenth.py | 0 {utils => python/utils}/__init__.py | 6 +- {utils => python/utils}/formatting.py | 0 {utils => python/utils}/io.py | 0 {utils => python/utils}/stats.py | 0 24 files changed, 1893 insertions(+), 3 deletions(-) create mode 100644 python/Bridge resistance measurement.ipynb rename Bridge resistance measurement.py => python/Bridge resistance measurement.py (100%) create mode 100644 python/Hall effect.ipynb rename Hall effect.py => python/Hall effect.py (100%) create mode 100644 python/Newton's rings.ipynb rename Newton's rings.py => python/Newton's rings.py (100%) create mode 100644 python/Speed of sound.ipynb rename Speed of sound.py => python/Speed of sound.py (100%) create mode 100644 python/Surface tension coefficient.ipynb rename Surface tension coefficient.py => python/Surface tension coefficient.py (100%) create mode 100644 python/Thermal conductivity.ipynb rename Thermal conductivity.py => python/Thermal conductivity.py (100%) create mode 100644 python/Viscosity coefficient.ipynb rename Viscosity coefficient.py => python/Viscosity coefficient.py (100%) create mode 100644 python/Young's modulus.ipynb rename Young's modulus.py => python/Young's modulus.py (100%) create mode 100644 python/degree.ipynb rename degree.py => python/degree.py (100%) create mode 100644 python/lenth.ipynb rename lenth.py => python/lenth.py (100%) rename {utils => python/utils}/__init__.py (68%) rename {utils => python/utils}/formatting.py (100%) rename {utils => python/utils}/io.py (100%) rename {utils => python/utils}/stats.py (100%) diff --git a/python/Bridge resistance measurement.ipynb b/python/Bridge resistance measurement.ipynb new file mode 100644 index 0000000..3ed4df2 --- /dev/null +++ b/python/Bridge resistance measurement.ipynb @@ -0,0 +1,187 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 电桥法测量电阻计算器 (Wheatstone Bridge)\n", + "\n", + "本 Notebook 用于通过**惠斯通电桥(单臂电桥)**测量未知电阻值 $R_x$。包含不同比例臂($1:1, 1:10, 10:1$ 等)下的平衡电阻统计、电桥灵敏度 $S$ 测定、仪器误差限 $\\Delta_{\\mathrm{inst}}$ 评估、不确定度合成及各比例臂对比汇总。\n", + "\n", + "---\n", + "### 实验原理与数学公式\n", + "\n", + "#### 1. 惠斯通电桥平衡条件\n", + "当检流计指针示数为零时,电桥达到平衡:\n", + "$$R_x = R_0 \\cdot \\frac{R_1}{R_2} = R_0 \\cdot C$$\n", + "* $R_x$:待测电阻\n", + "* $R_0$:比较臂标准电阻箱读数\n", + "* $C = R_1 / R_2$:比例臂倍率(如 $1, 0.1, 10$)\n", + "\n", + "#### 2. 电桥灵敏度 $S$ 与仪器误差限 $\\Delta_{\\mathrm{inst}}$\n", + "平衡后微调比较臂电阻使检流计偏转 $\\Delta n$ 格(通常取 5 格),记偏调后的读数为 $R_0'$:\n", + "$$\\Delta R_0 = |R_0' - R_0|, \\quad S = \\frac{\\Delta n}{\\Delta R_0 / R_0} = \\frac{\\Delta n \\cdot R_0}{\\Delta R_0}$$\n", + "电桥所能分辨的最小电阻改变量(即由灵敏度决定的仪器误差限)为:\n", + "$$\\Delta_{\\mathrm{inst}} = \\frac{R_0}{S} = \\frac{\\Delta R_0}{\\Delta n}$$\n", + "\n", + "#### 3. 不确定度传递与合成公式\n", + "比例臂 $C$ 由精密标准电阻构成,其不确定度可忽略,则:\n", + "* $R_0$ 的合成不确定度:\n", + "$$u(R_0) = \\sqrt{ u_A^2(R_0) + u_B^2(R_0) }, \\quad u_A(R_0) = \\frac{s(R_0)}{\\sqrt{n}}, \\quad u_B(R_0) = \\frac{\\Delta_{\\mathrm{inst}}}{\\sqrt{3}}$$\n", + "* 待测电阻 $R_x$ 的不确定度:\n", + "$$u(R_x) = C \\cdot u(R_0), \\quad u_r(R_x) = \\frac{u(R_x)}{R_x} = \\frac{u(R_0)}{R_0}$$\n", + "* 结果按照“四舍六入五凑偶”保留一位有效数字,末位对齐。" + ], + "id": "aae827c35ef68a9f" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "from decimal import Decimal\n", + "from python.utils import scientific_round, calculate_stats\n", + "\n", + "print(\"电桥法计算模块加载完成。\")" + ], + "id": "c7d75a2788fc0cac" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1. 测量实验数据配置 (支持多组标称值与比例臂)\n", + "> **提示**:可以在 `calibration_data` 列表中增减测试组。每组包含:标称值、比例臂标签、比例因子 $C$、5次平衡读数 $R_0$ 列表,以及偏转 5 格时的读数 $R_0'$。" + ], + "id": "44f6b780972c58ad" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 实验测量数据配置列表\n", + "# 结构: dict(R_nominal, ratio_label, C, R0_list, R0_prime)\n", + "calibration_data = [\n", + " {\n", + " \"R_nominal\": \"10000\", \"ratio_label\": \"1:1\", \"C\": Decimal(\"1\"),\n", + " \"R0_list\": [Decimal(\"9985.2\"), Decimal(\"9985.0\"), Decimal(\"9985.3\"), Decimal(\"9985.1\"), Decimal(\"9985.2\")],\n", + " \"R0_prime\": Decimal(\"9995.2\")\n", + " },\n", + " {\n", + " \"R_nominal\": \"10000\", \"ratio_label\": \"10:1\", \"C\": Decimal(\"10\"),\n", + " \"R0_list\": [Decimal(\"998.5\"), Decimal(\"998.4\"), Decimal(\"998.6\"), Decimal(\"998.5\"), Decimal(\"998.5\")],\n", + " \"R0_prime\": Decimal(\"1002.5\")\n", + " },\n", + " {\n", + " \"R_nominal\": \"2000\", \"ratio_label\": \"1:1\", \"C\": Decimal(\"1\"),\n", + " \"R0_list\": [Decimal(\"1996.8\"), Decimal(\"1996.9\"), Decimal(\"1996.7\"), Decimal(\"1996.8\"), Decimal(\"1996.8\")],\n", + " \"R0_prime\": Decimal(\"2002.8\")\n", + " },\n", + " {\n", + " \"R_nominal\": \"2000\", \"ratio_label\": \"10:1\", \"C\": Decimal(\"10\"),\n", + " \"R0_list\": [Decimal(\"199.7\"), Decimal(\"199.6\"), Decimal(\"199.7\"), Decimal(\"199.8\"), Decimal(\"199.7\")],\n", + " \"R0_prime\": Decimal(\"201.2\")\n", + " },\n", + " {\n", + " \"R_nominal\": \"200\", \"ratio_label\": \"1:1\", \"C\": Decimal(\"1\"),\n", + " \"R0_list\": [Decimal(\"199.65\"), Decimal(\"199.62\"), Decimal(\"199.68\"), Decimal(\"199.64\"), Decimal(\"199.65\")],\n", + " \"R0_prime\": Decimal(\"201.15\")\n", + " },\n", + " {\n", + " \"R_nominal\": \"200\", \"ratio_label\": \"1:10\", \"C\": Decimal(\"0.1\"),\n", + " \"R0_list\": [Decimal(\"1996.4\"), Decimal(\"1996.2\"), Decimal(\"1996.5\"), Decimal(\"1996.4\"), Decimal(\"1996.3\")],\n", + " \"R0_prime\": Decimal(\"2005.4\")\n", + " },\n", + " {\n", + " \"R_nominal\": \"200\", \"ratio_label\": \"10:1\", \"C\": Decimal(\"10\"),\n", + " \"R0_list\": [Decimal(\"19.96\"), Decimal(\"19.95\"), Decimal(\"19.97\"), Decimal(\"19.96\"), Decimal(\"19.96\")],\n", + " \"R0_prime\": Decimal(\"20.36\")\n", + " }\n", + "]\n", + "\n", + "print(f\"成功载入 {len(calibration_data)} 组电桥测量方案。\")" + ], + "id": "c3bfc04680149d32" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2. 批量计算、灵敏度分析与不确定度评定" + ], + "id": "254400fffad68f9" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "delta_n = Decimal(\"5\") # 偏转格数\n", + "results = []\n", + "\n", + "for item in calibration_data:\n", + " R_nom = item[\"R_nominal\"]\n", + " ratio = item[\"ratio_label\"]\n", + " C = item[\"C\"]\n", + " R0_data = item[\"R0_list\"]\n", + " R0_prime = item[\"R0_prime\"]\n", + " \n", + " R0_mean_temp = sum(R0_data) / Decimal(len(R0_data))\n", + " delta_R0 = abs(R0_prime - R0_mean_temp)\n", + " \n", + " if delta_R0 == 0:\n", + " S = Decimal(\"Infinity\")\n", + " delta_inst = Decimal(\"0\")\n", + " else:\n", + " S = delta_n * R0_mean_temp / delta_R0\n", + " delta_inst = R0_mean_temp / S\n", + " \n", + " R0_mean, u_R0, u_a_R0, u_b_R0, _ = calculate_stats(R0_data, str(delta_inst))\n", + " \n", + " Rx_mean = C * R0_mean\n", + " u_Rx = C * u_R0\n", + " u_r_Rx = u_Rx / Rx_mean if Rx_mean != 0 else Decimal(\"0\")\n", + " Rx_final, u_Rx_final = scientific_round(Rx_mean, u_Rx)\n", + " \n", + " results.append({\n", + " \"R_nominal\": R_nom, \"ratio_label\": ratio, \"C\": C,\n", + " \"R0_mean\": R0_mean, \"S\": S, \"delta_inst\": delta_inst,\n", + " \"Rx_mean\": Rx_mean, \"u_Rx\": u_Rx, \"u_r_Rx\": u_r_Rx,\n", + " \"Rx_final\": Rx_final, \"u_Rx_final\": u_Rx_final\n", + " })\n", + "\n", + "print(\"=\" * 85)\n", + "print(\" 测 量 结 果 汇 总 表 \")\n", + "print(\"=\" * 85)\n", + "header = f\"{'标定电阻':>8} {'比例臂':>8} {'R0均值(Ω)':>12} {'灵敏度S':>10} {'Δ_inst(Ω)':>12} {'Rx(Ω)':>12} {'u(Rx)(Ω)':>10} {'相对不确定度':>10}\"\n", + "print(header)\n", + "print(\"-\" * 85)\n", + "for res in results:\n", + " S_str = f\"{res['S']:.1f}\" if res['S'] != Decimal(\"Infinity\") else \"∞\"\n", + " print(f\"{res['R_nominal'] + ' Ω':>8} {res['ratio_label']:>8} {res['R0_mean']:>12.4f} {S_str:>10} \"\n", + " f\"{res['delta_inst']:>12.4f} {res['Rx_mean']:>12.4f} {res['u_Rx']:>10.4f} {res['u_r_Rx']*100:>9.2f}%\")\n", + "print(\"=\" * 85)\n", + "\n", + "print(\"\\n\" + \"*\" * 55)\n", + "print(\" 最 终 规 范 修 约 结 果 \")\n", + "print(\"*\" * 55)\n", + "for res in results:\n", + " print(f\"标定 {res['R_nominal']:>5} Ω (比例臂 {res['ratio_label']:>4}) : Rx = {res['Rx_final']} ± {res['u_Rx_final']} Ω\")\n", + "print(\"*\" * 55)" + ], + "id": "9a88f538183e4b0c" + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Bridge resistance measurement.py b/python/Bridge resistance measurement.py similarity index 100% rename from Bridge resistance measurement.py rename to python/Bridge resistance measurement.py diff --git a/python/Hall effect.ipynb b/python/Hall effect.ipynb new file mode 100644 index 0000000..ebec694 --- /dev/null +++ b/python/Hall effect.ipynb @@ -0,0 +1,210 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 霍尔效应实验数据处理 (Hall Effect)\n", + "\n", + "本 Notebook 用于对霍尔效应实验中直接测得的霍尔电压 $V_H$ 及零磁场电导电压 $V_\\sigma$ 进行线性拟合、导电类型判别、物理参数计算(电导率 $\\sigma$、霍尔灵敏度 $K_H$、霍尔系数 $R_H$、载流子浓度 $n$ 与迁移率 $\\mu$)及完整的不确定度传递分析。\n", + "\n", + "---\n", + "### 实验原理与数学公式\n", + "\n", + "#### 1. 霍尔效应基本关系与载流子类型判别\n", + "将通有控制工作电流 $I_s$ 的半导体薄片置于垂直磁场 $B$ 中,受洛伦兹力偏转产生横向霍尔电压 $V_H$:\n", + "$$V_H = K_H \\cdot I_s \\cdot B$$\n", + "* 若 $V_H > 0$:空穴导电,判定为 **P 型半导体**;\n", + "* 若 $V_H < 0$:电子导电,判定为 **N 型半导体**。\n", + "\n", + "#### 2. 磁场计算(螺线管)\n", + "励磁电流为 $I_m$,螺线管中心磁场强度满足:\n", + "$$B = k_{\\mathrm{sol}} \\cdot I_m = \\left(\\mu_0 \\frac{N}{L_{\\mathrm{sol}}}\\right) I_m$$\n", + "\n", + "#### 3. 电导率 $\\sigma$ 计算\n", + "在零磁场 ($I_m = 0$) 条件下测定不同工作电流 $I_s$ 下沿长度方向的电位差 $V_\\sigma$。线性回归斜率即为霍尔片电阻 $R = V_\\sigma / I_s$,则材料电导率为:\n", + "$$\\sigma = \\frac{L}{R \\cdot b \\cdot d}$$\n", + "* $L$:电极间距(长度);$b$:霍尔片宽度;$d$:霍尔片厚度。\n", + "\n", + "#### 4. 霍尔系数 $R_H$、载流子浓度 $n$ 与迁移率 $\\mu$\n", + "* 霍尔系数:$R_H = |K_H| \\cdot d$\n", + "* 载流子浓度:$n = \\frac{1}{R_H \\cdot e} = \\frac{1}{|K_H| \\cdot e \\cdot d}$($e = 1.602 \\times 10^{-19}\\,\\mathrm{C}$)\n", + "* 载流子迁移率:$\\mu = \\sigma \\cdot R_H = \\sigma \\cdot |K_H| \\cdot d$\n", + "\n", + "#### 5. 回归斜率不确定度与相对误差合成\n", + "* 线性回归斜率 $k$ 的不确定度:$u(k) = |k| \\sqrt{\\frac{1/r^2 - 1}{N - 2}}$\n", + "* 各物理量相对不确定度传递:\n", + "$$u_r(\\sigma) \\approx \\frac{u(R)}{R}, \\quad u_r(K_H) \\approx \\frac{u(k_1)}{k_1}, \\quad u_r(n) = u_r(K_H), \\quad u_r(\\mu) = \\sqrt{u_r^2(\\sigma) + u_r^2(K_H)}$$" + ], + "id": "c151d3bfb3ac3774" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "from python.utils import scientific_round, linear_regression\n", + "\n", + "print(\"霍尔效应计算与回归分析模块加载完成。\")" + ], + "id": "ebdba69fe30f7d93" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1. 物理常数与霍尔元件几何尺寸" + ], + "id": "eb6332ab0ffbedaf" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 基本物理常量\n", + "e_charge = 1.602e-19 # 元电荷 (C)\n", + "mu_0 = 4.0 * math.pi * 1e-7 # 真空磁导率 (T·m/A)\n", + "\n", + "# 霍尔元件几何尺寸 (mm -> m)\n", + "d_mm = 0.095 # 厚度 d (mm)\n", + "b_mm = 0.235 # 宽度 b (mm)\n", + "L_mm = 0.270 # 电极间距 L (mm)\n", + "\n", + "d = d_mm * 1e-3\n", + "b = b_mm * 1e-3\n", + "L = L_mm * 1e-3\n", + "\n", + "# 螺线管磁场比值常数 k_sol = B / Im (T/A)\n", + "k_sol = 0.00209\n", + "\n", + "print(f\"霍尔元件尺寸: L={L_mm} mm, b={b_mm} mm, d={d_mm} mm\")\n", + "print(f\"螺线管比值: k_sol = {k_sol} T/A\")" + ], + "id": "964b32703abf7887" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2. 实验测量数据输入\n", + "> **包含三组数据**:\n", + "> 1. 固定 $I_m$,测量 $V_H$ 随 $I_s$ 的变化;\n", + "> 2. 固定 $I_s$,测量 $V_H$ 随 $I_m$ 的变化;\n", + "> 3. 零磁场下测量 $V_\\sigma$ 随 $I_s$ 的变化。" + ], + "id": "bae2c240cece3f2a" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# --- 1. VH vs Is 数据 (固定 Im = 600 mA) ---\n", + "Im_fixed_mA = 600.0\n", + "B_fixed = k_sol * (Im_fixed_mA * 1e-3) # T\n", + "Is_1 = [0.20 * (i + 1) for i in range(10)] # mA: 0.20 ~ 2.00\n", + "# 实测 VH 带符号读数 (mV)\n", + "VH_signed_1 = [-1.52, -3.05, -4.58, -6.11, -7.63, -9.16, -10.68, -12.21, -13.74, -15.26]\n", + "VH_abs_1 = [abs(x) for x in VH_signed_1]\n", + "\n", + "# --- 2. VH vs Im 数据 (固定 Is = 1.50 mA) ---\n", + "Is_fixed_mA = 1.50\n", + "Im_2 = [100.0, 150.0, 200.0, 250.0, 300.0, 350.0, 400.0, 450.0, 500.0, 550.0, 600.0] # mA\n", + "VH_signed_2 = [-1.91, -2.87, -3.82, -4.78, -5.73, -6.69, -7.64, -8.60, -9.55, -10.51, -11.46] # mV\n", + "VH_abs_2 = [abs(x) for x in VH_signed_2]\n", + "\n", + "# --- 3. Vσ vs Is 数据 (零磁场 Im = 0) ---\n", + "Is_3 = list(Is_1) # mA\n", + "Vsigma = [45.2, 90.5, 135.8, 181.0, 226.3, 271.5, 316.8, 362.0, 407.3, 452.5] # mV\n", + "\n", + "print(\"实验数据载入完成。\")" + ], + "id": "b944163d5b6fa5f7" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3. 线性回归分析与参数计算" + ], + "id": "913d723bf7836a79" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 1. 回归 1: VH(mV) vs Is(mA) -> 斜率单位 mV/mA = V/A\n", + "k1, b1, r1, uk1 = map(float, linear_regression(Is_1, VH_abs_1))\n", + "\n", + "# 2. 回归 2: VH(mV) vs Im(mA)\n", + "k2, b2, r2, uk2 = map(float, linear_regression(Im_2, VH_abs_2))\n", + "\n", + "# 3. 回归 3: Vσ(mV) vs Is(mA) -> 样品电阻 R (Ω)\n", + "R_sample, b3, r3, u_R = map(float, linear_regression(Is_3, Vsigma))\n", + "\n", + "# 导电类型判定\n", + "carrier_type = \"P 型 (空穴导电)\" if sum(VH_signed_1)/len(VH_signed_1) > 0 else \"N 型 (电子导电)\"\n", + "\n", + "# 电导率 σ = L / (R · b · d)\n", + "sigma = L / (R_sample * b * d)\n", + "u_r_sigma = abs(u_R / R_sample)\n", + "u_sigma = sigma * u_r_sigma\n", + "sigma_final, u_sigma_final = scientific_round(sigma, u_sigma)\n", + "\n", + "# 霍尔灵敏度 KH = k1 / B_fixed\n", + "KH = k1 / B_fixed\n", + "u_r_KH = abs(uk1 / k1)\n", + "u_KH = KH * u_r_KH\n", + "KH_final, u_KH_final = scientific_round(KH, u_KH)\n", + "\n", + "# 霍尔系数 RH = KH * d\n", + "RH = KH * d\n", + "\n", + "# 载流子浓度 n = 1 / (RH · e)\n", + "n_carrier = 1.0 / (RH * e_charge)\n", + "u_n = n_carrier * u_r_KH\n", + "n_final, u_n_final = scientific_round(n_carrier, u_n)\n", + "\n", + "# 迁移率 μ = σ · RH\n", + "mu = sigma * RH\n", + "u_r_mu = math.sqrt(u_r_sigma**2 + u_r_KH**2)\n", + "u_mu = mu * u_r_mu\n", + "mu_final, u_mu_final = scientific_round(mu, u_mu)\n", + "\n", + "print(\"=\" * 55)\n", + "print(\" 霍 尔 效 应 实 验 结 果 汇 总 \")\n", + "print(\"=\" * 55)\n", + "print(f\"材料导电类型 : {carrier_type}\")\n", + "print(f\"VH-Is 回归斜率 k1 : {k1:.4f} V/A (r={r1:.4f})\")\n", + "print(f\"VH-Im 回归斜率 k2 : {k2:.4f} V/A (r={r2:.4f})\")\n", + "print(f\"样品内阻 R (斜率k3): {R_sample:.4f} Ω (r={r3:.4f})\")\n", + "print(\"-\" * 55)\n", + "print(f\"电导率 σ : {sigma_final} ± {u_sigma_final} S/m\")\n", + "print(f\"霍尔灵敏度 |KH| : {KH_final} ± {u_KH_final} V/(A·T)\")\n", + "print(f\"霍尔系数 RH : {RH:.4e} m³/C\")\n", + "print(f\"载流子浓度 n : {n_final} ± {u_n_final} m⁻³\")\n", + "print(f\"载流子迁移率 μ : {mu_final} ± {u_mu_final} m²/(V·s)\")\n", + "print(\"-\" * 55)\n", + "print(f\"相对不确定度 u_r(σ) : {u_r_sigma*100:.2f}%\")\n", + "print(f\"相对不确定度 u_r(KH): {u_r_KH*100:.2f}%\")\n", + "print(f\"相对不确定度 u_r(μ) : {u_r_mu*100:.2f}%\")\n", + "print(\"=\" * 55)" + ], + "id": "7f25b56c710f5a1f" + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Hall effect.py b/python/Hall effect.py similarity index 100% rename from Hall effect.py rename to python/Hall effect.py diff --git a/python/Newton's rings.ipynb b/python/Newton's rings.ipynb new file mode 100644 index 0000000..029368b --- /dev/null +++ b/python/Newton's rings.ipynb @@ -0,0 +1,161 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 牛顿环曲率半径计算 (逐差法)\n", + "\n", + "本 Notebook 用于通过读数显微镜测得的牛顿环干涉暗环直径平方数据,利用**逐差法**计算平凸透镜的曲率半径 $R$,并完成高精度的合成不确定度评定与结果修约。\n", + "\n", + "---\n", + "### 实验原理与数学公式\n", + "\n", + "#### 1. 牛顿环曲率半径计算公式\n", + "平凸透镜与平板玻璃构成的空气薄膜发生等厚干涉,第 $m$ 级暗环直径为 $D_m$。第 $i+M$ 级与第 $i$ 级暗环直径平方差为:\n", + "$$\\Delta_i = D_{i+M}^2 - D_i^2 = 4 M R \\lambda$$\n", + "逐差平均值:\n", + "$$\\Delta_{\\mathrm{avg}} = \\frac{1}{M} \\sum_{i=1}^M \\Delta_i$$\n", + "曲率半径 $R$ 为:\n", + "$$R = \\frac{\\Delta_{\\mathrm{avg}}}{4 M \\lambda}$$\n", + "* $\\lambda$:单色光波长(钠黄光默认取 $\\lambda = 589.3\\,\\mathrm{nm} = 0.0005893\\,\\mathrm{mm}$)\n", + "* $M$:逐差跨度(通常第 11~30 环,分为 11~20 环与 21~30 环,跨度 $M = 10$)\n", + "\n", + "#### 2. 不确定度传递公式\n", + "* **A 类不确定度**(针对逐差后的 $\\Delta_i$ 项):\n", + "$$s_\\Delta = \\sqrt{\\frac{1}{M-1} \\sum_{i=1}^M (\\Delta_i - \\Delta_{\\mathrm{avg}})^2}, \\quad u_A(\\Delta) = \\frac{s_\\Delta}{\\sqrt{M}}$$\n", + "* **B 类不确定度**(由读数显微镜测量误差传递):\n", + "设显微镜坐标不确定度为 $u(x) = \\frac{\\Delta_{\\mathrm{inst}}}{\\sqrt{3}}$,则直径 $D = x_R - x_L$ 的平方 $D^2$ 的不确定度满足 $u^2(D^2) = 8 D^2 u^2(x)$。\n", + "由逐差平均传播可得:\n", + "$$u_B^2(\\Delta_{\\mathrm{avg}}) = \\frac{8 u^2(x)}{M^2} \\sum_{i=1}^M (D_i^2 + D_{i+M}^2)$$\n", + "* **合成不确定度传递到曲率半径 $R$**:\n", + "$$u(\\Delta) = \\sqrt{u_A^2(\\Delta) + u_B^2(\\Delta_{\\mathrm{avg}})}, \\quad u(R) = \\frac{u(\\Delta)}{4 M \\lambda}$$" + ], + "id": "6a150fa764d0b625" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "from decimal import Decimal\n", + "from python.utils import scientific_round\n", + "\n", + "print(\"牛顿环计算模块加载完成。\")" + ], + "id": "9218591496bcc1bc" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1. 实验常数与暗环测量数据输入\n", + "> **提示**:`d_squared_vals` 为第 11 到 30 环的暗环直径平方 $D^2\\,(\\mathrm{mm^2})$(共 20 个数据)。" + ], + "id": "924c4d14704f767a" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 钠光波长 λ (mm)\n", + "WAVELENGTH = Decimal(\"0.0005893\")\n", + "\n", + "# 仪器参数\n", + "delta_inst = Decimal(\"0.005\") # 读数显微镜仪器误差限 (mm)\n", + "\n", + "# 第 11 环 ~ 第 30 环的 D^2 数据 (mm^2)\n", + "# 示例数据 (请按实际实验数据替换):\n", + "d_squared_vals = [\n", + " Decimal(\"12.15\"), Decimal(\"13.20\"), Decimal(\"14.35\"), Decimal(\"15.42\"), Decimal(\"16.55\"),\n", + " Decimal(\"17.68\"), Decimal(\"18.72\"), Decimal(\"19.85\"), Decimal(\"20.90\"), Decimal(\"22.05\"),\n", + " Decimal(\"23.15\"), Decimal(\"24.28\"), Decimal(\"25.40\"), Decimal(\"26.50\"), Decimal(\"27.62\"),\n", + " Decimal(\"28.75\"), Decimal(\"29.80\"), Decimal(\"30.95\"), Decimal(\"32.00\"), Decimal(\"33.15\")\n", + "]\n", + "\n", + "print(f\"光波波长 λ: {WAVELENGTH} mm\")\n", + "print(f\"显微镜误差 Δ_inst: {delta_inst} mm\")\n", + "print(f\"已载入暗环平方数据点数: {len(d_squared_vals)}\")" + ], + "id": "959e1b03984add7a" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2. 逐差计算与曲率半径求解" + ], + "id": "d92aa19f25481c10" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "M = 10 # 逐差项数\n", + "deltas = []\n", + "sum_d_pairs = Decimal(\"0\")\n", + "\n", + "print(\"--- 逐差过程 (Δ_i = D_{i+10}^2 - D_i^2) ---\")\n", + "for i in range(10):\n", + " d_low_sq = d_squared_vals[i]\n", + " d_high_sq = d_squared_vals[i + 10]\n", + " diff = d_high_sq - d_low_sq\n", + " deltas.append(diff)\n", + " sum_d_pairs += (d_low_sq + d_high_sq)\n", + " print(f\"Δ_{i+11:02d} = D_{i+21}^2 - D_{i+11}^2 = {diff:.3f} mm^2\")\n", + "\n", + "mean_delta = sum(deltas) / Decimal(str(M))\n", + "\n", + "# 曲率半径 R = Δ_avg / (4 * M * λ)\n", + "denominator = Decimal(\"4\") * Decimal(str(M)) * WAVELENGTH\n", + "r_val = mean_delta / denominator\n", + "\n", + "# 不确定度计算\n", + "# 1. A 类不确定度\n", + "variance_delta = sum((x - mean_delta)**2 for x in deltas) / Decimal(str(M - 1))\n", + "s_delta = Decimal(str(math.sqrt(float(variance_delta))))\n", + "u_a_delta = s_delta / Decimal(str(math.sqrt(M)))\n", + "\n", + "# 2. B 类不确定度传播\n", + "u_x = delta_inst / Decimal(str(math.sqrt(3)))\n", + "u_b_delta_sq = (Decimal(\"8\") * (u_x**2) * sum_d_pairs) / (Decimal(str(M))**2)\n", + "u_b_delta = Decimal(str(math.sqrt(float(u_b_delta_sq))))\n", + "\n", + "# 3. 合成不确定度并在 R 上传递\n", + "u_delta_combined = Decimal(str(math.sqrt(float(u_a_delta**2 + u_b_delta**2))))\n", + "u_r = u_delta_combined / denominator\n", + "\n", + "# 4. 科学修约\n", + "r_final, u_final = scientific_round(r_val, u_r)\n", + "\n", + "print(\"\\n\" + \"=\" * 45)\n", + "print(\" 牛 顿 环 曲 率 半 径 计 算 结 果 \")\n", + "print(\"=\" * 45)\n", + "print(f\"逐差平均差值 Δ_avg : {mean_delta:.4f} mm^2\")\n", + "print(f\"差值标准差 s(Δ) : {s_delta:.4f} mm^2\")\n", + "print(f\"A 类不确定度 u_A(Δ) : {u_a_delta:.4f} mm^2\")\n", + "print(f\"B 类不确定度 u_B(Δ) : {u_b_delta:.4f} mm^2\")\n", + "print(\"-\" * 45)\n", + "print(f\"曲率半径 R (原始) : {r_val:.4f} mm ({r_val/1000:.4f} m)\")\n", + "print(f\"合成不确定度 u(R) : {u_r:.4f} mm\")\n", + "print(\"-\" * 45)\n", + "print(f\"★ 最终修约结果 : R = {r_final} ± {u_final} mm\")\n", + "print(\"=\" * 45)" + ], + "id": "6258ef3d424cf16f" + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Newton's rings.py b/python/Newton's rings.py similarity index 100% rename from Newton's rings.py rename to python/Newton's rings.py diff --git a/python/Speed of sound.ipynb b/python/Speed of sound.ipynb new file mode 100644 index 0000000..7de640f --- /dev/null +++ b/python/Speed of sound.ipynb @@ -0,0 +1,158 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 声速测量计算器 (逐差法)\n", + "\n", + "本 Notebook 用于通过**干涉法(驻波法)**与**相位法(李萨如图形法)**测得的接收器位移坐标,利用**逐差法**计算超声波在空气中的波长 $\\lambda$、实际声速 $v$,并与理论声速进行对比和不确定度评定。\n", + "\n", + "---\n", + "### 实验原理与数学公式\n", + "\n", + "#### 1. 理论声速公式\n", + "在摄氏温度 $t\\,({}^\\circ\\mathrm{C})$ 下,空气中理想声速为:\n", + "$$v_t = 331.45 \\sqrt{1 + \\frac{t}{273.15}} \\quad (\\mathrm{m/s})$$\n", + "\n", + "#### 2. 逐差法测波长 $\\lambda$\n", + "两组数据各测 12 个极值或特定相位位置坐标 $x_1, x_2, \\dots, x_{12}$,分成两组逐差:\n", + "$$\\Delta x_i = x_{i+6} - x_i \\quad (i = 1, 2, \\dots, 6), \\quad \\Delta x_{\\mathrm{avg}} = \\frac{1}{6} \\sum_{i=1}^6 \\Delta x_i$$\n", + "* **干涉法(驻波法)**:相邻波腹间距为 $\\lambda/2$,6 个间隔对应 $\\Delta x = 6 \\times (\\lambda/2) = 3\\lambda$,故:\n", + "$$\\lambda = \\frac{\\Delta x_{\\mathrm{avg}}}{3}$$\n", + "* **相位法(李萨如法)**:相邻同向直线相位差为 $2\\pi$,对应间距为 $\\lambda$,6 个间隔对应 $\\Delta x = 6\\lambda$,故:\n", + "$$\\lambda = \\frac{\\Delta x_{\\mathrm{avg}}}{6}$$\n", + "\n", + "#### 3. 声速与不确定度传递公式\n", + "实验测得声速为 $v = f \\cdot \\lambda$。\n", + "* 逐差平均值的 A 类不确定度:$u_A(\\Delta x_{\\mathrm{avg}}) = \\frac{s_\\Delta}{\\sqrt{6}}$\n", + "* 逐差平均值的 B 类不确定度:$u_B(\\Delta x_{\\mathrm{avg}}) = \\sqrt{\\frac{2}{6}} \\frac{\\Delta_{\\mathrm{inst}}}{\\sqrt{3}} = \\frac{\\Delta_{\\mathrm{inst}}}{3}$\n", + "* 合成不确定度:$u_c(\\Delta x_{\\mathrm{avg}}) = \\sqrt{u_A^2 + u_B^2}$\n", + "* 波长及声速不确定度(忽略频率 $f$ 极小的不确定度):\n", + "$$u(\\lambda) = \\frac{u_c(\\Delta x_{\\mathrm{avg}})}{k}, \\quad u(v) = f \\cdot u(\\lambda)$$" + ], + "id": "db386d820ed39819" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "from decimal import Decimal\n", + "from python.utils import scientific_round\n", + "\n", + "print(\"声速计算模块加载完成。\")" + ], + "id": "72c0a986ef718706" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1. 实验条件与测量数据输入\n", + "> **提示**:可在此输入环境温度、激励信号源频率,以及干涉法和相位法分别测得的 12 个坐标值 $x_i\\,(\\mathrm{mm})$。" + ], + "id": "b2da551f9b00370d" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 实验环境参数\n", + "t_celsius = 24.0 # 环境温度 (℃)\n", + "freq = 37000.0 # 谐振超声波频率 (Hz)\n", + "delta_inst = Decimal(\"0.02\") # 游标卡尺/测微鼓轮分度值误差 (mm)\n", + "\n", + "# 1. 干涉法 (驻波法) 12 个坐标 (mm)\n", + "x_vals_interference = [\n", + " Decimal(\"10.20\"), Decimal(\"14.85\"), Decimal(\"19.50\"), Decimal(\"24.15\"), Decimal(\"28.80\"), Decimal(\"33.45\"),\n", + " Decimal(\"38.10\"), Decimal(\"42.75\"), Decimal(\"47.40\"), Decimal(\"52.05\"), Decimal(\"56.70\"), Decimal(\"61.35\")\n", + "]\n", + "\n", + "# 2. 相位法 (李萨如图形法) 12 个坐标 (mm)\n", + "x_vals_phase = [\n", + " Decimal(\"12.00\"), Decimal(\"21.30\"), Decimal(\"30.60\"), Decimal(\"39.90\"), Decimal(\"49.20\"), Decimal(\"58.50\"),\n", + " Decimal(\"67.80\"), Decimal(\"77.10\"), Decimal(\"86.40\"), Decimal(\"95.70\"), Decimal(\"105.00\"), Decimal(\"114.30\")\n", + "]\n", + "\n", + "v_theory = 331.45 * math.sqrt(1 + t_celsius / 273.15)\n", + "print(f\"当前温度 {t_celsius} ℃ 下的理论声速 v_t: {v_theory:.2f} m/s\")" + ], + "id": "634f5ee7bd5300e5" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2. 逐差法处理与声速不确定度评定" + ], + "id": "223b20661658522d" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def process_speed_of_sound(method_name, x_vals, f, k, delta_inst):\n", + " M = 6\n", + " deltas = []\n", + " for i in range(M):\n", + " diff = x_vals[i + M] - x_vals[i]\n", + " deltas.append(diff)\n", + " mean_delta = sum(deltas) / Decimal(str(M))\n", + " \n", + " # 计算波长 (mm -> m)\n", + " wavelength_mm = mean_delta / Decimal(str(k))\n", + " wavelength_m = wavelength_mm / Decimal(\"1000\")\n", + " velocity = Decimal(str(f)) * wavelength_m\n", + " \n", + " # 不确定度分析\n", + " variance_delta = sum((d - mean_delta)**2 for d in deltas) / Decimal(str(M - 1))\n", + " s_delta = Decimal(str(math.sqrt(float(variance_delta))))\n", + " u_a_delta = s_delta / Decimal(str(math.sqrt(M)))\n", + " \n", + " u_x = delta_inst / Decimal(str(math.sqrt(3)))\n", + " u_b_delta = Decimal(str(math.sqrt(2.0 / M))) * u_x\n", + " u_delta_combined = Decimal(str(math.sqrt(float(u_a_delta**2 + u_b_delta**2))))\n", + " \n", + " u_wavelength_mm = u_delta_combined / Decimal(str(k))\n", + " u_wavelength_m = u_wavelength_mm / Decimal(\"1000\")\n", + " u_velocity = Decimal(str(f)) * u_wavelength_m\n", + " \n", + " v_final, uv_final = scientific_round(velocity, u_velocity)\n", + " \n", + " print(\"=\" * 45)\n", + " print(f\" {method_name} 计算结果 \")\n", + " print(\"=\" * 45)\n", + " print(f\"逐差平均位移 Δx_avg : {mean_delta:.4f} mm\")\n", + " print(f\"位移标准差 s(Δx) : {s_delta:.4f} mm\")\n", + " print(f\"声波波长 λ : {wavelength_mm:.4f} mm\")\n", + " print(f\"波长不确定度 u(λ) : {u_wavelength_mm:.4f} mm\")\n", + " print(f\"实验声速 v (原始) : {velocity:.2f} m/s\")\n", + " print(f\"声速合成不确定度 u(v): {u_velocity:.2f} m/s\")\n", + " rel_error = abs(float(velocity) - v_theory) / v_theory * 100\n", + " print(f\"与理论声速相对误差 : {rel_error:.2f}%\")\n", + " print(\"-\" * 45)\n", + " print(f\"★ 最终修约结果 : v = {v_final} ± {uv_final} m/s\")\n", + " print(\"=\" * 45 + \"\\n\")\n", + "\n", + "# 处理两种方法\n", + "process_speed_of_sound(\"干涉法 (驻波法)\", x_vals_interference, freq, 3, delta_inst)\n", + "process_speed_of_sound(\"相位法 (李萨如法)\", x_vals_phase, freq, 6, delta_inst)" + ], + "id": "70554a3d5158f78e" + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Speed of sound.py b/python/Speed of sound.py similarity index 100% rename from Speed of sound.py rename to python/Speed of sound.py diff --git a/python/Surface tension coefficient.ipynb b/python/Surface tension coefficient.ipynb new file mode 100644 index 0000000..08ca06a --- /dev/null +++ b/python/Surface tension coefficient.ipynb @@ -0,0 +1,190 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 拉脱法测液体表面张力系数计算器\n", + "\n", + "本 Notebook 用于通过力敏传感器测定拉脱法实验中液体(水)的表面张力系数 $\\alpha$。包含砝码电压标定(线性回归求传感器灵敏度 $K$)、拉脱峰值电压与吊环内外径统计、不确定度合成及规范修约。\n", + "\n", + "---\n", + "### 实验原理与数学公式\n", + "\n", + "#### 1. 表面张力系数计算公式\n", + "当薄壁圆环从液体表面被拉脱的一瞬间,所受的最大液体表面张力与圆环内外周长成正比:\n", + "$$\\alpha = \\frac{F}{\\pi (d_1 + d_2)}$$\n", + "* $F$:最大拉脱力(通过力敏传感器测得 $F = U_{\\mathrm{pull}} / K$)\n", + "* $K$:力敏传感器灵敏度 $(\\mathrm{V/N})$ 或 $(\\mathrm{mV/N})$\n", + "* $d_1, d_2$:金属圆环的内径与外径\n", + "\n", + "#### 2. 力敏传感器灵敏度标定\n", + "使用单重为 $m$ 的标准小砝码增重与减重 $i$ 个($i = 0, 1, \\dots, 5$),各测得对应的输出电压 $U_{1,i}, U_{2,i}$。取增减重平均值:\n", + "$$\\bar{U}_i = \\frac{U_{1,i} + U_{2,i}}{2}, \\quad F_i = i \\cdot m \\cdot g$$\n", + "使用最小二乘线性回归对 $(F_i, \\bar{U}_i)$ 拟合直线 $U = K \\cdot F + b$,斜率即为灵敏度 $K$,其标准不确定度记为 $u(K)$。\n", + "\n", + "#### 3. 不确定度合成与传递\n", + "* 拉脱力 $F$ 的相对不确定度:\n", + "$$u_r(F) = \\sqrt{ \\left(\\frac{u_U}{\\bar{U}_{\\mathrm{pull}}}\\right)^2 + \\left(\\frac{u_K}{K}\\right)^2 }$$\n", + "* 直径和 $(d_1 + d_2)$ 的不确定度:\n", + "$$u(d_1 + d_2) = \\sqrt{u^2(d_1) + u^2(d_2)}, \\quad u_r(d_1+d_2) = \\frac{u(d_1 + d_2)}{d_1 + d_2}$$\n", + "* 表面张力系数 $\\alpha$ 的总相对不确定度与绝对不确定度:\n", + "$$u_r(\\alpha) = \\sqrt{u_r^2(F) + u_r^2(d_1 + d_2)}, \\quad u(\\alpha) = \\alpha \\cdot u_r(\\alpha)$$" + ], + "id": "781711c13d45a52a" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "from decimal import Decimal\n", + "from python.utils import scientific_round, calculate_stats, linear_regression\n", + "\n", + "print(\"表面张力系数模块加载完成。\")" + ], + "id": "d1f8bddcdc7a754a" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1. 传感器砝码标定数据输入与拟合\n", + "> **提示**:可在此输入单个小砝码质量 `m_mass_g` 以及增重、减重各 6 个点对应的标定电压。" + ], + "id": "6a7bf3a6389e57db" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "G = Decimal(\"9.80\") # 重力加速度 (m/s^2)\n", + "PI = Decimal(str(math.pi))\n", + "\n", + "# 单个小砝码质量 (g)\n", + "m_mass_g = Decimal(\"0.50\")\n", + "m_mass_kg = m_mass_g / Decimal(\"1000\")\n", + "\n", + "# 标定电压 (0~5个砝码, 单位与后续拉脱电压保持统一即可, 如 V 或 mV)\n", + "U_calib_1 = [Decimal(\"0.00\"), Decimal(\"0.052\"), Decimal(\"0.104\"), Decimal(\"0.155\"), Decimal(\"0.207\"), Decimal(\"0.259\")] # 增重\n", + "U_calib_2 = [Decimal(\"0.00\"), Decimal(\"0.051\"), Decimal(\"0.103\"), Decimal(\"0.156\"), Decimal(\"0.208\"), Decimal(\"0.260\")] # 减重\n", + "\n", + "# 计算平均电压与外力\n", + "U_calib_avg = [(U_calib_1[i] + U_calib_2[i]) / Decimal(\"2\") for i in range(6)]\n", + "F_calib = [Decimal(str(i)) * m_mass_kg * G for i in range(6)]\n", + "\n", + "# 线性回归拟合灵敏度 K\n", + "K, K_b, r, u_K = linear_regression(F_calib, U_calib_avg)\n", + "\n", + "print(\"=\" * 45)\n", + "print(\" 传 感 器 标 定 拟 合 结 果 \")\n", + "print(\"=\" * 45)\n", + "print(f\"灵敏度斜率 K : {K:.6f} (电压单位/N)\")\n", + "print(f\"截距 b : {K_b:.6f}\")\n", + "print(f\"相关系数 r : {r:.6f}\")\n", + "print(f\"斜率不确定度 u_K: {u_K:.6e}\")" + ], + "id": "db8e4e72f93077d5" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2. 拉脱实验数据与圆环几何尺寸输入" + ], + "id": "37706b6c98e4d762" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 仪器允差\n", + "delta_U = Decimal(\"0.001\") # 电压表仪器误差\n", + "delta_d = Decimal(\"0.02\") # 游标卡尺仪器误差 (mm)\n", + "\n", + "# 6次测量数据列表\n", + "U_pull_vals = [Decimal(\"0.215\"), Decimal(\"0.214\"), Decimal(\"0.216\"), Decimal(\"0.215\"), Decimal(\"0.213\"), Decimal(\"0.215\")]\n", + "d1_vals = [Decimal(\"33.10\"), Decimal(\"33.12\"), Decimal(\"33.08\"), Decimal(\"33.10\"), Decimal(\"33.14\"), Decimal(\"33.10\")] # 内径 (mm)\n", + "d2_vals = [Decimal(\"35.20\"), Decimal(\"35.22\"), Decimal(\"35.18\"), Decimal(\"35.20\"), Decimal(\"35.22\"), Decimal(\"35.20\")] # 外径 (mm)\n", + "\n", + "# 统计各量\n", + "U_pull_mean, U_pull_u = calculate_stats(U_pull_vals, delta_U)[:2]\n", + "d1_mean, d1_u = calculate_stats(d1_vals, delta_d)[:2]\n", + "d2_mean, d2_u = calculate_stats(d2_vals, delta_d)[:2]\n", + "\n", + "print(f\"拉脱平均电压 U_pull: {U_pull_mean:.4f} ± {U_pull_u:.4f}\")\n", + "print(f\"圆环平均内径 d1 : {d1_mean:.3f} ± {d1_u:.3f} mm\")\n", + "print(f\"圆环平均外径 d2 : {d2_mean:.3f} ± {d2_u:.3f} mm\")" + ], + "id": "4898cff018c8803b" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3. 表面张力系数计算与不确定度修约" + ], + "id": "188a7b2b3dc5b54" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 计算拉脱力 F (N)\n", + "F_pull_mean = U_pull_mean / K\n", + "\n", + "# 换算为标准国际单位 m\n", + "d1_m = d1_mean / Decimal(\"1000\")\n", + "d2_m = d2_mean / Decimal(\"1000\")\n", + "d1_u_m = d1_u / Decimal(\"1000\")\n", + "d2_u_m = d2_u / Decimal(\"1000\")\n", + "d_sum = d1_m + d2_m\n", + "\n", + "# 表面张力系数 α = F / [π * (d1 + d2)]\n", + "alpha_val = F_pull_mean / (PI * d_sum)\n", + "\n", + "# 不确定度合成传递\n", + "u_r_F_sq = (U_pull_u / U_pull_mean)**2 + (u_K / K)**2\n", + "u_r_F = Decimal(str(math.sqrt(float(u_r_F_sq))))\n", + "\n", + "u_d_sum = Decimal(str(math.sqrt(float(d1_u_m**2 + d2_u_m**2))))\n", + "u_r_d_sum = u_d_sum / d_sum\n", + "\n", + "u_r_alpha = Decimal(str(math.sqrt(float(u_r_F**2 + u_r_d_sum**2))))\n", + "alpha_u = alpha_val * u_r_alpha\n", + "\n", + "# 科学修约\n", + "alpha_final, u_final = scientific_round(alpha_val, alpha_u)\n", + "\n", + "print(\"=\" * 45)\n", + "print(\" 表 面 张 力 系 数 计 算 结 果 \")\n", + "print(\"=\" * 45)\n", + "print(f\"最大拉脱力 F : {F_pull_mean:.6f} N\")\n", + "print(f\"圆环周长项 (d1+d2): {(d_sum*1000):.3f} mm\")\n", + "print(f\"表面张力系数 α (原始): {alpha_val:.6f} N/m\")\n", + "print(\"-\" * 45)\n", + "print(f\"相对不确定度 u_r : {u_r_alpha * 100:.2f}%\")\n", + "print(f\"绝对不确定度 u_α : {alpha_u:.6f} N/m\")\n", + "print(\"-\" * 45)\n", + "print(f\"★ 最终修约结果 : α = {alpha_final} ± {u_final} N/m\")\n", + "print(\"=\" * 45)" + ], + "id": "74791d410688e40a" + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Surface tension coefficient.py b/python/Surface tension coefficient.py similarity index 100% rename from Surface tension coefficient.py rename to python/Surface tension coefficient.py diff --git a/python/Thermal conductivity.ipynb b/python/Thermal conductivity.ipynb new file mode 100644 index 0000000..3ee42d1 --- /dev/null +++ b/python/Thermal conductivity.ipynb @@ -0,0 +1,216 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 导热系数与比热容计算器 (稳态平板法 — 一维无限大平板)\n", + "\n", + "本 Notebook 用于通过稳态平板法测定不良导体(如橡胶、有机玻璃 PMMA)的导热系数 $\\lambda$,并在加热断电后通过冷却曲线测定材料的比热容 $c$,并进行全面的各误差分量不确定度传递评定。\n", + "\n", + "---\n", + "### 实验原理与数学公式\n", + "\n", + "#### 1. 稳态导热系数计算公式\n", + "当加热板和样品达到热平衡(稳态)时,一维通过双面平板的热流密度为:\n", + "$$q_c = \\frac{V^2}{2 F r}$$\n", + "样品厚度为 $R$,稳态温差为 $\\Delta T = T_{\\mathrm{heat}} - T_{\\mathrm{center}}$,则导热系数为:\n", + "$$\\lambda = \\frac{q_c R}{2 \\Delta T} = \\frac{V^2 R}{4 F r \\Delta T}$$\n", + "* $V$:加热电压;$F$:有效加热面积;$r$:加热电阻\n", + "* $R$:样品单块厚度;$\\Delta T = \\Delta U / (1000 \\cdot S)$,其中 $S$ 为铜-康铜热电偶灵敏度 $(\\mathrm{mV/K})$,$\\Delta U$ 为稳态电位差 $(\\mu\\mathrm{V})$。\n", + "\n", + "#### 2. 冷却法测比热容公式\n", + "断电后,样品中心面温度随时间自然冷却。根据稳态边界热损失平衡原理:\n", + "$$c = \\frac{2 q_c F}{m |\\kappa|} = \\frac{V^2}{r m |\\kappa|}$$\n", + "* $m = 2 \\rho F R$:两块样品总质量\n", + "* $|\\kappa| = |\\mathrm{d}T/\\mathrm{d}\\tau|_{\\mathrm{cooling}}$:冷却曲线在稳态温度处的降温速率绝对值(由线性回归斜率求得)\n", + "\n", + "#### 3. 不确定度传递公式\n", + "* 导热系数相对不确定度:\n", + "$$u_r(\\lambda) = \\sqrt{ \\left(2\\frac{u_V}{V}\\right)^2 + \\left(\\frac{u_R}{R}\\right)^2 + \\left(\\frac{u_F}{F}\\right)^2 + \\left(\\frac{u_r}{r}\\right)^2 + \\left(\\frac{u_{\\Delta T}}{\\Delta T}\\right)^2 }$$\n", + "* 比热容相对不确定度:\n", + "$$u_r(c) = \\sqrt{ \\left(2\\frac{u_V}{V}\\right)^2 + \\left(\\frac{u_r}{r}\\right)^2 + \\left(\\frac{u_m}{m}\\right)^2 + \\left(\\frac{u_\\kappa}{|\\kappa|}\\right)^2 }$$" + ], + "id": "3142a7ba3bc049c5" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "from decimal import Decimal\n", + "from python.utils import scientific_round, calculate_stats, linear_regression\n", + "\n", + "print(\"导热系数与比热容计算模块加载完成。\")" + ], + "id": "c47a1312e0c65d9a" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1. 实验仪器参数与误差限设置" + ], + "id": "154810a723172bde" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# --- 稳态平板仪参数 ---\n", + "V_heat = Decimal(\"18.00\") # 加热电压 (V)\n", + "F = Decimal(\"0.006885\") # 有效加热面积 = 0.85 * 0.09 * 0.09 (m^2)\n", + "r = Decimal(\"110.0\") # 加热板电阻 (Ω)\n", + "R_thickness = Decimal(\"0.010\") # 样品厚度 (m)\n", + "S_sensitivity = Decimal(\"0.040\") # 热电偶灵敏度 (mV/K)\n", + "\n", + "# --- 仪器误差限 ---\n", + "delta_V = Decimal(\"0.01\") # 电压表误差 (V)\n", + "delta_F = Decimal(\"0.00001\") # 面积误差 (m^2)\n", + "delta_r = Decimal(\"0.1\") # 电阻误差 (Ω)\n", + "delta_R = Decimal(\"0.0001\") # 厚度误差 (m)\n", + "delta_uV_inst = Decimal(\"2.0\") # 微伏计仪器误差 (uV)\n", + "\n", + "print(\"实验基础参数载入完成。\")" + ], + "id": "c3a982cafe2a031e" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2. 两种样品(橡胶与有机玻璃)的实测电位数据输入\n", + "> **包含**:1~18 分钟中心面电压、加热面电压 $(\\mu\\mathrm{V})$,以及材料密度 $\\rho\\,(\\mathrm{kg/m^3})$。" + ], + "id": "5723fb0d64d37098" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 橡胶 (Rubber) 数据\n", + "rubber_center_uV = [\n", + " Decimal(\"15.2\"), Decimal(\"35.4\"), Decimal(\"58.1\"), Decimal(\"80.5\"), Decimal(\"101.2\"), Decimal(\"120.0\"),\n", + " Decimal(\"136.5\"), Decimal(\"150.2\"), Decimal(\"161.5\"), Decimal(\"170.4\"), Decimal(\"177.2\"), Decimal(\"182.1\"),\n", + " Decimal(\"185.5\"), Decimal(\"187.8\"), Decimal(\"189.2\"), Decimal(\"190.0\"), Decimal(\"190.4\"), Decimal(\"190.6\")\n", + "]\n", + "rubber_heating_uV = [\n", + " Decimal(\"85.0\"), Decimal(\"150.2\"), Decimal(\"205.1\"), Decimal(\"250.0\"), Decimal(\"285.2\"), Decimal(\"312.4\"),\n", + " Decimal(\"332.1\"), Decimal(\"346.5\"), Decimal(\"356.2\"), Decimal(\"363.0\"), Decimal(\"367.5\"), Decimal(\"370.4\"),\n", + " Decimal(\"372.2\"), Decimal(\"373.4\"), Decimal(\"374.0\"), Decimal(\"374.5\"), Decimal(\"374.8\"), Decimal(\"375.0\")\n", + "]\n", + "rubber_rho = Decimal(\"1374\")\n", + "\n", + "# 有机玻璃 (PMMA) 数据\n", + "pmma_center_uV = [\n", + " Decimal(\"18.0\"), Decimal(\"42.1\"), Decimal(\"68.5\"), Decimal(\"94.2\"), Decimal(\"118.0\"), Decimal(\"139.5\"),\n", + " Decimal(\"158.0\"), Decimal(\"173.2\"), Decimal(\"185.5\"), Decimal(\"195.0\"), Decimal(\"202.1\"), Decimal(\"207.2\"),\n", + " Decimal(\"210.5\"), Decimal(\"212.8\"), Decimal(\"214.1\"), Decimal(\"214.9\"), Decimal(\"215.3\"), Decimal(\"215.5\")\n", + "]\n", + "pmma_heating_uV = [\n", + " Decimal(\"92.0\"), Decimal(\"162.0\"), Decimal(\"218.5\"), Decimal(\"263.0\"), Decimal(\"298.0\"), Decimal(\"324.5\"),\n", + " Decimal(\"344.0\"), Decimal(\"358.0\"), Decimal(\"368.0\"), Decimal(\"374.8\"), Decimal(\"379.2\"), Decimal(\"382.1\"),\n", + " Decimal(\"383.8\"), Decimal(\"384.9\"), Decimal(\"385.5\"), Decimal(\"385.9\"), Decimal(\"386.1\"), Decimal(\"386.3\")\n", + "]\n", + "pmma_rho = Decimal(\"1196\")\n", + "\n", + "print(\"两组材料测量数据已载入。\")" + ], + "id": "c57eb22539adb11f" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3. 稳态分析、导热系数与比热容完整计算" + ], + "id": "5e74a7899623ab0a" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def analyze_material(mat_name, center_uV, heating_uV, rho):\n", + " n = len(center_uV)\n", + " S_uV = S_sensitivity * Decimal(\"1000\")\n", + " delta_uV = [heating_uV[i] - center_uV[i] for i in range(n)]\n", + " \n", + " # 取最后 4 个点作为稳态区间\n", + " steady_delta_uV = delta_uV[-4:]\n", + " delta_U_mean, u_delta_U = calculate_stats(steady_delta_uV, delta_uV_inst)[:2]\n", + " delta_T_steady = delta_U_mean / S_uV\n", + " \n", + " # 1. 热流密度 qc 与导热系数 λ\n", + " q_c = V_heat**2 / (Decimal(\"2\") * F * r)\n", + " lam = q_c * R_thickness / (Decimal(\"2\") * delta_T_steady)\n", + " \n", + " # 2. 冷却曲线拟合比热容 c\n", + " # 取最后 5 分钟中心面电压降温模拟冷却段\n", + " cooling_uV = list(reversed(center_uV[-5:]))\n", + " x_cool = [Decimal(str(i * 60)) for i in range(len(cooling_uV))]\n", + " y_cool = [uv / S_uV for uv in cooling_uV]\n", + " k_cool, _, _, u_k_cool = linear_regression(x_cool, y_cool)\n", + " dT_dtau_cool = abs(k_cool)\n", + " \n", + " m_sample = Decimal(\"2\") * rho * F * R_thickness\n", + " delta_m = m_sample * Decimal(str(math.sqrt(float((delta_F / F)**2 + (delta_R / R_thickness)**2))))\n", + " c_val = (Decimal(\"2\") * q_c * F / (m_sample * dT_dtau_cool)) if dT_dtau_cool != 0 else Decimal(\"0\")\n", + " \n", + " # 3. 不确定度合成\n", + " sqrt3 = Decimal(str(math.sqrt(3)))\n", + " ur_V = (delta_V / sqrt3) / V_heat\n", + " ur_F = (delta_F / sqrt3) / F\n", + " ur_r = (delta_r / sqrt3) / r\n", + " ur_R = (delta_R / sqrt3) / R_thickness\n", + " ur_m = (delta_m / sqrt3) / m_sample\n", + " ur_deltaT = u_delta_U / abs(delta_U_mean)\n", + " ur_cool = (u_k_cool / dT_dtau_cool) if dT_dtau_cool != 0 else Decimal(\"0\")\n", + " \n", + " ur_lam_sq = (Decimal(\"2\") * ur_V)**2 + ur_R**2 + ur_F**2 + ur_r**2 + ur_deltaT**2\n", + " ur_lam = Decimal(str(math.sqrt(float(ur_lam_sq))))\n", + " u_lam = lam * ur_lam\n", + " \n", + " ur_c_sq = (Decimal(\"2\") * ur_V)**2 + ur_r**2 + ur_m**2 + ur_cool**2\n", + " ur_c = Decimal(str(math.sqrt(float(ur_c_sq))))\n", + " u_c = c_val * ur_c\n", + " \n", + " lam_final, u_lam_final = scientific_round(lam, u_lam)\n", + " c_final, u_c_final = scientific_round(c_val, u_c)\n", + " \n", + " print(\"=\" * 55)\n", + " print(f\" {mat_name} 计算与不确定度结果 \")\n", + " print(\"=\" * 55)\n", + " print(f\"稳态温差 ΔT : {float(delta_T_steady):.3f} K (ΔU={float(delta_U_mean):.1f} uV)\")\n", + " print(f\"热流密度 q_c : {float(q_c):.4f} W/m²\")\n", + " print(f\"冷却速率 |κ| : {float(dT_dtau_cool):.6f} K/s\")\n", + " print(f\"样品质量 m : {float(m_sample):.6f} kg\")\n", + " print(\"-\" * 55)\n", + " print(f\"相对不确定度 u_r(λ): {float(ur_lam)*100:.2f}%\")\n", + " print(f\"相对不确定度 u_r(c): {float(ur_c)*100:.2f}%\")\n", + " print(\"-\" * 55)\n", + " print(f\"★ 导热系数 λ : ({lam_final} ± {u_lam_final}) W/(m·K)\")\n", + " print(f\"★ 比热容 c : ({c_final} ± {u_c_final}) J/(kg·K)\")\n", + " print(\"=\" * 55 + \"\\n\")\n", + "\n", + "analyze_material(\"橡胶 (Rubber)\", rubber_center_uV, rubber_heating_uV, rubber_rho)\n", + "analyze_material(\"有机玻璃 (PMMA)\", pmma_center_uV, pmma_heating_uV, pmma_rho)" + ], + "id": "fc2830f7af827cc7" + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Thermal conductivity.py b/python/Thermal conductivity.py similarity index 100% rename from Thermal conductivity.py rename to python/Thermal conductivity.py diff --git a/python/Viscosity coefficient.ipynb b/python/Viscosity coefficient.ipynb new file mode 100644 index 0000000..af03ed2 --- /dev/null +++ b/python/Viscosity coefficient.ipynb @@ -0,0 +1,291 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 蓖麻油粘滞系数计算 (斯托克斯定律)\n", + "\n", + "本项目用于计算落球法测量蓖麻油粘滞系数实验的数据处理与不确定度评定。\n", + "\n", + "---\n", + "### 实验原理与计算公式\n", + "\n", + "#### 1. 粘滞系数计算公式(考虑管壁修正)\n", + "小球在液体中受重力、浮力与斯托克斯粘滞阻力作用达到终端匀速下落速度 $v = l/t$。考虑到圆筒玻璃管壁的有限边界效应,引入管壁修正因子 $(1 + 2.4 d/D)$:\n", + "$$\\eta = \\frac{(\\rho - \\rho_0) g d^2 t}{18 l \\left(1 + 2.4 \\frac{d}{D}\\right)}$$\n", + "\n", + "* $\\rho$:小球密度(钢球通常取 $7800\\,\\mathrm{kg/m^3}$)\n", + "* $\\rho_0$:液体(蓖麻油)密度(室温下约 $960\\,\\mathrm{kg/m^3}$)\n", + "* $g$:重力加速度(取 $9.80\\,\\mathrm{m/s^2}$)\n", + "* $d$:小球直径\n", + "* $D$:玻璃管内径\n", + "* $l$:小球下落距离\n", + "* $t$:下落平均时间\n", + "\n", + "#### 2. 雷诺数($Re$)判断与奥辛(Oseen)修正\n", + "斯托克斯公式成立的条件为层流状态且惯性力远小于粘性阻力,其雷诺数定义为:\n", + "$$Re = \\frac{\\rho_0 v d}{\\eta}$$\n", + "* 当 $Re \\le 0.1$ 时,斯托克斯定律完全适用,无需修正。\n", + "* 当 $0.1 < Re \\le 1.0$ 时,流场出现微小扰动,采用奥辛公式进行一级修正:\n", + "$$\\eta_{\\mathrm{corrected}} = \\frac{\\eta}{1 + \\frac{3}{16} Re}$$\n", + "\n", + "#### 3. 不确定度传递公式\n", + "假定 $\\rho, \\rho_0, g$ 的误差可忽略不计,由误差合成法则,粘滞系数的相对不确定度传递公式为:\n", + "$$u_r(\\eta) = \\frac{u_\\eta}{\\eta} = \\sqrt{ \\left(2 \\frac{u_d}{d}\\right)^2 + \\left(\\frac{u_t}{t}\\right)^2 + \\left(\\frac{u_l}{l}\\right)^2 + \\left(\\frac{u_D}{D}\\right)^2 }$$\n", + "\n", + "绝对不确定度为:\n", + "$$u_\\eta = \\eta \\cdot u_r(\\eta)$$\n", + "\n", + "其中各物理量 $X$ 的不确定度按 A 类与 B 类合成:\n", + "$$u_X = \\sqrt{u_{A,X}^2 + u_{B,X}^2}, \\quad u_{A,X} = \\frac{s_X}{\\sqrt{n}}, \\quad u_{B,X} = \\frac{\\Delta_{\\mathrm{inst}}}{\\sqrt{3}}$$" + ], + "id": "e5127401d3a8f15d" + }, + { + "cell_type": "code", + "metadata": { + "ExecuteTime": { + "end_time": "2026-09-22T07:54:14.369907400Z", + "start_time": "2026-09-22T07:54:14.243997100Z" + } + }, + "source": [ + "import math\n", + "from decimal import Decimal\n", + "from python.utils import scientific_round, calculate_stats\n", + "\n", + "print(\"工具函数与模块加载完成。\")" + ], + "id": "ca5f57b71e825238", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "工具函数与模块加载完成。\n" + ] + } + ], + "execution_count": 1 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1. 实验参数与数据输入\n", + "> **提示**:可直接在下方单元格中修改小球下落时间测量值 `t_raw_data` 以及各项仪器参数,无需命令行逐次敲入。" + ], + "id": "4f13dab8eefdafa4" + }, + { + "cell_type": "code", + "metadata": { + "ExecuteTime": { + "end_time": "2026-09-22T07:54:26.075852500Z", + "start_time": "2026-09-22T07:54:26.004272300Z" + } + }, + "source": [ + "# --- 物理常数与材料密度 ---\n", + "G = Decimal(\"9.80\") # 重力加速度 (m/s^2)\n", + "RHO_BALL = Decimal(\"7800\") # 小球(钢球)密度 (kg/m^3)\n", + "RHO_OIL = Decimal(\"960\") # 蓖麻油密度 (kg/m^3)\n", + "\n", + "# --- 几何与仪器参数 (单位: mm / cm) ---\n", + "D_mean = Decimal(\"20.0\") # 玻璃管直径 D (mm)\n", + "d_mean = Decimal(\"1.0\") # 小球直径 d (mm)\n", + "l_mean = Decimal(\"20.0\") # 下落距离 l (cm)\n", + "\n", + "# --- 仪器允差 (仪器误差) Δ_inst ---\n", + "delta_D = Decimal(\"0\") # D 的仪器误差 (mm),如未单独评估可设为 0\n", + "delta_d = Decimal(\"0\") # d 的仪器误差 (mm),如未单独评估可设为 0\n", + "delta_l = Decimal(\"0\") # l 的仪器误差 (mm),如未单独评估可设为 0\n", + "delta_t = Decimal(\"0.01\") # 秒表仪器误差 (s)\n", + "\n", + "# --- 5次下落时间测量值 (s) ---\n", + "# 请在此填入实际实验数据:\n", + "t_raw_data = [Decimal(\"12.45\"), Decimal(\"12.38\"), Decimal(\"12.50\"), Decimal(\"12.42\"), Decimal(\"12.48\")]\n", + "\n", + "print(f\"参数设置完成:\")\n", + "print(f\"- 玻璃管直径 D: {D_mean} mm\")\n", + "print(f\"- 小球直径 d: {d_mean} mm\")\n", + "print(f\"- 下落距离 l: {l_mean} cm\")\n", + "print(f\"- 时间测量值 t: {[float(x) for x in t_raw_data]} s\")" + ], + "id": "b222d81b56a956be", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "参数设置完成:\n", + "- 玻璃管直径 D: 20.0 mm\n", + "- 小球直径 d: 1.0 mm\n", + "- 下落距离 l: 20.0 cm\n", + "- 时间测量值 t: [12.45, 12.38, 12.5, 12.42, 12.48] s\n" + ] + } + ], + "execution_count": 2 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2. 测量数据统计与不确定度计算" + ], + "id": "b96c99013eae004b" + }, + { + "cell_type": "code", + "metadata": { + "ExecuteTime": { + "end_time": "2026-09-22T07:54:28.891288400Z", + "start_time": "2026-09-22T07:54:28.810903200Z" + } + }, + "source": [ + "# 长度单位换算为 mm 以便统一不确定度计算\n", + "l_mean_mm = l_mean * Decimal(\"10\")\n", + "\n", + "# 单次测量参数的 B 类不确定度 (u = Δ_inst / √3)\n", + "D_u = delta_D / Decimal(str(math.sqrt(3)))\n", + "d_u = delta_d / Decimal(str(math.sqrt(3)))\n", + "l_u = delta_l / Decimal(str(math.sqrt(3)))\n", + "\n", + "# 5次时间测量的统计量 (平均值、合成不确定度、A类、B类)\n", + "t_mean, t_u, t_ua, t_ub, t_s = calculate_stats(t_raw_data, delta_t)\n", + "\n", + "print(\"--- 时间统计分析 ---\")\n", + "print(f\"时间平均值 t_mean : {t_mean:.4f} s\")\n", + "print(f\"样本标准差 s(t) : {t_s:.4f} s\")\n", + "print(f\"A 类不确定度 u_A(t): {t_ua:.4f} s\")\n", + "print(f\"B 类不确定度 u_B(t): {t_ub:.4f} s\")\n", + "print(f\"合成不确定度 u(t) : {t_u:.4f} s\")" + ], + "id": "b609644356d56555", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "--- 时间统计分析 ---\n", + "时间平均值 t_mean : 12.4460 s\n", + "样本标准差 s(t) : 0.0477 s\n", + "A 类不确定度 u_A(t): 0.0214 s\n", + "B 类不确定度 u_B(t): 0.0058 s\n", + "合成不确定度 u(t) : 0.0221 s\n" + ] + } + ], + "execution_count": 3 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3. 粘滞系数计算、雷诺数校验与修约输出" + ], + "id": "652642328be1ab03" + }, + { + "cell_type": "code", + "metadata": { + "ExecuteTime": { + "end_time": "2026-09-22T07:54:30.748035800Z", + "start_time": "2026-09-22T07:54:30.683789300Z" + } + }, + "source": [ + "# 统一转换为国际单位制 (m, kg, s)\n", + "D_m = D_mean / Decimal(\"1000\")\n", + "d_m = d_mean / Decimal(\"1000\")\n", + "l_m = l_mean_mm / Decimal(\"1000\")\n", + "\n", + "# --- 1. 斯托克斯粘滞系数计算 (带管壁修正) ---\n", + "correction_factor = Decimal(\"1\") + Decimal(\"2.4\") * (d_m / D_m)\n", + "numerator = (RHO_BALL - RHO_OIL) * G * (d_m**2) * t_mean\n", + "denominator = Decimal(\"18\") * l_m * correction_factor\n", + "eta_val = numerator / denominator\n", + "\n", + "# --- 2. 下落终端速度与雷诺数 Re 校验 ---\n", + "v_val = l_m / t_mean\n", + "Re = RHO_OIL * v_val * d_m / eta_val\n", + "\n", + "# --- 3. 雷诺数判断与 Oseen 修正 ---\n", + "if Re > Decimal(\"0.1\"):\n", + " eta_corrected = eta_val / (Decimal(\"1\") + Decimal(\"3\") / Decimal(\"16\") * Re)\n", + " oseen_applied = True\n", + "else:\n", + " eta_corrected = eta_val\n", + " oseen_applied = False\n", + "\n", + "# --- 4. 相对不确定度与绝对不确定度 ---\n", + "# 各相对不确定度平方和项: (2*ud/d)^2 + (ut/t)^2 + (ul/l)^2 + (uD/D)^2\n", + "term_d = (Decimal(\"2\") * d_u / d_mean) if d_mean != 0 else Decimal(\"0\")\n", + "term_t = (t_u / t_mean) if t_mean != 0 else Decimal(\"0\")\n", + "term_l = (l_u / l_mean_mm) if l_mean_mm != 0 else Decimal(\"0\")\n", + "term_D = (D_u / D_mean) if D_mean != 0 else Decimal(\"0\")\n", + "\n", + "rel_u_sq = term_d**2 + term_t**2 + term_l**2 + term_D**2\n", + "rel_u = Decimal(str(math.sqrt(float(rel_u_sq))))\n", + "eta_u = eta_corrected * rel_u\n", + "\n", + "# --- 5. 按照科学修约规范对齐保留有效数字 ---\n", + "eta_final, u_final = scientific_round(eta_corrected, eta_u)\n", + "\n", + "# --- 6. 实验报告格式化输出 ---\n", + "print(\"=\" * 45)\n", + "print(\" 实 验 计 算 结 果 \")\n", + "print(\"=\" * 45)\n", + "print(f\"下落平均时间 t : {t_mean:.3f} ± {t_u:.3f} s\")\n", + "print(f\"小球下落速度 v : {v_val:.5f} m/s\")\n", + "print(\"-\" * 45)\n", + "print(f\"雷诺数 Re : {Re:.4f}\")\n", + "if not oseen_applied:\n", + " print(\"判定结论 : Re <= 0.1,满足斯托克斯层流条件,无需 Oseen 修正。\")\n", + "else:\n", + " print(\"判定结论 : Re > 0.1,已自动应用 Oseen 修正公式。\")\n", + " print(f\"修正前粘滞系数 η0 : {eta_val:.4f} Pa·s\")\n", + "print(\"-\" * 45)\n", + "print(f\"相对不确定度 u_r : {rel_u * 100:.2f}%\")\n", + "print(f\"绝对不确定度 u_η : {eta_u:.4f} Pa·s\")\n", + "print(\"-\" * 45)\n", + "print(f\"最终结果表示 : η = {eta_final} ± {u_final} Pa·s\")\n", + "print(\"=\" * 45)" + ], + "id": "5abf7dd1e1769bf6", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "=============================================\n", + " 实 验 计 算 结 果 \n", + "=============================================\n", + "下落平均时间 t : 12.446 ± 0.022 s\n", + "小球下落速度 v : 0.01607 m/s\n", + "---------------------------------------------\n", + "雷诺数 Re : 0.0746\n", + "判定结论 : Re <= 0.1,满足斯托克斯层流条件,无需 Oseen 修正。\n", + "---------------------------------------------\n", + "相对不确定度 u_r : 0.18%\n", + "绝对不确定度 u_η : 0.0004 Pa·s\n", + "---------------------------------------------\n", + "最终结果表示 : η = 0.2069 ± 0.0004 Pa·s\n", + "=============================================\n" + ] + } + ], + "execution_count": 4 + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Viscosity coefficient.py b/python/Viscosity coefficient.py similarity index 100% rename from Viscosity coefficient.py rename to python/Viscosity coefficient.py diff --git a/python/Young's modulus.ipynb b/python/Young's modulus.ipynb new file mode 100644 index 0000000..15c4b44 --- /dev/null +++ b/python/Young's modulus.ipynb @@ -0,0 +1,201 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 杨氏模量计算器 (光杠杆 & 逐差法)\n", + "\n", + "本 Notebook 用于通过光杠杆镜尺法测量金属丝在拉伸过程中的微小伸长量,利用**逐差法**计算金属丝的杨氏弹性模量 $E$,并进行完整的各分量不确定度传递与规范修约。\n", + "\n", + "---\n", + "### 实验原理与数学公式\n", + "\n", + "#### 1. 杨氏模量计算公式\n", + "根据胡克定律与光杠杆光学放大原理:\n", + "$$E = \\frac{F/S}{\\Delta L / L} = \\frac{4 F L}{\\pi d^2 \\Delta L}$$\n", + "设单个砝码质量为 $m$,增重过程每加 4 个砝码(通过逐差法将 8 个加重、减重平均刻度值 $n_i$ 隔 4 项逐差)产生的刻度尺平均位移为 $\\Delta n_{\\mathrm{avg}}$,光杠杆后足长为 $b$,反射镜至标尺距离为 $D$,则伸长量 $\\Delta L = \\frac{b \\Delta n_{\\mathrm{avg}}}{2 D}$,代入得:\n", + "$$E = \\frac{32 m g L D}{\\pi d^2 b \\Delta n_{\\mathrm{avg}}}$$\n", + "* $L$:金属丝原长\n", + "* $D$:光杠杆镜面到刻度尺标尺距离\n", + "* $d$:金属丝直径(千分尺测量)\n", + "* $b$:光杠杆常数(刀口到后足支点的垂直距离)\n", + "* $m$:单组砝码质量;$g$:重力加速度\n", + "* $\\Delta n_{\\mathrm{avg}}$:4 个砝码引起的标尺平均位移\n", + "\n", + "#### 2. 不确定度传递公式\n", + "* 相对不确定度平方和:\n", + "$$u_r(E) = \\frac{u_E}{E} = \\sqrt{ \\left(\\frac{u_L}{L}\\right)^2 + \\left(\\frac{u_D}{D}\\right)^2 + \\left(2\\frac{u_d}{d}\\right)^2 + \\left(\\frac{u_b}{b}\\right)^2 + \\left(\\frac{u_{\\Delta n}}{\\Delta n_{\\mathrm{avg}}}\\right)^2 }$$\n", + "* 各直接测量量的不确定度按 A 类与仪器 B 类合成:\n", + "$$u_X = \\sqrt{u_{A,X}^2 + u_{B,X}^2}, \\quad u_{A,X} = \\frac{s_X}{\\sqrt{n}}, \\quad u_{B,X} = \\frac{\\Delta_{\\mathrm{inst}}}{\\sqrt{3}}$$" + ], + "id": "5763889c1dbb6480" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "from decimal import Decimal\n", + "from python.utils import scientific_round, calculate_stats\n", + "\n", + "print(\"杨氏模量模块加载完成。\")" + ], + "id": "a70d2b4077726fc0" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1. 实验参数、几何尺寸与标尺读数输入\n", + "> **提示**:可直接在下方单元格中修改各几何长度的多次测量列表以及加减砝码的刻度尺读数矩阵。" + ], + "id": "b3fe9624ea56f3d0" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# --- 常量设置 ---\n", + "M_WEIGHT = Decimal(\"0.350\") # 单个砝码质量 350g = 0.350kg\n", + "G = Decimal(\"9.80\") # 重力加速度 m/s^2\n", + "PI = Decimal(str(math.pi))\n", + "\n", + "# --- 几何长度测量值 (单位: mm,各6次) ---\n", + "L_vals = [Decimal(\"905.0\"), Decimal(\"905.5\"), Decimal(\"904.5\"), Decimal(\"905.0\"), Decimal(\"905.2\"), Decimal(\"904.8\")]\n", + "D_vals = [Decimal(\"1502.0\"), Decimal(\"1501.5\"), Decimal(\"1502.5\"), Decimal(\"1502.0\"), Decimal(\"1501.8\"), Decimal(\"1502.2\")]\n", + "d_vals = [Decimal(\"0.702\"), Decimal(\"0.704\"), Decimal(\"0.701\"), Decimal(\"0.703\"), Decimal(\"0.702\"), Decimal(\"0.703\")]\n", + "b_vals = [Decimal(\"68.20\"), Decimal(\"68.22\"), Decimal(\"68.18\"), Decimal(\"68.20\"), Decimal(\"68.24\"), Decimal(\"68.20\")]\n", + "\n", + "# --- 仪器误差限 Δ_inst (mm) ---\n", + "delta_L = Decimal(\"1.0\")\n", + "delta_D = Decimal(\"1.0\")\n", + "delta_d = Decimal(\"0.005\")\n", + "delta_b = Decimal(\"0.02\")\n", + "delta_n_inst = Decimal(\"0.5\")\n", + "\n", + "# --- 标尺读数 n (0~7 个砝码,单位: mm;每项为 [加重读数, 减重读数]) ---\n", + "n_readings = [\n", + " [Decimal(\"15.2\"), Decimal(\"15.4\")], # 0个\n", + " [Decimal(\"22.1\"), Decimal(\"22.3\")], # 1个\n", + " [Decimal(\"29.0\"), Decimal(\"29.1\")], # 2个\n", + " [Decimal(\"35.8\"), Decimal(\"36.0\")], # 3个\n", + " [Decimal(\"42.7\"), Decimal(\"42.9\")], # 4个\n", + " [Decimal(\"49.5\"), Decimal(\"49.7\")], # 5个\n", + " [Decimal(\"56.3\"), Decimal(\"56.5\")], # 6个\n", + " [Decimal(\"63.1\"), Decimal(\"63.2\")], # 7个\n", + "]\n", + "\n", + "print(\"实验数据加载完成。\")" + ], + "id": "b6816ecdb2083f36" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2. 逐差法处理标尺读数" + ], + "id": "7cecf972d1d0806c" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 计算各加重减重的平均读数 n_i\n", + "n_means = [(row[0] + row[1]) / Decimal(\"2\") for row in n_readings]\n", + "\n", + "# 逐差法计算 Δn (隔 4 项相减,相当于 4 个砝码的位移)\n", + "dn_list = []\n", + "print(\"--- 逐差过程 (Δn_j = n_{j+4} - n_j) ---\")\n", + "for j in range(4):\n", + " diff = n_means[j + 4] - n_means[j]\n", + " dn_list.append(diff)\n", + " print(f\"Δn_{j} = n_{j+4} - n_{j} = {diff:.2f} mm\")\n", + "\n", + "dn_mean = sum(dn_list) / Decimal(\"4\")\n", + "variance_dn = sum((x - dn_mean)**2 for x in dn_list) / Decimal(\"3\")\n", + "s_dn = Decimal(str(math.sqrt(float(variance_dn))))\n", + "u_a_dn = s_dn / Decimal(str(math.sqrt(4)))\n", + "u_b_dn = delta_n_inst / Decimal(str(math.sqrt(3)))\n", + "dn_u = Decimal(str(math.sqrt(float(u_a_dn**2 + u_b_dn**2))))\n", + "\n", + "print(f\"\\nΔn 平均值 : {dn_mean:.3f} ± {dn_u:.3f} mm\")" + ], + "id": "67a1199d24b1f978" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3. 杨氏模量计算与不确定度评定" + ], + "id": "66279721f812b579" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 几何参数的均值与不确定度\n", + "L_mean, L_u = calculate_stats(L_vals, delta_L)[:2]\n", + "D_mean, D_u = calculate_stats(D_vals, delta_D)[:2]\n", + "d_mean, d_u = calculate_stats(d_vals, delta_d)[:2]\n", + "b_mean, b_u = calculate_stats(b_vals, delta_b)[:2]\n", + "\n", + "# 转换为国际单位制 (m)\n", + "L_m = L_mean / Decimal(\"1000\")\n", + "D_m = D_mean / Decimal(\"1000\")\n", + "d_m = d_mean / Decimal(\"1000\")\n", + "b_m = b_mean / Decimal(\"1000\")\n", + "dn_m = dn_mean / Decimal(\"1000\")\n", + "\n", + "# E = (32 * m * g * L * D) / (pi * d^2 * b * Δn)\n", + "numerator = Decimal(\"32\") * M_WEIGHT * G * L_m * D_m\n", + "denominator = PI * (d_m**2) * b_m * dn_m\n", + "E_val = numerator / denominator\n", + "\n", + "# 不确定度合成传递\n", + "rel_u_sq = (L_u/L_mean)**2 + (D_u/D_mean)**2 + (Decimal(\"2\")*d_u/d_mean)**2 + (b_u/b_mean)**2 + (dn_u/dn_mean)**2\n", + "rel_u = Decimal(str(math.sqrt(float(rel_u_sq))))\n", + "E_u = E_val * rel_u\n", + "\n", + "# 科学修约 (以 10^11 Pa 数量级表示)\n", + "E_11 = E_val / Decimal(\"1e11\")\n", + "E_u_11 = E_u / Decimal(\"1e11\")\n", + "E_final, u_final = scientific_round(E_11, E_u_11)\n", + "\n", + "print(\"=\" * 45)\n", + "print(\" 杨 氏 模 量 计 算 结 果 \")\n", + "print(\"=\" * 45)\n", + "print(f\"金属丝长度 L : {L_mean:.2f} ± {L_u:.2f} mm\")\n", + "print(f\"镜面到尺距离 D: {D_mean:.2f} ± {D_u:.2f} mm\")\n", + "print(f\"金属丝直径 d : {d_mean:.4f} ± {d_u:.4f} mm\")\n", + "print(f\"光杠杆臂长 b : {b_mean:.3f} ± {b_u:.3f} mm\")\n", + "print(f\"标尺位移 Δn : {dn_mean:.3f} ± {dn_u:.3f} mm (4个砝码对应位移)\")\n", + "print(\"-\" * 45)\n", + "print(f\"杨氏模量 E (原始): {E_val:.4e} Pa\")\n", + "print(f\"相对不确定度 u_r : {rel_u * 100:.2f}%\")\n", + "print(f\"绝对不确定度 u_E : {E_u:.4e} Pa\")\n", + "print(\"-\" * 45)\n", + "print(f\"★ 最终结果表示 : E = {E_final} ± {u_final} × 10^11 Pa\")\n", + "print(\"=\" * 45)" + ], + "id": "d4864c1fd01816d6" + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Young's modulus.py b/python/Young's modulus.py similarity index 100% rename from Young's modulus.py rename to python/Young's modulus.py diff --git a/python/degree.ipynb b/python/degree.ipynb new file mode 100644 index 0000000..fb150e5 --- /dev/null +++ b/python/degree.ipynb @@ -0,0 +1,137 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 角度直接测量与不确定度评定\n", + "\n", + "本 Notebook 用于对分光计等仪器测得的角度多次测量数据(支持度分格式 `度.分`)进行不确定度合成与保留一位有效数字的规范修约输出。\n", + "\n", + "---\n", + "### 实验原理与数学公式\n", + "\n", + "#### 1. 角度单位换算与平均值计算\n", + "角度采用“分(arcminute)”为内部基准单位换算:\n", + "$$D^\\circ M' = D \\times 60' + M'$$\n", + "对 $n$ 次测量值 $\\theta_1, \\theta_2, \\dots, \\theta_n$(以分为单位):\n", + "$$\\bar{\\theta} = \\frac{1}{n} \\sum_{i=1}^n \\theta_i$$\n", + "$$s = \\sqrt{\\frac{1}{n-1} \\sum_{i=1}^n (\\theta_i - \\bar{\\theta})^2}$$\n", + "\n", + "#### 2. 不确定度评定公式\n", + "* **A 类不确定度**:\n", + "$$u_A = \\frac{s}{\\sqrt{n}}$$\n", + "* **B 类不确定度**(分光计游标盘分度值误差 $\\Delta_{\\mathrm{inst}}$ 通常为 $1'$):\n", + "$$u_B = \\frac{\\Delta_{\\mathrm{inst}}}{\\sqrt{3}}$$\n", + "* **合成不确定度**:\n", + "$$u = \\sqrt{u_A^2 + u_B^2}$$\n", + "\n", + "#### 3. 结果修约规则\n", + "不确定度 $u$ 按照“四舍六入五凑偶”保留一位有效数字,角度平均值末位与不确定度对齐,并还原输出为度分格式 $D^\\circ M'$。" + ], + "id": "b15a8bae68dd9273" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "from decimal import Decimal\n", + "from python.utils import parse_angle_to_minutes, format_minutes_as_angle, scientific_round\n", + "\n", + "print(\"角度换算与修约模块加载完成。\")" + ], + "id": "afc36384df4dec97" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1. 角度测量数据与分度值输入\n", + "> **输入格式**:字符串 `'度.分'`(例如 `'120.15'` 代表 $120^\\circ 15'$)。" + ], + "id": "2d31b33f8ef516db" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 测量数据列表 (度.分 格式)\n", + "angle_data = [\"120.15\", \"120.16\", \"120.14\", \"120.15\", \"120.17\", \"120.15\"]\n", + "\n", + "# 仪器分度值 (单位: 分 ')\n", + "res_str = \"1\"\n", + "\n", + "print(f\"输入角度数据: {angle_data}\")\n", + "print(f\"仪器分度值: {res_str} '\")" + ], + "id": "50959966653d1094" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2. 统计计算与修约输出" + ], + "id": "d91d9838fb92eb89" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 转换为内部单位“分”\n", + "data_minutes = [parse_angle_to_minutes(s) for s in angle_data]\n", + "res = Decimal(res_str)\n", + "n = len(data_minutes)\n", + "\n", + "# 1. 平均值与标准差\n", + "mean_minutes = sum(data_minutes) / n\n", + "variance = sum((x - mean_minutes) ** 2 for x in data_minutes) / (n - 1) if n > 1 else Decimal(\"0\")\n", + "s = Decimal(str(math.sqrt(float(variance))))\n", + "\n", + "# 2. A类、B类及合成不确定度\n", + "u_A = s / Decimal(str(math.sqrt(n))) if n > 1 else Decimal(\"0\")\n", + "u_B = res / Decimal(str(math.sqrt(3)))\n", + "u = Decimal(str(math.sqrt(float(u_A**2 + u_B**2))))\n", + "\n", + "# 3. 科学修约与格式还原\n", + "u_float = float(u)\n", + "if u_float == 0:\n", + " final_result_str = f\"θ = {format_minutes_as_angle(mean_minutes)} ± 0'\"\n", + "else:\n", + " first_digit_pos = math.floor(math.log10(u_float))\n", + " prec = Decimal('1e' + str(first_digit_pos))\n", + " mean_final_val, u_final = scientific_round(mean_minutes, u)\n", + " mean_str = format_minutes_as_angle(mean_final_val, prec)\n", + " final_result_str = f\"θ = {mean_str} ± {u_final}'\"\n", + "\n", + "print(\"=\" * 45)\n", + "print(\" 角 度 测 量 不 确 定 度 分 析 \")\n", + "print(\"=\" * 45)\n", + "print(f\"测量次数 n : {n}\")\n", + "print(f\"角度平均值 : {format_minutes_as_angle(mean_minutes)} ({mean_minutes:.4f}')\")\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(\"-\" * 45)\n", + "print(f\"★ 最终修约结果 : {final_result_str}\")\n", + "print(\"=\" * 45)" + ], + "id": "ef15abbb8e0d991c" + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/degree.py b/python/degree.py similarity index 100% rename from degree.py rename to python/degree.py diff --git a/python/lenth.ipynb b/python/lenth.ipynb new file mode 100644 index 0000000..cdb77e5 --- /dev/null +++ b/python/lenth.ipynb @@ -0,0 +1,139 @@ +{ + "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 +} diff --git a/lenth.py b/python/lenth.py similarity index 100% rename from lenth.py rename to python/lenth.py diff --git a/utils/__init__.py b/python/utils/__init__.py similarity index 68% rename from utils/__init__.py rename to python/utils/__init__.py index c577f60..630bb85 100644 --- a/utils/__init__.py +++ b/python/utils/__init__.py @@ -1,14 +1,14 @@ -from utils.formatting import ( +from python.utils.formatting import ( get_decimal_places, scientific_round, parse_angle_to_minutes, format_minutes_as_angle ) -from utils.stats import ( +from python.utils.stats import ( calculate_stats, linear_regression ) -from utils.io import ( +from python.utils.io import ( input_float, input_decimal, input_int, diff --git a/utils/formatting.py b/python/utils/formatting.py similarity index 100% rename from utils/formatting.py rename to python/utils/formatting.py diff --git a/utils/io.py b/python/utils/io.py similarity index 100% rename from utils/io.py rename to python/utils/io.py diff --git a/utils/stats.py b/python/utils/stats.py similarity index 100% rename from utils/stats.py rename to python/utils/stats.py