Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. #Copper
  2.  
  3. sheets, Counts1, Counts2, Counts3 = np.loadtxt('AttenuationCopper.csv', delimiter = ',', unpack = True)
  4.  
  5. sheetthicknessC = 0.66 #cm
  6. thickC = sheets*sheetthicknessC #total thickness
  7. errorsheetC = 0.005 #cm
  8. errorthickC = sheets*errorsheetC
  9. countsError = np.sqrt(AvgCountsC)
  10.  
  11. time = 20
  12. Distance = 12.5 #cm
  13.  
  14. AvgCountsC = (Counts1+Counts2+Counts3)/3
  15. RateC = AvgCountsC/time
  16. errorRateC = np.sqrt((countsError/AvgCountsC)**2 + (timeError/time)**2)
  17.  
  18.  
  19. def fitLine(p, x):
  20. '''
  21. Straight line
  22. '''
  23. f = p[0] + p[1]*x
  24. return f
  25. #
  26. def fitError(p, x, y, xerr, yerr):
  27. '''
  28. Error function for straight line fit
  29. '''
  30. e = (y - fitLine(p, x))/(np.sqrt(yerr**2 + p[1]**2*xerr**2))
  31. return e
  32. #
  33. # Set initial values of fit parameters, run fit
  34.  
  35. xDataC = thickC
  36. yDataC = RateC
  37. xErrorC = errorthickC
  38. yErrorC = errorRateC
  39.  
  40. nPoints = 7
  41.  
  42. pInit = [1.0, -1.0]
  43. out = least_squares(fitError, pInit, args=(xDataC, yDataC, xErrorC, yErrorC))
  44. #
  45. fitOK = out.success
  46. #
  47. # Test if fit failed
  48. if not fitOK:
  49. print(" ")
  50. print("Fit failed")
  51. else:
  52. #
  53. # get output
  54. pFinal = out.x
  55. cVal = pFinal[0]
  56. mVal = pFinal[1]
  57. #
  58. # Calculate chis**2 per point, summed chi**2 and chi**2/NDF
  59. chiarr = fitError(pFinal, xDataC, yDataC, xErrorC, yErrorC)**2
  60. chisq = np.sum(chiarr)
  61. NDF = nPoints - 2
  62. redchisq = chisq/NDF
  63. #
  64. np.set_printoptions(precision = 3)
  65. print(" ")
  66. print("Fit quality:")
  67. print("chisq per point = \n",chiarr)
  68. print("chisq = {:5.2f}, chisq/NDF = {:5.2f}.".format(chisq, redchisq))
  69. #
  70. # Compute covariance
  71. jMat = out.jac
  72. jMat2 = np.dot(jMat.T, jMat)
  73. detJmat2 = np.linalg.det(jMat2)
  74. #
  75. if detJmat2 < 1E-32:
  76. print("Value of determinat detJmat2",detJmat2)
  77. print("Matrix singular, error calculation failed.")
  78. print(" ")
  79. print("Parameters returned by fit:")
  80. print("Intercept = {:5.2f}".format(cVal))
  81. print("Gradient = {:5.2f}".format(mVal))
  82. print(" ")
  83. cErr = 0.0
  84. mErr = 0.0
  85. else:
  86. covar = np.linalg.inv(jMat2)
  87. cErr = np.sqrt(covar[0, 0])
  88. mErr = np.sqrt(covar[1, 1])
  89. #
  90. print(" ")
  91. print("Parameters returned by fit:")
  92. print("Intercept (Sin theta i) = {:5.2f} +- {:5.2f}".format(cVal, cErr))
  93. print("Gradient () = {:5.2f} +- {:5.2f}".format(mVal, mErr))
  94. print(" ")
  95. #
  96. # Calculate fitted function values
  97. fitDataC = fitLine(pFinal, xDataC)
  98. #
  99.  
  100.  
  101. # Plot data
  102. fig = plt.figure(figsize = (8, 6))
  103. plt.title('Data with fit')
  104. plt.xlabel('Thickness of sheets')
  105. plt.ylabel('Rate')
  106. plt.errorbar(xDataC, yDataC, xerr = xErrorC, yerr = yErrorC, fmt='r', \
  107. linestyle = '', label = "Data")
  108. plt.plot(xDataC, fitDataC, color = 'b', linestyle = '-', label = "Fit")
  109. #plt.xlim(1.0, 7.0)
  110. #plt.ylim(0.0, 3.0)
  111. plt.grid(color = 'g')
  112.  
  113. plt.savefig("Copper.png")
  114. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement