Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. %matplotlib inline
  4. from __future__ import division
  5. from scipy.optimize import least_squares
  6. import scipy.odr as odr
  7. import matplotlib.pyplot as plt
  8.  
  9. nPoints = 4
  10.  
  11. AlphaArr = np.zeros(4)
  12. PubArr = np.zeros(4)
  13. AlphaErr = np.zeros(4)
  14. PubErr = np.zeros(4)
  15.  
  16. AlphaArr = [popt1[1], popt2[1], popt3[1], popt4[1]]
  17. PubArr = [8.04, 8.63, 7.47, 6.40]
  18. AlphaErr = [FWHM1_1,FWHM1_2,FWHM1_3,FWHM1_4]
  19. PubErr = [0.1,0.1,0.1,0.1]
  20.  
  21. xData = AlphaArr
  22. yData = PubArr
  23. xError = AlphaErr
  24. yError = PubErr
  25.  
  26. def fitFunc(p, x):
  27. '''
  28. Fit function
  29. '''
  30. f = p[0] + p[1]*x
  31. return f
  32.  
  33. def fitFuncDiff(p, x):
  34. '''
  35. Differential of fit function
  36. '''
  37. df = p[1]
  38. return df
  39.  
  40. def calcChiSq(p, x, y, xerr, yerr):
  41. '''
  42. Error function for fit
  43. '''
  44. e = (y - fitFunc(p, x))/(np.sqrt(yerr**2 + fitFuncDiff(p, x)**2*xerr**2))
  45. return e
  46. fig = plt.figure(figsize = (8, 6))
  47. plt.errorbar(xdata, ydata, yerror, xerror, marker=".", linestyle = '')
  48. plt.show
  49. linearModel_1 = odr.Model(fitFunc)
  50. fitData_1 = odr.Data(xdata, ydata, xdata, ydata)
  51. odrObject_1 = odr.ODR(fitData_1, linearModel_1, beta0=[1.,1.])
  52. odrFit_1 = odrObject_1.run()
  53. odrFit_1.pprint()
  54. plt.plot(xdata, fitFunc(odrFit_1.beta, xdata))
  55. chi_array = calcChiSq(odrFit_1.beta, xdata, ydata, xerror, yerror)**2
  56. chisqndf = np.sum(chi_array)/np.sqrt(len(xdata)-2)
  57. print("ChiSq/NDF: {0:.3f}".format(chisqndf))
  58. #
  59. #
  60. #
  61. nPoints = 4
  62.  
  63. BetaArr = np.zeros(4)
  64. PubArr = np.zeros(4)
  65. BetaErr = np.zeros(4)
  66. PubErr = np.zeros(4)
  67.  
  68. BetaArr = [popt1[1], popt2[1], popt3[1], popt4[1]]
  69. PubArr = [8.04, 8.63, 7.47, 6.40]
  70. BetaErr = [FWHM1_1,FWHM1_2,FWHM1_3,FWHM1_4]
  71. PubErr = [0.1,0.1,0.1,0.1]
  72.  
  73. xData = AlphaArr
  74. yData = PubArr
  75. xError = AlphaErr
  76. yError = PubErr
  77.  
  78. def fitFunc(p, x):
  79. '''
  80. Fit function
  81. '''
  82. f = p[0] + p[1]*x
  83. return f
  84.  
  85. def fitFuncDiff(p, x):
  86. '''
  87. Differential of fit function
  88. '''
  89. df = p[1]
  90. return df
  91.  
  92. def calcChiSq(p, x, y, xerr, yerr):
  93. '''
  94. Error function for fit
  95. '''
  96. e = (y - fitFunc(p, x))/(np.sqrt(yerr**2 + fitFuncDiff(p, x)**2*xerr**2))
  97. return e
  98. fig = plt.figure(figsize = (8, 6))
  99. plt.errorbar(xdata, ydata, yerror, xerror, marker=".", linestyle = '')
  100. plt.show
  101. linearModel_1 = odr.Model(fitFunc)
  102. fitData_1 = odr.Data(xdata, ydata, xdata, ydata)
  103. odrObject_1 = odr.ODR(fitData_1, linearModel_1, beta0=[1.,1.])
  104. odrFit_1 = odrObject_1.run()
  105. odrFit_1.pprint()
  106. plt.plot(xdata, fitFunc(odrFit_1.beta, xdata))
  107. chi_array = calcChiSq(odrFit_1.beta, xdata, ydata, xerror, yerror)**2
  108. chisqndf = np.sum(chi_array)/np.sqrt(len(xdata)-2)
  109. print("ChiSq/NDF: {0:.3f}".format(chisqndf))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement