我试图让绘制的图表的方程显示在它上面,但它一直向我显示一个等于零的方程,但它显然不是。当我运行我的代码时,图表非常好,非常完美,但我知道方程不应该给出这样的结果。
这是代码:
import matplotlib.pyplot as plt
import numpy as np
# Section 1: Calculating values of H for toroid A
# defining the values to find H for toroid A
N_1 = 3420
L_1 = 0.076
R_1 = 8
# defining function to find the value of H with given V_1
def H_toroid_A(V_1):
return (V_1 * N_1) / (L_1 * R_1)
# Section 2: Calculating the delta list of uncertainties for H toroid A
# defining variables and uncertainties
V_1_values = [0.198, 0.266, 0.340, 0.402, 0.505]
delta_V_1_values = [0.050, 0.050, 0.050, 0.050, 0.1] # uncertainties for V_1
delta_N_1 = 0 # uncertainty for N_1
delta_L_1 = 0.001 # uncertainty for L_1
delta_R_1 = 0.4 # uncertainty for R_1
# function to calculate uncertainties in H
def calculate_uncertainty_H(V_values, delta_V_values, delta_N, delta_L, delta_R):
uncertainties = []
for i in range(len(V_values)):
uncertainty_H = np.sqrt(
((V_values[i] / (L_1 * R_1)) * delta_N)**2 +
((N_1 / (L_1 * R_1)) * delta_V_values[i])**2 +
((-V_values[i] * N_1 / (L_1**2 * R_1)) * delta_L)**2 +
((-V_values[i] * N_1 / (L_1 * R_1**2)) * delta_R)**2
)
uncertainties.append(uncertainty_H)
return uncertainties
uncertainties_H = calculate_uncertainty_H(V_1_values, delta_V_1_values, delta_N_1, delta_L_1, delta_R_1)
# Section 3: Calculating values of B for toroid A
# defining the values to find B for toroid A
R_2 = 270000
C = 220e-9
N_2 = 820
A_2 = 9.025e-5
# defining function to find the value of B with given V_c
def B_toroid_A(V_c):
return (R_2 * C * V_c) / (N_2 * A_2)
# Section 4: Calculating list of uncertainties for B toroid A
# defining variables and uncertainties
V_c_values = [0.0266, 0.0372, 0.0505, 0.0605, 0.076]
delta_V_c_values = [0.005, 0.005, 0.01, 0.01, 0.01] # uncertainties for V_c
delta_N_2 = 0 # uncertainty for N_2
delta_C = 1.1e-8 # uncertainty for C
delta_A_2 = 5e-8 # uncertainty for A_2
delta_R_2 = 13500 # uncertainty for R_2
# function to calculate uncertainties in B
def calculate_uncertainty_B(V_values, delta_V_values, delta_N, delta_C, delta_A, delta_R):
uncertainties = []
for i in range(len(V_values)):
uncertainty_B = np.sqrt(
((C * V_values[i]) / (N_2 * A_2))**2 * delta_R_2**2 +
((R_2 * V_values[i]) / (N_2 * A_2))**2 * delta_C**2 +
((R_2 * C) / (N_2 * A_2))**2 * delta_V_values[i]**2 +
((R_2 * C * V_values[i]) / (N_2 * A_2**2))**2 * delta_A_2**2 +
((R_2 * C * V_values[i]) / (N_2**2 * A_2))**2 * delta_N_2**2
)
uncertainties.append(uncertainty_B)
return uncertainties
uncertainties_B = calculate_uncertainty_B(V_c_values, delta_V_c_values, delta_N_2, delta_C, delta_A_2, delta_R_2)
# Section Alpha: calculating list of values of H
H_values = [H_toroid_A(V_1) for V_1 in V_1_values]
# Section Beta: calculating list of values of B
B_values = [B_toroid_A(V_c) for V_c in V_c_values]
# Plotting
plt.figure(figsize=(8, 6))
plt.errorbar(H_values, B_values, xerr=uncertainties_H, yerr=uncertainties_B, fmt='o', label='Data Points')
plt.xlabel('H (Intensité du champ magnétique)')
plt.ylabel('B (Densité du flux magnétique)')
plt.title('Courbe Hysteresis')
# Performing linear regression with uncertainties
coefficients, cov_matrix = np.polyfit(H_values, B_values, 1, w=1/np.array(uncertainties_B), cov=True)
slope = coefficients[0]
intercept = coefficients[1]
# Plotting the linear fit
plt.plot(H_values, slope * np.array(H_values) + intercept, color='red', label=f'Linear Fit: B = {slope:.2f} * H + {intercept:.2f}')
plt.legend()
plt.grid(True)
plt.show()
将 .2f 切换为更精确的格式,即 12.5e。由 manu190466 建议。