Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. def calcChiSq(p, x, y, yerr):
  2. '''
  3. Error function for fit
  4. '''
  5. e = (y - fitFunc(x, *p))/yerr
  6. return e
  7.  
  8. # First do the plot of Refractive index versus wavelength
  9. fig = plt.figure(figsize = (8, 6))
  10.  
  11.  
  12. xdata = wavelengths
  13. ydata = refractive_indices
  14.  
  15. xerror = 0
  16. yerror = d_refractive_indices
  17.  
  18. fig = plt.figure(figsize = (8, 6))
  19. plt.title('Refractive index as a function of wavelength')
  20. plt.xlabel('Wavelength (m)')
  21. plt.ylabel('Refractive Index')
  22. plt.grid(color = 'g')
  23. plt.errorbar(xdata, ydata, yerror, xerror, marker=".", linestyle = '')
  24. plt.show
  25.  
  26.  
  27.  
  28. # Now we need to do a fit to the data. The fitting function is defined below, and is non-linear. You should update the variable
  29. # mu_0 to reflect _your_ own value for mu_0, the refractive index at lambda_0
  30. lambda_0 = 4.7999e-7
  31. mu_0 = 1.747
  32.  
  33. def fitFunc(x, alpha, beta):
  34. return mu_0 + alpha*x + beta*x**2
  35.  
  36. # Do the fit with our variables. You might need to edit the fit bounds to get an optimised fit
  37.  
  38. popt, pcov = curve_fit(fitFunc, (wavelengths-lambda_0), refractive_indices, bounds=([-2.5E5, -2.5E11], [-2.0E5, -2.0E11]))
  39. print('Alpha: {0:.3e}, Beta: {1:.3e}'.format(*popt))
  40.  
  41. # Plot the fit
  42. plt.plot(wavelengths, fitFunc( (wavelengths-lambda_0), *popt))
  43.  
  44. # Calculate the ChiSq/NDF of the fit, and print
  45. chiarr = calcChiSq(popt, xdata, ydata, yerror)**2
  46. chisq = np.sum(chiarr)
  47. NDF = nPoints - nPars
  48. chisqndf = chisq/NDF
  49. print('ChiSq/NDF: {0:.2f}'.format(chisqndf))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement