Speed of Light Part 1

From Advanced Labs Wiki
Jump to navigation Jump to search

Back to 2014.

Initial Python code example for Speed of Light lab.

import numpy as np
import pylab as plt

dif = np.loadtxt('data_IA_far.txt')
dtf = np.loadtxt('data_TM_far.txt')

#Python slice notation:
print "IA's data =", dif
print "IA's 1st column with rev/s =", dif[:,0]
print "IA's 2nd column with positions (mm) =", dif[:,1]

#Average across different measurements
velocity_far = (dif[:,0]+dtf[:,0])/2 #Velocities in 1st column
displace_far = (dif[:,1]+dtf[:,1])/2 #Displacements in 2nd column

#Take out offset in data
offset_TM_far = (dtf[:,1]-displace_far).mean()
offset_IA_far = (dif[:,1]-displace_far).mean()
print "Offsets =", offset_TM_far, offset_IA_far
dtf[:,1] -= offset_TM_far
dif[:,1] -= offset_IA_far

#Compute scatter between different measurements
N=2 #only two datasets from TM and IA used
var_array_far = ((dtf[:,1]-displace_far)**2+(dif[:,1]-displace_far)**2)/(N-1)
var_far = var_array_far.mean()/N #variance on mean divide by N
print 'Error on far data =', np.sqrt(var_far)

#Plot
plt.errorbar(velocity_far, displace_far, np.sqrt(var_far), fmt='.', label='Far')
plt.ylabel('Distance (mm)')
plt.xlabel('Angular velocity (rev/s)')
plt.legend(loc='upper left')
plt.savefig('data_example.png')
plt.clf()