Modern Phys Lab

This commit is contained in:
Nathan Nguyen
2025-02-25 22:21:47 -06:00
parent eca6b8bc3f
commit af7471f4b4
2 changed files with 46 additions and 12 deletions

View File

@@ -1,5 +1,7 @@
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from matplotlib import colors
import scipy
def read_file(file: str) -> np.typing.ArrayLike: def read_file(file: str) -> np.typing.ArrayLike:
with open(f'{file}.csv', 'r') as file: with open(f'{file}.csv', 'r') as file:
@@ -12,7 +14,7 @@ all_data = {
'40': sum([read_file(f'40-{i}') for i in range(1, 6)]) '40': sum([read_file(f'40-{i}') for i in range(1, 6)])
} }
fig, axs = plt.subplots(len(all_data), sharex='all') fig, axs = plt.subplots(len(all_data), 1, sharex='col')
i = 0 i = 0
for dwell_time in all_data: for dwell_time in all_data:
@@ -41,15 +43,47 @@ for dwell_time in all_data:
print(f'Found {times_dev_more_than_P} / {N} ({times_dev_more_than_P / N * 100:0.2f}%) samples deviating from the mean more than P {P:0.2f}') print(f'Found {times_dev_more_than_P} / {N} ({times_dev_more_than_P / N * 100:0.2f}%) samples deviating from the mean more than P {P:0.2f}')
ax = axs[i] ax = axs[i]
ax.set_xlabel('Run Count')
ax.set_ylabel('Events Measured')
ax.set_title('Raw Data')
ax.scatter(np.arange(1, len(data)+1), data, s=4)
ax.legend(['\n'.join([
f'Mean: {mean:0.2f}',
f'Sample Sd. Dev: {stdev:0.2f}',
f'P: {P:0.2f}',
f'Events > sigma: {times_dev_more_than_s} ({times_dev_more_than_s / N * 100:0.2f}%)',
f'Events > P: {times_dev_more_than_P} ({times_dev_more_than_P / N * 100:0.2f}%)'
])])
# ax = axs[i][1]
# ax.set_visible(False)
# ax.set_title(f'Cumulative Average ({dwell_time}ms Dwell time)')
# ax.set_xlabel('After N Runs')
# ax.set_ylabel('Cumulative Average')
# sample_num = np.arange(1, N + 1)
# cumulative_average = np.cumsum(data) / sample_num
# ax.plot(sample_num, cumulative_average, label=f'Final Value: {cumulative_average[-1]:0.2f}')
# ax.legend()
# ax = axs[i]
# N, bins, patches = ax.hist(data, density=True, bins=32, label='Measured Data')
# fracs = N / N.max()
# norm = colors.Normalize(fracs.min(), fracs.max())
# for thisfrac, thispatch in zip(fracs, patches):
# color = plt.cm.viridis(norm(thisfrac))
# thispatch.set_facecolor(color)
# ax.set_xlabel('Event Count')
# ax.set_ylabel('Frequency')
# linspace = np.arange(bins.min(), bins.max())
# gaussian = scipy.stats.norm.pdf(linspace, mean, stdev)
# ax.plot(linspace, gaussian, label='Fitted Gaussian')
# ax.legend()
i += 1 i += 1
ax.set_title(f'Cumulative Average ({dwell_time}ms Dwell time)')
ax.set_xlabel('After N Runs')
ax.set_ylabel('Cumulative Average')
sample_num = np.arange(1, N + 1)
cumulative_average = np.cumsum(data) / sample_num
ax.plot(sample_num, cumulative_average, label=f'Final Value: {cumulative_average[-1]:0.2f}')
ax.legend()
plt.show() plt.show()

View File

@@ -6,7 +6,7 @@ voltages = np.array(np.arange(2500, 5000 + 1, +500)) #+1 to include 5000
voltages_inv_sqrt = 1 / np.sqrt(voltages) voltages_inv_sqrt = 1 / np.sqrt(voltages)
# New units: m # New units: m
diameter_measured_error = 0.002 diameter_measured_error = 0.001
# Average inner and outer # Average inner and outer
# New format: d[0 = small, 1 = large][voltage] # New format: d[0 = small, 1 = large][voltage]