from scipy.optimize import curve_fit
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
def linearFunc(x,intercept,slope):
y = intercept + slope * x
return y
student_data={}
student_data[1] = {}
student_data[1]['Trial1']={}
student_data[1]['Trial2']={}
student_data[1]['Trial3']={}
student_data[1]['Trial4']={}
student_data[1]['Trial1']['x'] = [0.58, 0.48, 0.38, 0.28, 0.18, 0.08]
student_data[1]['Trial2']['x'] = [0.58, 0.48, 0.38, 0.28, 0.18, 0.08]
student_data[1]['Trial3']['x'] = [0.58, 0.48, 0.38, 0.28, 0.18, 0.08]
student_data[1]['Trial4']['x'] = [0.58, 0.48, 0.38, 0.28, 0.18, 0.08]
student_data[1]['Trial1']['t'] = [7.16, 6.48, 5.8, 5.16, 4.4, 3.76]
student_data[1]['Trial2']['t'] = [7.12, 6.52, 5.76, 5.12, 4.4, 3.68]
student_data[1]['Trial3']['t'] = [7.16, 6.52, 5.8, 5.12, 4.4, 3.72]
student_data[1]['Trial4']['t'] = [7.16, 6.48, 5.8, 5.12, 4.4, 3.76]
student_data[2] = {}
student_data[2]['Trial1']={}
student_data[2]['Trial2']={}
student_data[2]['Trial3']={}
student_data[2]['Trial4']={}
student_data[2]['Trial1']['x'] = [0.55, 0.50, 0.45, 0.40]
student_data[2]['Trial2']['x'] = [0.55, 0.50, 0.45, 0.40]
student_data[2]['Trial3']['x'] = [0.55, 0.50, 0.45, 0.40]
student_data[2]['Trial4']['x'] = [0.55, 0.50, 0.45, 0.40]
student_data[2]['Trial1']['t'] = [6.9, 6.62, 6.24, 5.80]
student_data[2]['Trial2']['t'] = [6.86, 6.6, 6.24, 5.86]
student_data[2]['Trial3']['t'] = [6.9, 6.62, 6.24, 5.86]
student_data[2]['Trial4']['t'] = [6.88, 6.6, 6.22, 5.84]
student_data[3] = {}
student_data[3]['Trial1']={}
student_data[3]['Trial2']={}
student_data[3]['Trial3']={}
student_data[3]['Trial4']={}
student_data[3]['Trial5']={}
student_data[3]['Trial1']['x'] = [0.01, 0.11, 0.21, 0.31, 0.41]
student_data[3]['Trial2']['x'] = [0.01, 0.03, 0.05, 0.07, 0.09]
student_data[3]['Trial3']['x'] = [0.01, 0.16, 0.31, 0.46, 0.61]
student_data[3]['Trial4']['x'] = [0.01, 0.06, 0.11, 0.16, 0.21]
student_data[3]['Trial5']['x'] = [0.01, 0.11, 0.21, 0.31, 0.41]
student_data[3]['Trial1']['t'] = [3.22, 3.84, 4.66, 5.42, 6.04]
student_data[3]['Trial2']['t'] = [3.12, 3.3, 3.44, 3.6, 3.76]
student_data[3]['Trial3']['t'] = [3.1, 4.22, 5.38, 6.38, 7.38]
student_data[3]['Trial4']['t'] = [3.16, 3.5, 3.86, 4.2, 4.6]
student_data[3]['Trial5']['t'] = [3.1, 3.88, 4.62, 5.4, 6.06]
student_data[4] = {}
student_data[4]['Trial1']={}
student_data[4]['Trial2']={}
student_data[4]['Trial3']={}
student_data[4]['Trial4']={}
student_data[4]['Trial1']['x'] = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
student_data[4]['Trial2']['x'] = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
student_data[4]['Trial3']['x'] = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
student_data[4]['Trial4']['x'] = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
student_data[4]['Trial1']['t'] = [0.84, 1.6, 2.36, 3.08, 3.8, 4.44]
student_data[4]['Trial1']['t'] = [t + 2.6 for t in student_data[4]['Trial1']['t']]
student_data[4]['Trial2']['t'] = [0.76, 1.56, 2.4, 3.08, 3.68, 4.4]
student_data[4]['Trial2']['t'] = [t + 2.64 for t in student_data[4]['Trial2']['t']]
student_data[4]['Trial3']['t'] = [0.88, 1.6, 2.44, 3.16, 3.72, 4.44]
student_data[4]['Trial3']['t'] = [t + 2.6 for t in student_data[4]['Trial3']['t']]
student_data[4]['Trial4']['t'] = [0.84, 1.6, 2.4, 3.16, 3.76, 4.44]
student_data[4]['Trial4']['t'] = [t + 2.6 for t in student_data[4]['Trial4']['t']]
student_data[5] = {}
student_data[5]['Trial1']={}
student_data[5]['Trial1']['x'] = [0.5125, 0.6125, 0.7125, 0.8125, 0.9125]
student_data[5]['Trial1']['t'] = [2.84, 3.64, 4.4, 5.32, 6.06]
student_data[6] = {}
student_data[6]['Trial1']={}
student_data[6]['Trial2']={}
student_data[6]['Trial3']={}
student_data[6]['Trial4']={}
student_data[6]['Trial1']['x'] = [0.6, 0.5, 0.4, 0.3, 0.2, 0.1]
student_data[6]['Trial2']['x'] = [0.6, 0.5, 0.4, 0.3, 0.2, 0.1]
student_data[6]['Trial3']['x'] = [0.6, 0.5, 0.4, 0.3, 0.2, 0.1]
student_data[6]['Trial4']['x'] = [0.6, 0.5, 0.4, 0.3, 0.2, 0.1]
student_data[6]['Trial1']['t'] = [7.2, 6.52, 5.84, 5.16, 4.48, 3.68]
student_data[6]['Trial2']['t'] = [7.12, 6.6, 5.84, 5.16, 4.44, 3.72]
student_data[6]['Trial3']['t'] = [7.08, 6.56, 5.84, 5.16, 4.44, 3.76]
student_data[6]['Trial4']['t'] = [7.12, 6.52, 5.80, 5.16, 4.48, 3.72]
student_data[7] = {}
student_data[7]['Trial1']={}
student_data[7]['Trial2']={}
student_data[7]['Trial3']={}
student_data[7]['Trial4']={}
student_data[7]['Trial1']['x'] = [0.6, 0.5, 0.4, 0.3, 0.2, 0.1]
student_data[7]['Trial2']['x'] = [0.6, 0.5, 0.4, 0.3, 0.2, 0.1]
student_data[7]['Trial3']['x'] = [0.6, 0.5, 0.4, 0.3, 0.2, 0.1]
student_data[7]['Trial4']['x'] = [0.6, 0.5, 0.4, 0.3, 0.2, 0.1]
student_data[7]['Trial1']['t'] = [7.2, 6.52, 5.84, 5.16, 4.48, 3.68]
student_data[7]['Trial2']['t'] = [7.12, 6.6, 5.84, 5.16, 4.44, 3.72]
student_data[7]['Trial3']['t'] = [7.08, 6.56, 5.84, 5.16, 4.44, 3.76]
student_data[7]['Trial4']['t'] = [7.12, 6.52, 5.80, 5.16, 4.48, 3.72]
plt.figure(figsize=(12,8))
for student in student_data:
plot_x = []
plot_y = []
for trial in student_data[student]:
for val in student_data[student][trial]['x']:
plot_x.append(val)
for val in student_data[student][trial]['t']:
plot_y.append(val)
plt.plot(plot_x, plot_y, 'o', label='Student #'+str(student))
plt.legend(loc='best',fontsize=12)
plt.xlabel('Recorded corner cube location (m)',fontsize=16)
plt.ylabel('Recorded time difference (ns)',fontsize=16)
plt.figure(figsize=(12,8))
for student in [1, 2, 3, 6, 7]:
plot_x = []
plot_y = []
for trial in student_data[student]:
for val in student_data[student][trial]['x']:
plot_x.append(val)
for val in student_data[student][trial]['t']:
plot_y.append(val)
plt.plot(plot_x, plot_y, 'o', label='Student #'+str(student))
plt.legend(loc='best',fontsize=12)
plt.xlabel('Recorded corner cube location (m)',fontsize=16)
plt.ylabel('Recorded time difference (ns)',fontsize=16)
xdata = []
ydata = []
sigma_y = []
for student in [1, 2, 3, 6, 7]:
for trial in student_data[student]:
for x in student_data[student][trial]['x']:
xdata.append(2*x)
for y in student_data[student][trial]['t']:
ydata.append(y*1e-9)
sigma_y.append(0.1e-9)
print(xdata)
print(ydata)
xdata = np.array(xdata)
ydata = np.array(ydata)
sigma_y = np.array(sigma_y)
#xdata = np.array([8.213, 7.402, 6.876, 5.491, 5.196])
#ydata = np.array([3.261, 2.52, 2.239, 1.299, 1.175])
#sigma_y = np.array([9.71e-2, 5.59e-2, 7.08e-2, 6.83e-2, 8.93e-2])
a_fit,cov=curve_fit(linearFunc,xdata,ydata,sigma=sigma_y)
inter = a_fit[0]
slope = a_fit[1]
d_inter = np.sqrt(cov[0][0])
d_slope = np.sqrt(cov[1][1])
plt.figure(figsize=(12,8))
plt.errorbar(xdata,ydata,yerr=sigma_y,fmt='r.',label='Data')
# Compute a best fit line from the fit intercept and slope.
yfit = inter + slope*xdata
# Create a graph of the fit to the data. We just use the ordinary plot
# command for this.
#plt.plot(xdata,yfit,label='Fit')
#plt.plot(xdata, (1/(299792458))*xdata, label='Theory')
#plt.plot(xdata, (1/(299792458))*xdata + inter, label='Theory with fitted offset')
plt.plot(xdata, (1/(299702547))*xdata + inter, label='Theory with fitted offset (c = 299702547 m/s)')
# in air 299 702 547 m / s
# in vacuum 299,792,458 m/s
# Display a legend, label the x and y axes and title the graph.
plt.legend(fontsize=12)
plt.xlabel('Difference in path length (roundtrip) (meters)',fontsize=16)
plt.ylabel('Difference in arrival time (seconds)',fontsize=16)
print(f'The slope = {slope}, with uncertainty {d_slope}')
print(f'The intercept = {inter}, with uncertainty {d_inter}')
print(f'The speed of light = {1/slope}')
residuals = ydata- linearFunc(xdata, *a_fit)
ss_res = np.sum(residuals**2)
ss_tot = np.sum((ydata-np.mean(ydata))**2)
r_squared = 1 - (ss_res / ss_tot)
print(r_squared)