import numpy as np
import matplotlib.pyplot as plt

# Convert wavelength to frequency for each filter
wavelengths_nm = [405, 450, 500, 550, 600, 670]  # Wavelengths in nm
wavelengths_m = [wl * 1e-9 for wl in wavelengths_nm]  # Convert nm to meters
speed_of_light = 3e8  # m/s
frequencies = [speed_of_light / wl for wl in wavelengths_m]  # Calculate frequency in Hz

# Define constants
planck_constant = 6.626e-34  # Planck's constant in J*s
electron_charge = 1.602e-19  # Charge of an electron in coulombs

# Calculate kinetic energy in eV
kinetic_energy_eV = [planck_constant * freq / electron_charge for freq in frequencies]

# Plot the graph
plt.figure(figsize=(8, 6))
plt.plot(frequencies, kinetic_energy_eV, 'bo', label='Data points')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Initial Kinetic Energy (eV)')
plt.title('Initial Kinetic Energy vs. Frequency')
plt.grid(True)

# Perform linear regression to fit a straight line
slope, intercept = np.polyfit(frequencies, kinetic_energy_eV, 1)
plt.plot(frequencies, np.polyval([slope, intercept], frequencies), 'r-', label='Linear fit')

# Set axis limits
plt.xlim(0, 8e14)
plt.ylim(-2, 2)

# Display slope and y-intercept
print("Slope:", slope, "eV*s")
print("Y-intercept:", intercept, "eV")

# Show legend and plot
plt.legend()
plt.show()
