Advertisement
Guest User

Franck-Hertz Data Graphs

a guest
Feb 21st, 2020
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. %matplotlib inline
  2. import numpy as np
  3. import math
  4. import matplotlib.pyplot as pl
  5.  
  6.  
  7. #1ST PART: Argon collected current vs accelerating voltage - peaks
  8.  
  9. #X = [1,2,3,4,5,6]
  10. #Y = [20.7, 30.2, 40.7, 52.1, 63.6, 76.1]
  11.  
  12. #pl.plot(X,Y,'o', label = 'Accelerating Voltage at each peak number')
  13. #pl.xlabel('Peak Number')
  14. #pl.ylabel('Accelerating Voltage')
  15. #pl.show()
  16.  
  17. # number of parameters for line fit (2)
  18. npar = 2
  19. # vector with data (Y); std of measurement error is sigma =.1
  20. sigma = 0.1
  21. # here is data to fit
  22. Y = np.array([20.7, 30.2, 40.7, 52.1, 63.6, 76.1])
  23. Yerror = 1
  24. # here are X positions of data
  25. X = np.array([1,2,3,4,5,6]);
  26. # number of data points
  27. nobs = len(X)
  28.  
  29. # create M – ones in first column, X in second column
  30. M = np.column_stack( (np.ones(nobs),X) )
  31.  
  32. # solve
  33. MTM = np.dot(M.transpose(),M)
  34. MTMINV = np.linalg.inv(MTM)
  35. MTY = np.dot(M.transpose(),Y)
  36.  
  37. P = np.dot(MTMINV,MTY) # SOLUTION
  38.  
  39. C = MTMINV * sigma**2 # COVARIANCE MATRIX
  40.  
  41. # compute RMS and print it
  42. Residuals = Y - np.dot(M,P)
  43. ChiSq = np.dot(Residuals.transpose(),Residuals)
  44. RMS = math.sqrt(ChiSq/nobs)
  45. #print('RMS = %f'%(RMS))
  46.  
  47. # plot the result of the fit
  48. pl.ion()
  49. pl.plot(X,Y,'ro', label = 'Accelerating Voltage at each peak number')
  50. pl.errorbar(X,Y,yerr=Yerror)
  51. xx = np.linspace(0.5,6.5,101)
  52. ModelY = P[0] + P[1]*xx
  53. pl.plot(xx,ModelY,'k')
  54. pl.xlabel('Peak Number')
  55. pl.ylabel('Accelerating Voltage (V)')
  56. pl.title('Accelerating Voltage at Peak Versus Peak Number')
  57. pl.show()
  58.  
  59. # print solution
  60. print(' Intercept= %f +/- %f'%( P[0],math.sqrt(C[0,0]) ))
  61. print(' Slope = %f +/- %f'%( P[1],math.sqrt(C[1,1]) ))
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. #2ND PART: Argon collected current vs accelerating voltage - dips
  72.  
  73. #X = [1,2,3,4,5,6]
  74. #Y = [23.8, 35.2, 46.3, 57.6, 69, 79.6]
  75.  
  76. #pl.plot(X,Y,'o', label = 'Accelerating Voltage at each peak number')
  77. #pl.xlabel('Peak Number')
  78. #pl.ylabel('Accelerating Voltage')
  79. #pl.show()
  80.  
  81. # number of parameters for line fit (2)
  82. npar = 2
  83. # vector with data (Y); std of measurement error is sigma =.1
  84. sigma = 0.1
  85. # here is data to fit
  86. Y = np.array([23.8, 35.2, 46.3, 57.6, 69, 79.6])
  87. Yerror = 1
  88. # here are X positions of data
  89. X = np.array([1,2,3,4,5,6]);
  90. # number of data points
  91. nobs = len(X)
  92.  
  93. # create M – ones in first column, X in second column
  94. M = np.column_stack( (np.ones(nobs),X) )
  95.  
  96. # solve
  97. MTM = np.dot(M.transpose(),M)
  98. MTMINV = np.linalg.inv(MTM)
  99. MTY = np.dot(M.transpose(),Y)
  100.  
  101. P = np.dot(MTMINV,MTY) # SOLUTION
  102.  
  103. C = MTMINV * sigma**2 # COVARIANCE MATRIX
  104.  
  105. # compute RMS and print it
  106. Residuals = Y - np.dot(M,P)
  107. ChiSq = np.dot(Residuals.transpose(),Residuals)
  108. RMS = math.sqrt(ChiSq/nobs)
  109. #print('RMS = %f'%(RMS))
  110.  
  111. # plot the result of the fit
  112. pl.ion()
  113. pl.plot(X,Y,'ro', label = 'Accelerating Voltage at each peak number')
  114. pl.errorbar(X,Y,yerr=Yerror)
  115. xx = np.linspace(0.5,6.5,101)
  116. ModelY = P[0] + P[1]*xx
  117. pl.plot(xx,ModelY,'k')
  118. pl.xlabel('Dip Number')
  119. pl.ylabel('Accelerating Voltage (V)')
  120. pl.title('Accelerating Voltage at Dip Versus Dip Number')
  121. pl.show()
  122.  
  123. # print solution
  124. print(' Intercept= %f +/- %f'%( P[0],math.sqrt(C[0,0]) ))
  125. print(' Slope = %f +/- %f'%( P[1],math.sqrt(C[1,1]) ))
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. #3RD PART: Neon collected current vs accelerating voltage - peaks
  134.  
  135. #X = [1,2,3,4]
  136. #Y = [19.2,38.3,61.6,80.6]
  137.  
  138. #pl.plot(X,Y,'o', label = 'Accelerating Voltage at each peak number')
  139. #pl.xlabel('Peak Number')
  140. #pl.ylabel('Accelerating Voltage')
  141. #pl.show()
  142.  
  143. # number of parameters for line fit (2)
  144. npar = 2
  145. # vector with data (Y); std of measurement error is sigma =.1
  146. sigma = 0.1
  147. # here is data to fit
  148. Y = np.array([19.2,38.3,61.6,80.6])
  149. Yerror = 1
  150. # here are X positions of data
  151. X = np.array([1,2,3,4]);
  152. # number of data points
  153. nobs = len(X)
  154.  
  155. # create M – ones in first column, X in second column
  156. M = np.column_stack( (np.ones(nobs),X) )
  157.  
  158. # solve
  159. MTM = np.dot(M.transpose(),M)
  160. MTMINV = np.linalg.inv(MTM)
  161. MTY = np.dot(M.transpose(),Y)
  162.  
  163. P = np.dot(MTMINV,MTY) # SOLUTION
  164.  
  165. C = MTMINV * sigma**2 # COVARIANCE MATRIX
  166.  
  167. # compute RMS and print it
  168. Residuals = Y - np.dot(M,P)
  169. ChiSq = np.dot(Residuals.transpose(),Residuals)
  170. RMS = math.sqrt(ChiSq/nobs)
  171. #print('RMS = %f'%(RMS))
  172.  
  173. # plot the result of the fit
  174. pl.ion()
  175. pl.plot(X,Y,'ro', label = 'Accelerating Voltage at each peak number')
  176. pl.errorbar(X,Y,yerr=Yerror)
  177. xx = np.linspace(0.99,4.01,101)
  178. ModelY = P[0] + P[1]*xx
  179. pl.plot(xx,ModelY,'k')
  180. pl.xlabel('Peak Number')
  181. pl.ylabel('Accelerating Voltage (V)')
  182. pl.title('Accelerating Voltage at Peak Versus Peak Number (Neon)')
  183. pl.show()
  184.  
  185. # print solution
  186. print(' Intercept= %f +/- %f'%( P[0],math.sqrt(C[0,0]) ))
  187. print(' Slope = %f +/- %f'%( P[1],math.sqrt(C[1,1]) ))
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195. #4TH PART: Neon collected current vs accelerating voltage - dips
  196.  
  197. #X = [1,2,3]
  198. #Y = [25.5,46.7,69.4]
  199.  
  200. #pl.plot(X,Y,'o', label = 'Accelerating Voltage at each peak number')
  201. #pl.xlabel('Peak Number')
  202. #pl.ylabel('Accelerating Voltage')
  203. #pl.show()
  204.  
  205. # number of parameters for line fit (2)
  206. npar = 2
  207. # vector with data (Y); std of measurement error is sigma =.1
  208. sigma = 0.1
  209. # here is data to fit
  210. Y = np.array([25.5,46.7,69.4])
  211. Yerror = 1
  212. # here are X positions of data
  213. X = np.array([1,2,3]);
  214. # number of data points
  215. nobs = len(X)
  216.  
  217. # create M – ones in first column, X in second column
  218. M = np.column_stack( (np.ones(nobs),X) )
  219.  
  220. # solve
  221. MTM = np.dot(M.transpose(),M)
  222. MTMINV = np.linalg.inv(MTM)
  223. MTY = np.dot(M.transpose(),Y)
  224.  
  225. P = np.dot(MTMINV,MTY) # SOLUTION
  226.  
  227. C = MTMINV * sigma**2 # COVARIANCE MATRIX
  228.  
  229. # compute RMS and print it
  230. Residuals = Y - np.dot(M,P)
  231. ChiSq = np.dot(Residuals.transpose(),Residuals)
  232. RMS = math.sqrt(ChiSq/nobs)
  233. #print('RMS = %f'%(RMS))
  234.  
  235. # plot the result of the fit
  236. pl.ion()
  237. pl.plot(X,Y,'ro', label = 'Accelerating Voltage at each peak number')
  238. pl.errorbar(X,Y,yerr=Yerror)
  239. xx = np.linspace(0.99,3.01,101)
  240. ModelY = P[0] + P[1]*xx
  241. pl.plot(xx,ModelY,'k')
  242. pl.xlabel('Dip Number')
  243. pl.ylabel('Accelerating Voltage (V)')
  244. pl.title('Accelerating Voltage at Dip Versus Dip Number (Neon)')
  245. pl.show()
  246.  
  247. # print solution
  248. print(' Intercept= %f +/- %f'%( P[0],math.sqrt(C[0,0]) ))
  249. print(' Slope = %f +/- %f'%( P[1],math.sqrt(C[1,1]) ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement