內容僅供參考,如有錯誤,歡迎指正,如有疑問,歡迎交流。?
因為我不會Excel所以只能用Python來處理
祝大家早日擺脫物理實驗的苦海
用到的一些方法
PCHIP (分段三次埃爾米特插值多項式)
因為實驗時記錄的數據比較少,記錄的也比較隨便,直接將這些數據連起來的話畫出的圖像十分的直,而伏安特性曲線顧名思義應該是一條曲線,因此我們需要進行數據擬合,但是呢我們發現直接進行數據擬合會出現下面的情況,即在某些區間內,電壓上升電流反而下降了,這很不合理,所以我讓AI幫我想到了PCHIP插值法進行數據點的擴展,具體是啥我也不知道,反正用就完了。
利用導數找拐點
看了示范報告,一開始把零點當成拐點算了,爆炸了
找拐點的方法我試了好多種,二階導等于零、一階導的極值、一階導作差、一階導和各種閾值比較等等,最后我用的是區間斜率與整體斜率的比較,也算是一階導和閾值比較的一種吧。
這些方法的核心思想都是在伏安特性曲線上找到電流變化陡峭的拐點,并將其對應的電壓作為截止電壓,關鍵是怎樣才算陡峭。本文用到的方法中,先計算電流隨電壓的平均變化率(即平均斜率),作為基準值 threshold =(
I_max - I_min) / (U_max - U_min),隨后遍歷所有插值后的數據點,利用五點差分法計算其局部斜率,如果這個局部斜率是基準斜率的1.5倍,就將其認為是拐點,記錄并退出循環。但是因為找到了第一個就退出循環了并且“1.5倍”不一定是每組數據的最佳選擇,所以結果可能會出現較大誤差。
threshold = (max(y_dense) - min(y_dense)) / (x_max - x_min) # 導數閾值for i in range(nums_data):if (y_dense[i + 5] - y_dense[i]) / (x_dense[i + 5] - x_dense[i]) > threshold * 1.5:cutoff_voltage = x_dense[i]break
代碼
import openpyxl
import matplotlib.pyplot as plt
import numpy as np
import os
from matplotlib.ticker import FormatStrFormatter
from scipy.interpolate import PchipInterpolator
from docx import Document
from docx.shared import Inches# 設置支持中文的字體和正常顯示負號
plt.rcParams['font.sans-serif'] = ['SimHei'] # 或者 ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False# ------------------------------------------控制臺--------------------------------------------------
N = 5 # 數據組數cutoff_voltages = [] # 截止電壓 (V)
calculate = 1 # 數據計算+圖片輸出
save_to_word = 0 # 將圖片保存到 Word 文檔output_dir = r"C:\Users\86138\Desktop\這是文件夾\這是文件夾里的文件夾" # 指定圖片保存目錄
excel_file = r"C:\Users\86138\Desktop\這是文件夾\這是xlsx.xlsx" # Excel 文件路徑 (用于加載原始數據和儲存計算后的數據)
word_file = r"C:\Users\86138\Desktop\這是文件夾\這是docx.docx" # Word 文件路徑(用于讀取圖片并保存)# --------------------------------------------------------------------------------------------------auto = 0 if cutoff_voltages else 1
os.makedirs(output_dir, exist_ok=True) # 確保目標文件夾存在def Htlang():# 物理常數c = 3.0e8 # 光速,單位 m/s# 1. 加載 Excel 文件wb = openpyxl.load_workbook(excel_file)sheet = wb.active# 2. 確定數據組的行間隔(第一組從第 1 行開始,每組占 2 行)group_start_rows = [1 + i * 2 for i in range(N)] # 生成 [1, 3, 5, 7, 9]# 3. 存儲所有數據以便后續繪圖wavelengths = [] # 波長 (nm)frequencies = [] # 頻率 (10^-14 Hz)# 4. 處理每一組數據for group_idx, start_row in enumerate(group_start_rows, start=1):# 讀取表格名稱、坐標軸標簽table_name = sheet.cell(row=start_row, column=1).value # A列存儲波長信息(例如 "365nm")x_label = sheet.cell(row=start_row, column=2).value # B列第一行: 橫坐標標簽(電壓)y_label = sheet.cell(row=start_row + 1, column=2).value # B列第二行: 縱坐標標簽(電流)# **處理 y_label,將乘方變為上標**if y_label:y_label = y_label.replace("10-10", r"$10^{-10}$") \.replace("10-11", r"$10^{-11}$") \.replace("10-12", r"$10^{-12}$")# 讀取電壓與電流數據(從 C 到 L 列)voltage_data = []current_data = []for col in range(3, 13): # C(3)到L(12)v = sheet.cell(row=start_row, column=col).value # 電壓數據i = sheet.cell(row=start_row + 1, column=col).value # 電流數據voltage_data.append(v)current_data.append(i)# 計算頻率# 從 table_name 中去除末尾兩個字符(例如 "nm")轉換為 int 得到波長wavelength_nm = int(table_name[:-2])wavelength_m = wavelength_nm * 1e-9 # 轉換為 mfrequency = c / wavelength_m # 計算頻率 (Hz)frequency_14 = frequency / 1e14 # 以 10^-14 Hz 為單位wavelengths.append(wavelength_nm)frequencies.append(frequency_14)# 過濾并排序有效數據valid_indices = [i for i, (v, i_val) in enumerate(zip(voltage_data, current_data))if v is not None and i_val is not None]x = np.array([voltage_data[i] for i in valid_indices])y = np.array([current_data[i] for i in valid_indices])# 按電壓升序排列sort_idx = np.argsort(x)x_sorted = x[sort_idx]y_sorted = y[sort_idx]if auto:cutoff_voltage = Noneelse:cutoff_voltage = cutoff_voltages[group_idx - 1]try:# 使用保形插值(保證單調性)pchip = PchipInterpolator(x_sorted, y_sorted)nums_data = 300# 生成密集采樣點,用于繪制曲線(擴展范圍為原數據范圍兩側各擴展10%)x_min, x_max = np.min(x_sorted), np.max(x_sorted)x_range = x_max - x_minx_dense = np.linspace(x_min - 0.1*x_range, x_max + 0.1*x_range, nums_data)y_dense = pchip(x_dense)if auto:threshold = (max(y_dense) - min(y_dense)) / (x_max - x_min) # 導數閾值for i in range(nums_data):if (y_dense[i + 5] - y_dense[i]) / (x_dense[i + 5] - x_dense[i]) > threshold * 1.5:cutoff_voltage = x_dense[i]breakexcept Exception as e:print(f"[{group_idx}] 插值失敗,使用線性連接: {str(e)}")cutoff_voltage = Nonex_dense = x_sortedy_dense = y_sortedif auto:if cutoff_voltage is not None:print(f"[{group_idx}] 拐點確定的截止電壓: {cutoff_voltage:.3f} V")else:print(f"[{group_idx}] 未檢測到拐點,截止電壓無法確定")cutoff_voltages.append(cutoff_voltage)# 繪制伏安特性曲線plt.figure(figsize=(10, 7))plt.plot(x_dense, y_dense, 'b-', linewidth=2, label="擬合曲線")plt.scatter(x_sorted, y_sorted, c='red', s=50, marker='o',edgecolors='k', label="實驗數據")if cutoff_voltage is not None:plt.axvline(cutoff_voltage, color='red', linestyle='--', label=f"截止電壓: {cutoff_voltage:.2f} V")plt.xlabel(x_label)plt.ylabel(y_label)plt.title(f"伏安特性曲線,波長 {table_name}")plt.grid(True)plt.legend()plt.tight_layout()output_path = os.path.join(output_dir, f"output_{group_idx}.png")plt.savefig(output_path, dpi=300)plt.close()print(f"[{group_idx}] 圖像已保存至: {output_path}")# 5. 線性擬合:截止電壓 vs 頻率(計算擬合參數及其不確定度)freq_array = np.array(frequencies)volt_array = np.array(cutoff_voltages, dtype=float)# 使用 cov=True 得到參數協方差矩陣coeffs, cov = np.polyfit(freq_array, volt_array, 1, cov=True)k, b = coeffserr_k = np.sqrt(cov[0, 0]) # 斜率 k 的標準差,即不確定度# 6. 繪制截止電壓 vs 頻率 圖(增加數據點連接線,并設置刻度格式)plt.figure(figsize=(8, 6))plt.scatter(freq_array, volt_array, color='blue', label="實驗數據")plt.plot(freq_array, volt_array, 'b-', label="數據連接線")plt.plot(freq_array, k * freq_array + b, 'r--', label=f"擬合直線: $U_s = {k:.4f}f + {b:.4f}$\n斜率不確定度: ±{err_k:.4f}")plt.xlabel(r"頻率 ($10^{-14}$ Hz)")plt.ylabel("截止電壓 (V)")plt.title("截止電壓-頻率圖線")plt.grid(True)plt.legend()ax = plt.gca()ax.xaxis.set_major_formatter(FormatStrFormatter('%.3f')) # 橫軸保留三位小數ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f')) # 縱軸保留兩位小數plt.tight_layout()output_path = os.path.join(output_dir, f"output_{N + 1}.png")plt.savefig(output_path, dpi=300)plt.close()print(f"截止電壓-頻率圖已保存至: {output_path}")# 7. 計算普朗克常數h0 = 6.64 * 10**(-34) # 公認普朗克常數 (J·s)e = 1.6 * 10**(-19) # 電子電荷量 (C)h = e * (-k) * 1e-14 # 計算出的普朗克常數 (J·s)_h = e * err_k * 1e-14 # 計算出的普朗克常數的不確定度 (J·s)Ep = (h - h0) * 100 / h0# 8. 寫入 Excel# 將標題寫入 A15, A16, A17sheet["A15"] = "波長 (nm)"sheet["A16"] = "頻率 (10^-14 Hz)"sheet["A17"] = "截止電壓 (V)"# 假設數據組數為 5,對應寫入 B 到 F 列for i, (w, f, u) in enumerate(zip(wavelengths, frequencies, cutoff_voltages)):col_letter = chr(ord('B') + i) # B, C, D, E, Fsheet[f"{col_letter}15"] = f"{w:.0f}"sheet[f"{col_letter}16"] = f"{f:.2f}"sheet[f"{col_letter}17"] = f"{u:.2f}"# 將計算結果寫入 Excel(從 A19 開始)sheet["A19"] = "h"sheet["B19"] = f"{h * 10 ** 34:.2f} × 10^(-34) J·s"sheet["A20"] = "Δh"sheet["B20"] = f"{_h * 10 ** 34:.2f} × 10^(-34) J·s"sheet["A21"] = "h±Δh"sheet["B21"] = f"{h * 10 ** 34:.2f} ± {_h * 10 ** 34:.2f} × 10^(-34) J·s"sheet["A22"] = "Ep"sheet["B22"] = f"{Ep:.2f}%"wb.save(excel_file)print("計算結果已寫入 Excel")print(f"{Ep:.2f}%")if save_to_word:from docx import Documentfrom docx.shared import Inchesdef Htlanglanglang():doc = Document()# 創建表格(3 行 2 列),確保六張圖片能排在一頁上table = doc.add_table(rows=3, cols=2)table.autofit = True # 自動調整列寬img_index = 0 # 計數器for i in range(1, N + 2): # 遍歷所有圖片(包括截止電壓-頻率圖)image_path = os.path.join(output_dir, f"output_{i}.png")if os.path.exists(image_path):row = img_index // 2 # 計算行號col = img_index % 2 # 計算列號cell = table.cell(row, col) # 獲取單元格paragraph = cell.paragraphs[0]run = paragraph.add_run()run.add_picture(image_path, width=Inches(3)) # 調整寬度,確保 3 張圖片一行img_index += 1doc.save(word_file)print(f"實驗結果已保存至 Word: {word_file}")if calculate:Htlang()
if save_to_word:Htlanglanglang()
?操作步驟
第0步 創建所需文件
在電腦某個位置新建一個文件夾,然后在新建的文件夾里新建一個.xlsx文件、一個.py文件、一個.docx文件以及一個文件夾,完成后大概長下面這樣
第1步 填入數據、粘貼代碼
打開excel文件,將原始數據輸入,格式如下
注意:A列的幾個單元格要合并一下不然會報錯(我懶的調了),電流的單位中的-11次方這些不需要在excel中就給它改為上標不然不知道會不會報錯(我懶的試了)
打開Python文件,將代碼粘貼進去,并在代碼中注釋了控制臺的部分對文件路徑進行修改
第2步 安裝必要的Python擴展庫
打開cmd,輸入以下代碼(二選一)
pip install scipy
pip install python-docx
pip install openpyxl
pip install matplotlib
pip install numpy
pip install openpyxl matplotlib numpy scipy python-docx
第3步 運行代碼
一切順利的話運行結果大致如下
?此外,打開excel,會發現多了一些數據
文件夾里也會多出6張圖片
第4步 發現問題并稍加調整
因為找拐點的方法比較粗略,所以可能會存在較大的誤差,導致算出來的百分差嚴重超標,所以我們需要進行手動調整,然后將五個截止電壓在代碼中事先輸入,如下圖
第5步 再次運行代碼
我們需要不斷的調整和嘗試,直到把百分差降到10%以下
注意:運行時記得把excel關掉否則會報錯
第6步 把圖片保存到word中
經過不斷的調整,將百分差控制在10以內之后,將代碼中的save_to_word這個變量設置為1(當然一開始就設置為1也不是不行,開始教學之前我忘記改了),再次運行代碼(此時可以將變量calculate設置為0,不過無所謂,數據量不大,運行也耗不了多長時間),會發現輸出多了一句話
然后就可以打開word,按自己的需求調整圖片的大小(代碼已經初步調好了),然后去打印店打印結果了。