In [95]:
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 [96]:
CS137Peaks = np.array([416.77])
CS137Energy = np.array([662]) #KeV
In [97]:
BA133Peaks = np.array([224.5,189.14,55.23])
BA133Energy = np.array([356,303,81])
In [98]:
CO60Peaks = np.array([809.59,714.21])
CO60Energy = np.array([1333,1173])
In [99]:
Peaks = np.hstack([CS137Peaks,BA133Peaks,CO60Peaks])
In [100]:
Peaks
Out[100]:
In [101]:
Energy = np.hstack([CS137Energy,BA133Energy,CO60Energy])
In [102]:
Energy
Out[102]:
In [103]:
UnknownPeaks = np.array([786.45,323.39,123.85])
In [104]:
plt.figure(figsize=(10,6));
plt.scatter(Peaks,Energy);
plt.xlabel('MCA Number',fontsize=20);
plt.ylabel('Energy (keV)',fontsize = 20);
plt.xticks(size = 13);
plt.yticks(size = 13);
#plt.savefig('Sample')
Out[104]:
In [105]:
def myfun(N,a,b,c):
ans = a + b*N + c*N**2 # this is y, "the function to be fit"
return ans
In [106]:
p0 = [-2,1,0]
In [107]:
xlots = np.linspace(0,910) # need lots of data points for smooth curve
yfit = np.zeros((len(Peaks),xlots.size))
plsq, pcov = curve_fit(myfun, Peaks, Energy, 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])
yfit = myfun(xlots,plsq[0],plsq[1],plsq[2]) # use fit results for a, b, c
print('a = %.7f +/- %.7f' % (plsq[0], np.sqrt(pcov[0,0])))
print('b = %.7f +/- %.7f' % (plsq[1], np.sqrt(pcov[1,1])))
print('c = %.7f +/- %.7f' % (plsq[2], np.sqrt(pcov[2,2])))
In [109]:
plt.figure(figsize=(10,6));
plt.scatter(Peaks,Energy);
plt.xlim(0,850)
plt.ylim(0,1600)
plt.xlabel('x (mm)');
plt.ylabel('y (mm)');
plt.plot(xlots,yfit);
plt.legend(['data','Fit'],loc='lower right');
plt.text(20,1400,'a = %.0f +/- %.0f keV' % (plsq[0], np.sqrt(pcov[0,0])),size=17)
plt.text(20,1250,'b = %.2f +/- %.2f keV MCA$^{-1}$' % (plsq[1], np.sqrt(pcov[1,1])),size=17)
plt.text(20,1100,'c = (%.2f +/- %.2f)$\cdot 10^{-3}$ keV MCA$^{-2}$' % (plsq[2]*1e3, np.sqrt(pcov[2,2])*1e3),size=17)
plt.xlabel('MCA Number',fontsize=20);
plt.ylabel('Energy (keV)',fontsize = 20);
plt.xticks(size = 13);
plt.yticks(size = 13);
plt.savefig('Linear')
In [71]:
E_calc = np.array([myfun(entry,a,b,c) for entry in Peaks])
In [72]:
delta_E = np.mean(np.array([abs(E_calc[i] - Energy[i]) for i in np.arange(len(E_calc))]))
delta_E
Out[72]:
In [73]:
CS137Energy[0]
Out[73]:
In [74]:
E_calc[0]
Out[74]:
In [75]:
myfun(416.77+22.48/2,a,b,c) - myfun(416.77-22.48/2,a,b,c)
Out[75]:
In [76]:
myfun(291,a,b,c)
Out[76]:
In [77]:
myfun(130.25,a,b,c)
Out[77]:
In [78]:
#All in channels
CSfwhm = np.array([22.48])
BAfwhm = np.array([17.03,14,6.2])
COfwhm = np.array([32.1, 30.71])
In [79]:
fwhmChannels = np.hstack([CSfwhm,BAfwhm,COfwhm])
In [80]:
fwhmEnergy = np.array([abs(myfun(Peaks[i] + fwhmChannels[i]/2,a,b,c)
- myfun(Peaks[i] - fwhmChannels[i]/2,a,b,c)) for i in np.arange(len(Peaks))])
In [81]:
fwhmEnergy
Out[81]:
In [82]:
resolution = np.array([fwhmEnergy[i]/myfun(Peaks[i],a,b,c) for i in np.arange(len(Peaks))])
In [83]:
EPeaks = myfun(Peaks,a,b,c)
In [84]:
plt.figure(figsize=(10,6));
plt.scatter(EPeaks,resolution);
plt.xlabel('Channel',fontsize=20);
plt.ylabel('Resolution (keV)',fontsize = 20);
plt.xticks(size = 13);
plt.yticks(size = 13);
#plt.savefig('Sample')
Out[84]:
In [85]:
def myfun2(E,a,x):
ans = a*E**x # this is y, "the function to be fit"
return ans
In [86]:
p02 = [.7,-1/2]
In [87]:
EPeaks
Out[87]:
In [88]:
xlots2 = np.linspace(0,1400) # need lots of data points for smooth curve
yfit2 = np.zeros((len(Peaks),xlots.size))
plsq, pcov = curve_fit(myfun2, EPeaks, resolution, p02) # curve fit returns p and covariance matrix
# these give the parameters and the uncertainties
aa = plsq[0]
eaa = np.sqrt(pcov[0,0])
x = plsq[1]
ex = np.sqrt(pcov[1,1])
yfit2 = myfun2(xlots2,plsq[0],plsq[1]) # use fit results for a, b, c
print('a = %.7f +/- %.7f' % (plsq[0], np.sqrt(pcov[0,0])))
print('x = %.7f +/- %.7f' % (plsq[1], np.sqrt(pcov[1,1])))
In [93]:
plt.figure(figsize=(10,6));
plt.scatter(EPeaks,resolution);
plt.xlim(0,1400)
plt.ylim(0,.2)
plt.xlabel('x (mm)');
plt.ylabel('y (mm)');
plt.plot(xlots2,yfit2);
plt.legend(['fit','data'],loc='best');
plt.text(400,.14,'a = %.2f +/- %.2f' % (plsq[0], np.sqrt(pcov[0,0])),size=17)
plt.text(400,.12,'x = %.2f +/- %.2f' % (plsq[1], np.sqrt(pcov[1,1])),size=17)
plt.xlabel('Energy (keV)',fontsize=20);
plt.ylabel('Resolution',fontsize = 20);
plt.xticks(size = 13);
plt.yticks(size = 13);
plt.savefig('Resolution')
In [90]:
myfun(786.45,a,b,c)
Out[90]:
In [91]:
myfun(323.39,a,b,c)
Out[91]:
In [92]:
myfun(123.85,a,b,c)
Out[92]: