Advertisement
erbilsilik

Untitled

Oct 13th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. from scipy.optimize import curve_fit
  3. import numpy as np
  4.  
  5.  
  6. fwhm_in = 3 # set FWHM for the artificial data - What does that it mean actually?
  7. sigma = fwhm_in/2/np.sqrt(2*np.log(2)) # calculate sigma - is my function normally distributed?
  8.  
  9. xval, yval = np.genfromtxt("G3.txt", unpack=True) # I'll read datas from file so I made it by taking values as a np.array
  10.  
  11. def fitFunc(x, x0, sigm): # this defines the fit-function
  12. return (sigm*np.sqrt(2*np.pi))**(-1)*np.exp(-(x-x0)**2/(2*sigm**2)) # Will I also find a fit-function for my own distribution?
  13.  
  14. guess = (35, 60) # Corresponding degree value for high intensity is around 40 degree, so it might be between these values
  15.  
  16. fitParams, fitCovariance = curve_fit(fitFunc, xval, yval, guess) # do the actual fit
  17. print(fitParams)
  18.  
  19. print('FWHM_calc = {:.3f}'.format(fwhm_in))
  20. fwhm_fit = 2*fitParams[1]*np.sqrt(2*np.log(2)) # calculate the FWHM from the fitted sigma ( = fitParams[1], since fitParams[0] is the offset x0)
  21. print('FWHM_fit = {:.3f}'.format(fwhm_fit))
  22.  
  23. plt.plot(xval,yval, 'r.', label='data')
  24. plt.plot(xval, fitFunc(xval, fitParams[0], fitParams[1]), 'k-', label='fit', linewidth = 3)
  25.  
  26. plt.grid(True)
  27. plt.legend()
  28. ax = plt.gca()
  29. ax.axvline(fwhm_fit/2, color='b')
  30. ax.axvline(-fwhm_fit/2, color='b')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement