Python
Python을 이용한 Hecht equation fitting
쿠피1905
2022. 12. 26. 18:47
운반자의 이동도와 수명의 곱을 계산하기 위한 파이썬 프로그래밍의 예제이다.
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import style
from matplotlib import pylab
import numpy as np
from scipy.optimize import curve_fit
# bias data and peak position channel data
xs=np.array([35,100,150,200,250,300])
ys=np.array([100,133,145,152,159,162])
#detector thickness in cm.
det_thick=0.2
#define fitting function
def expo_func(x, m, t):
return m*x*(1 - np.exp(-t/x) )
popt, pcov = curve_fit(expo_func, xs, ys, p0=(30, 40))
a, b = popt
c=det_thick*det_thick/b
xx = np.linspace(0.1,400,1000)
yy = expo_func(xx, *popt)
# calculation of R squared
r2 = 1. - sum((expo_func(xs, *popt) - ys) ** 2) / sum((ys - np.mean(ys)) ** 2)
plt.plot(xs,ys,'o',label="data")
plt.plot(xx, yy,'--',label="fitting")
pylab.title('Hecht equation fitting')
ax = plt.gca()
ax.set_ylim(0,300)
ax.set_xlim(0,350)
fig = plt.gcf()
plt.legend(loc='upper left')
plt.xlabel(r'Bias (V)')
plt.ylabel(r'Peak position (channel)')
plt.text(150,250, "R$^2$: {}".format(r2))
plt.text(150,200,"($\mu$$\\tau$)$_e$ = {:.5f} cm$^2$/V".format(c))
plt.show()
결과물은 다음과 같다.