In [135]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit  # import the curve fitting function
%matplotlib inline

IB Calibration


In [136]:
Currents = np.array([2.764, 2.896, 2.968, 3.083, 3.174, 3.278, 3.402, 3.526, 3.647, 3.777]) #Amps
Fields = np.array([3697, 3814, 3905, 4011, 4104, 4199, 4309, 4413, 4510, 4594])*1e-4 #Tesla
eFields = 2*1e-4

In [137]:
def myfun(A,b,c):
    ans = b*A + c*A**2  # this is y, "the function to be fit"
    return ans

p0 = [1.5/4,0]

xlots = np.linspace(0,3.9,500)  # need lots of data points for smooth curve
yfit = np.zeros((len(Currents),xlots.size))

plsq, pcov = curve_fit(myfun, Currents, Fields, p0,sigma = eFields)  # curve fit returns p and covariance matrix
# these give the parameters and the uncertainties
b = plsq[0]
eb = np.sqrt(pcov[0,0])

c = plsq[1]
ec = np.sqrt(pcov[1,1])




yfit = myfun(xlots,plsq[0],plsq[1])  # use fit results for a, b, c

plt.figure(figsize=(10,4))
plt.scatter(Currents, Fields);
plt.errorbar(Currents, Fields,eFields,fmt='none')
plt.xlim(0,3.9);
plt.ylim(0,0.5)
plt.xlabel('$I$ [A]',size=15);
plt.ylabel('$B_o$ [T]',size=15);
plt.xticks(size = 12);
plt.yticks(size = 12);
plt.text(2.45, 0.21, 'b = %.3f $\pm$ %.3f $T/A$' %(b,eb), fontsize=15)
plt.text(2.45, 0.17, 'c = %.4f $\pm$ %.4f $T/A^2$' %(c,ec), fontsize=15)
plt.plot(xlots,yfit);
plt.legend(['Fit','Data'],loc='best');
plt.savefig('FieldFit.png')


Finding Fit Uncertainty


In [138]:
def myfunUnc(A,eA,b,c,eb,ec): #Returns dB. B = a + b*A + c*A**2
    return np.sqrt((A*eb)**2 + (ec*A**2)**2 + ((b + 2*c*A)*eA)**2 )

Water Resonance


In [139]:
WaterCurrents = np.array([2.989, 3.037, 3.131, 3.244, 3.318, 3.396, 3.472, 3.569]) #Amps
eWaterCurrents = 0.001
WaterRF = np.array([17.06, 17.26, 17.65, 18.11, 18.35, 18.64, 18.94, 19.28])*1e6 #Hz
eWaterRF = 0.05*1e6

WaterFields = myfun(WaterCurrents,b,c) #Tesla
eWaterFields = myfunUnc(WaterCurrents,eWaterCurrents,b,c,eb,ec)

In [140]:
WaterFields


Out[140]:
array([ 0.39221143,  0.39681611,  0.40567853,  0.4160605 ,  0.42269852,
        0.4295576 ,  0.43610482,  0.44426615])

In [141]:
eWaterFields


Out[141]:
array([ 0.004119  ,  0.0042152 ,  0.00440714,  0.00464413,  0.00480307,
        0.00497382,  0.00514341,  0.00536449])

In [142]:
plt.figure(figsize=(9,4))
plt.scatter(Currents, Fields,label='Calibration Data',color='red');
plt.scatter(WaterCurrents,WaterFields,color='blue',label='Applied $B_o$')
plt.errorbar(WaterCurrents,WaterFields,eWaterFields,fmt='none')
plt.xlim(2.7,3.9);
#plt.ylim(0,2500)
plt.xlabel('Current [A]',size=20);
plt.ylabel('Magnetic Field [T]',size=20);
plt.xticks(size = 15);
plt.yticks(size = 15);
plt.text(3.4, 0.385, 'b = %.2f $\pm$ %.2f $G/A$' %(b,eb), fontsize=15)
plt.text(3.4, 0.37, 'c = %.2f $\pm$ %.2f $G/A^2$' %(c,ec), fontsize=15)
plt.plot(xlots,yfit,label='Quadratic Fit');
plt.legend(loc='best');
#plt.savefig('FieldFit.png')


Determine $\mu_N$ with water


In [143]:
h = 6.626*1e-34 #Js
uN = 5.05*1e-27 #J/t

def myfun2(B,guN):
    ans =  (1/h)*guN*B #Distance traveled with offset
    return ans

p02 = [1e-27] #guess

xspace2 = np.linspace(0.39,0.45) 

plsq, pcov = curve_fit(myfun2, WaterFields, WaterRF, p02,sigma=eWaterRF)  # curve fit returns p and covariance matrix
# these give the parameters and the uncertainties
guN = plsq[0]
eguN = np.sqrt(pcov[0,0])



yfit2 = myfun2(xspace2,plsq[0])  # use fit results for a, b, c

plt.figure(figsize=(10,6));

plt.scatter(WaterFields,WaterRF*1e-6, marker='.',label='Data');
plt.errorbar(WaterFields, WaterRF*1e-6, yerr=eWaterRF*1e-6,xerr=eWaterFields,fmt='none')
plt.plot(xspace2,yfit2*1e-6,label='Fit')

plt.legend(loc='best')
plt.xlabel('$B_o$ [T]',size=18);
plt.ylabel('$f_{RF}$ [MHz]',size=18);
plt.text(0.385,19,'$g = %.3f \pm %.3f$' % (plsq[0]/uN, np.sqrt(pcov[0,0])/uN),size=20)

plt.yticks(size = 15);
plt.xticks(size = 15);
plt.savefig('WaterFit.png')



In [151]:
myfun2(0.4,plsq[0])*1e-6


Out[151]:
17.38166560142659

In [152]:
df = (uN/h)*0.4*(np.sqrt(pcov[0,0])/uN)*1e-6
df


Out[152]:
0.0077767726633906041

In [153]:
(0.0077767726633906041/17.38166560142659)**2


Out[153]:
2.001778526188228e-07

Exponential Decay of peak amplitude


In [154]:
Time = np.array([0, 106, 190, 262, 324, 380, 432, 482, 528, 560, 612, 652, 688, 726])*1e-6 #Seconds
Amplitude = np.array([4.325, 3.625, 2.8, 2.45, 2.15, 1.7, 1.45, 1.3, 1.05, 1.025, 0.825, 0.775, 0.75, 0.525]) #Volts

eTime = 5*1e-6
eAmplitude = 0.005

logeAmplitude = np.array([eAmplitude/entry for entry in Amplitude])

In [155]:
h = 6.626*1e-34 #Js

def myfun4(t,A,tau):
    ans =  A*np.exp(-t/tau)
    return ans

p04 = [4,400] #guess

xspace4 = np.linspace(0,750) 

plsq, pcov = curve_fit(myfun4, Time*1e6, Amplitude, p04)  # curve fit returns p and covariance matrix
# these give the parameters and the uncertainties
A = plsq[0]
eA = np.sqrt(pcov[0,0])

tau = plsq[1]
etau = np.sqrt(pcov[1,1])


yfit4 = myfun4(xspace4,plsq[0],plsq[1])  # use fit results for a, b, c

plt.figure(figsize=(10,6));

plt.scatter(Time*1e6, Amplitude,label='Data');
plt.errorbar(Time*1e6, Amplitude, yerr=eAmplitude,xerr=eTime*1e6,fmt='none')
plt.plot(xspace4,yfit4,label='Fit')

plt.xlim(-5,750);

plt.legend(loc='upper right')
plt.xlabel('Time [$\mu s$]',size=18);
plt.ylabel('Peak Amplitude [V]',size=18);
plt.text(300,4.5,'$tau = (%.0f \pm %.0f)$ $\mu s$' % (plsq[1], np.sqrt(pcov[1,1])),size=20)

plt.yticks(size = 15);
plt.xticks(size = 15);
#plt.savefig('ExponentialFit.png')
plt.savefig('ExponentialDecay.png')



In [156]:
h = 6.626*1e-34 #Js

def myfun6(t,Vo,tau):
    ans =  np.log(Vo) - t/tau
    return ans

p06 = [4,400] #guess

xspace6 = np.linspace(0,750);

plsq, pcov = curve_fit(myfun6, Time*1e6, np.log(Amplitude), p04,sigma = logeAmplitude); # curve fit returns p and covariance matrix
# these give the parameters and the uncertainties
Vo = plsq[0];
eVo = np.sqrt(pcov[0,0]);

tau = plsq[1];
etau = np.sqrt(pcov[1,1]);


yfit6 = myfun6(xspace6,plsq[0],plsq[1]);  # use fit results for a, b, c

plt.figure(figsize=(10,6));

plt.figure(figsize=(10,6));
plt.scatter(Time*1e6, np.log(Amplitude),label='Data');
plt.errorbar(Time*1e6, np.log(Amplitude), yerr=logeAmplitude,xerr=eTime*1e6,fmt='none');
plt.xlabel('Time [$\mu s$]',size=18);
plt.ylabel('$\ln{V}$',size=18);
plt.plot(xspace6,yfit6,label='Fit');

plt.xlim(-5,750);

plt.legend(loc='upper right');
plt.text(20,0,'$tau = %.0f \pm %.0f$ $\mu s$' % (plsq[1], np.sqrt(pcov[1,1])),size=20)

plt.yticks(size = 15);
plt.xticks(size = 15);
plt.savefig('ExponentialFit.png')


<matplotlib.figure.Figure at 0x1c1f5234630>

Need to fit this and determine a time constant. Also account for every other measurement being slightly offset?

PTFE Resonance


In [157]:
PTFECurrents = np.array([2.9, 3.001, 3.099, 3.26, 3.256, 3.34, 3.45, 3.545, 3.652, 3.763, 3.848, 3.95]) #Amps
ePTFECurrents = 0.001
PTFERF = np.array([15.77, 16.15, 16.53, 17.32, 17.33, 17.51, 17.86, 18.02, 18.34, 18.67, 18.93, 19.19])*1e6 #Hz
ePTFERF = 0.05*1e6

PTFEFields = myfun(PTFECurrents,b,c) #Tesla
PTFEFields


Out[157]:
array([ 0.38353191,  0.39336762,  0.40268459,  0.41750653,  0.41714558,
        0.42464745,  0.43422337,  0.44226721,  0.45107597,  0.45993288,
        0.46652161,  0.47420647])

In [158]:
ePTFEFields = myfunUnc(PTFECurrents,ePTFECurrents,b,c,eb,ec)
ePTFEFields


Out[158]:
array([ 0.00394382,  0.00414293,  0.00434127,  0.00467824,  0.0046697 ,
        0.00485089,  0.00509399,  0.0053093 ,  0.00555781,  0.00582236,
        0.00602964,  0.00628377])

In [159]:
plt.figure(figsize=(9,4))
plt.scatter(Currents, Fields,label='Calibration Data',color='black');
plt.scatter(WaterCurrents,WaterFields,color='blue',label='$H_2 O$ $B_o$')
plt.errorbar(WaterCurrents,WaterFields,eWaterFields,fmt='none')

plt.scatter(PTFECurrents,PTFEFields,color='green',label='$PTFE$ $B_o$')
plt.errorbar(PTFECurrents,PTFEFields,ePTFEFields,fmt='none')

plt.xlim(2.7,3.9);
#plt.ylim(0,2500)
plt.xlabel('Current [A]',size=15);
plt.ylabel('Magnetic Field [T]',size=15);
plt.xticks(size = 15);
plt.yticks(size = 15);
plt.text(3.35, 0.385, '$b = %.3f \pm %.3f$ $G/A$' %(b,eb), fontsize=15)
plt.text(3.35, 0.37, '$c = %.4f \pm %.4f$ $G/A^2$' %(c,ec), fontsize=15)
plt.plot(xlots,yfit,label='Quadratic Fit');
plt.legend(loc='best');
#plt.savefig('FieldFit.png')


Determine $\mu_N$ for PTFE


In [160]:
h = 6.626*1e-34 #Js

def myfun3(B,guN):
    ans =  (1/h)*guN*B #Distance traveled with offset
    return ans

p03 = [1e-27] #guess

xspace3 = np.linspace(0.375,0.48) 

plsq, pcov = curve_fit(myfun3, PTFEFields, PTFERF, p03,sigma=ePTFERF)  # curve fit returns p and covariance matrix
# these give the parameters and the uncertainties
guN = plsq[0]
eguN = np.sqrt(pcov[0,0])



yfit3 = myfun3(xspace3,plsq[0])  # use fit results for a, b, c

plt.figure(figsize=(10,6));

plt.scatter(PTFEFields,PTFERF*1e-6,label='Data');
plt.errorbar(PTFEFields, PTFERF*1e-6, yerr=ePTFERF*1e-6,xerr=ePTFEFields,fmt='none')
plt.plot(xspace3,yfit3*1e-6,label='Fit')

plt.legend(loc='upper left')
plt.xlabel('$B_o$ [T]',size=18);
plt.ylabel('$f_{RF}$ [MHz]',size=18);
plt.text(0.365,18.8,'$g = %.2f \pm %.2f$' %  (plsq[0]/uN, np.sqrt(pcov[0,0])/uN),size=20)

plt.yticks(size = 15);
plt.xticks(size = 15);
plt.savefig('PTFEFit.png')



In [161]:
myfun3(0.4,plsq[0])*1e-6


Out[161]:
16.375408391213096

In [162]:
df = (uN/h)*0.4*(np.sqrt(pcov[0,0])/uN)*1e-6
df


Out[162]:
0.042258457941426575

In [163]:
np.sqrt((0.042258457941426575/16.375408391213096)**2 + (0.0077767726633906041/17.38166560142659)**2)


Out[163]:
0.0026191027331300448

In [164]:
16.375408391213096/17.38166560142659


Out[164]:
0.9421081251194415

Linespacing with $f$


In [112]:
fo = 17.4785*1e6
efo = 0.0002*1e6


Times = np.array([3.18, 6, 9.24, 7.25, 4.65])*1e-3 #Seconds
eTimes = 0.01*1e-3

Frequencies = np.array([17.5855, 17.6614, 17.6368, 17.614, 17.5989])*1e6 #MHz
eFrequencies = 0.01*1e6

Times = np.array([3.18, 9.24, 7.25, 4.65])*1e-3 #Seconds
Frequencies = np.array([17.5855, 17.6368, 17.614, 17.5989])*1e6 #MHz

BadTime = 6*1e-3
BadFreq = 17.6614*1e6

DeltaBadF = BadFreq - fo
DeltaBadTime = BadTime


DeltaF = Frequencies - fo
DeltaT = Times

In [116]:
plt.figure(figsize=(10,6));

plt.scatter(DeltaF*1e-6,DeltaT*1e3,label='Data');
plt.errorbar(DeltaF*1e-6, DeltaT*1e3, yerr=eFrequencies*1e-6,xerr=eTimes*1e3,fmt='none')

plt.scatter(DeltaBadF*1e-6,DeltaBadTime*1e3,color='green',label='Rejected',marker='*');
plt.errorbar(DeltaBadF*1e-6,DeltaBadTime*1e3, yerr=eFrequencies*1e-6,xerr=eTimes*1e3,fmt='none')

plt.legend(loc='upper left')
plt.ylabel('$\Delta T$ [s]',size=18);
plt.xlabel('$\Delta f_{RF}$ [MHz]',size=18);



In [134]:
def myfun7(f,a,b):
    ans =  a*f+b #Distance traveled with offset
    return ans

p07 = [10,0] #guess

xspace7 = np.linspace(0.1,0.16) 

plsq, pcov = curve_fit(myfun7, DeltaF*1e-6, DeltaT*1e3, p07)  # curve fit returns p and covariance matrix
# these give the parameters and the uncertainties
a = plsq[0]
ea = np.sqrt(pcov[0,0])

b = plsq[1]
eb = np.sqrt(pcov[1,1])



yfit7 = myfun7(xspace7,plsq[0],plsq[1])  # use fit results for a, b, c

plt.figure(figsize=(10,6));

plt.scatter(DeltaF*1e-6,DeltaT*1e3,label='Data');
plt.errorbar(DeltaF*1e-6, DeltaT*1e3, yerr=eFrequencies*1e-6,xerr=eTimes*1e3,fmt='none')

plt.scatter(DeltaBadF*1e-6,DeltaBadTime*1e3,color='green',label='Rejected',marker='*');
plt.errorbar(DeltaBadF*1e-6,DeltaBadTime*1e3, yerr=eFrequencies*1e-6,xerr=eTimes*1e3,fmt='none')


plt.ylabel('$\Delta T$ [ms]',size=18);
plt.xlabel('$\Delta f_{RF}$ [MHz]',size=18);


plt.plot(xspace7,yfit7,label='Fit')

plt.legend(loc='upper left')
plt.text(0.14,4,'Slope = $%.0f \pm %.0f$ $ms/MHz$' % (plsq[0], np.sqrt(pcov[0,0])),size=20)
#plt.text(17.62,4.35,'$b = %.2f \pm %.2f$ $ms$' % (plsq[1], np.sqrt(pcov[1,1])),size=20)

plt.yticks(size = 15);
plt.xticks(size = 15);
plt.savefig('LinearFit.png')



In [127]:
a


Out[127]:
121.41755248281964

In [129]:
#so 1/a carries units MHz/ms
((1/a)*400*1e-3)*1e3 #This has units kHz


Out[129]:
3.2944165964521419

In [130]:
np.sqrt((ea/a)**2 + (10/400)**2)


Out[130]:
0.10037747195861424

In [71]:
np.sort(Times)*1e3


Out[71]:
array([ 3.18,  4.65,  7.25,  9.24])

In [70]:
np.sort(Frequencies)*1e-6


Out[70]:
array([ 17.5855,  17.5989,  17.614 ,  17.6368])