fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. # Convert wavelength to frequency for each filter
  5. wavelengths_nm = [405, 450, 500, 550, 600, 670] # Wavelengths in nm
  6. wavelengths_m = [wl * 1e-9 for wl in wavelengths_nm] # Convert nm to meters
  7. speed_of_light = 3e8 # m/s
  8. frequencies = [speed_of_light / wl for wl in wavelengths_m] # Calculate frequency in Hz
  9.  
  10. # Define constants
  11. planck_constant = 6.626e-34 # Planck's constant in J*s
  12. electron_charge = 1.602e-19 # Charge of an electron in coulombs
  13.  
  14. # Calculate kinetic energy in eV
  15. kinetic_energy_eV = [planck_constant * freq / electron_charge for freq in frequencies]
  16.  
  17. # Plot the graph
  18. plt.figure(figsize=(8, 6))
  19. plt.plot(frequencies, kinetic_energy_eV, 'bo', label='Data points')
  20. plt.xlabel('Frequency (Hz)')
  21. plt.ylabel('Initial Kinetic Energy (eV)')
  22. plt.title('Initial Kinetic Energy vs. Frequency')
  23. plt.grid(True)
  24.  
  25. # Perform linear regression to fit a straight line
  26. slope, intercept = np.polyfit(frequencies, kinetic_energy_eV, 1)
  27. plt.plot(frequencies, np.polyval([slope, intercept], frequencies), 'r-', label='Linear fit')
  28.  
  29. # Set axis limits
  30. plt.xlim(0, 8e14)
  31. plt.ylim(-2, 2)
  32.  
  33. # Display slope and y-intercept
  34. print("Slope:", slope, "eV*s")
  35. print("Y-intercept:", intercept, "eV")
  36.  
  37. # Show legend and plot
  38. plt.legend()
  39. plt.show()
  40.  
Success #stdin #stdout 0.78s 54544KB
stdin
Standard input is empty
stdout
Slope: 4.136079900124844e-15 eV*s
Y-intercept: -3.625973214694716e-16 eV