In [2]:
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
In [3]:
Currents = np.array([0, 0.49, 0.98, 1.5, 1.98, 2.48, 2.99, 3.61, 3.97, 4.48, 5.02, 5.49, 6.04, 6.44, 6.98, 7.46, 8.02]) #Amps
Fields = np.array([0.3, 149, 302.3, 470.4, 623.2, 789, 949.2, 1143.5,
1245.8, 1408.2, 1571.5, 1679, 1802.8, 1879.4, 1963.5, 2028.2, 2096.2]) #Gauss
Above measurements were taken before the coil wires had time to warm. Suspect coils warmed due to current running for long periods of time during the measurement process of Zeeman splitting.
In [4]:
#Hot data
Currents = np.array([0, 0.5, 1, 1.5, 1.99, 2.48, 2.95, 3.51, 4, 4.48, 5, 5.49, 5.98, 6.49, 7.01, 7.25]) #Amps
Fields = np.array([0.1, 192.4, 366.2, 555.2, 753.1, 937.5, 1130.6, 1331.7, 1502.5, 1686.3, 1857.3,
2006.5, 2149.1, 2262, 2359.1, 2399.2]) #Gauss
In [5]:
plt.figure(figsize=(9,4))
plt.scatter(Currents, Fields);
plt.xlim(0,7.5);
plt.ylim(0,2500)
plt.xlabel('Current [A]',size=18);
plt.ylabel('Magnetic Field [Gauss]',size=18);
plt.xticks(size = 11);
plt.yticks(size = 11);
In [6]:
def myfun(A,a,b,c,d):
ans = a*A + b*A**2 + c*A**3 + d*A**4 # this is y, "the function to be fit"
return ans
In [7]:
p0 = [1500/4,0,0,0]
In [8]:
xlots = np.linspace(0,7.5,500) # need lots of data points for smooth curve
yfit = np.zeros((len(Currents),xlots.size))
plsq, pcov = curve_fit(myfun, Currents, Fields, p0) # 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])
c = plsq[2]
ec = np.sqrt(pcov[2,2])
d = plsq[3]
ed = np.sqrt(pcov[3,3])
yfit = myfun(xlots,plsq[0],plsq[1],c,d) # use fit results for a, b, c
print('a = %.0f +/- %.0f' % (plsq[0], np.sqrt(pcov[0,0])))
print('b = %.1f +/- %.1f' % (plsq[1], np.sqrt(pcov[1,1])))
print('c = %.1f +/- %.1f' % (plsq[2], np.sqrt(pcov[2,2])))
print('d = %.1f +/- %.1f' % (plsq[3], np.sqrt(pcov[3,3])))
In [9]:
plt.figure(figsize=(9,4))
plt.scatter(Currents, Fields);
plt.xlim(0,7.5);
plt.ylim(0,2500);
plt.xlabel('Current [A]',size=18);
plt.ylabel('Magnetic Field [Gauss]',size=18);
plt.xticks(size = 11);
plt.yticks(size = 11);
plt.text(4, 1000, 'a = %.2f $\pm$ %.2f $G/A$' %(a,ea), fontsize=15)
plt.text(4, 800, 'b = %.2f $\pm$ %.2f $G/A^2$' %(b,eb), fontsize=15)
plt.text(4, 600, 'c = %.2f $\pm$ %.2f $G/A^3$' %(c,ec), fontsize=15)
plt.text(4, 400, 'd = %.2f $\pm$ %.2f $G/A^4$' %(d,ed), fontsize=15)
plt.plot(xlots,yfit);
plt.legend(['Fit','Data'],loc='best');
plt.savefig('FieldFit.png')
In [10]:
AppliedCurrents1 = np.array([3.39, 3.98, 4.41, 4.81, 5.29, 5.87, 6.35, 6.87]) #Amps.
In [11]:
AppliedFields1 = myfun(AppliedCurrents1,a,b,c,d) #Gauss fields during Zeeman splitting measurements
AppliedFields1
Out[11]:
In [12]:
AppliedCurrents2 = np.array([5.64, 6, 6.31, 6.64, 6.93, 7.26]) #Amps.
In [13]:
AppliedFields2 = myfun(AppliedCurrents2,a,b,c,d) #Gauss fields during Zeeman splitting measurements
AppliedFields2
Out[13]:
In [ ]: