Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions physics/photoelectric_effect.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,49 @@
return max(PLANCK_CONSTANT_JS * frequency - work_function, 0)


def calculate_work_function(
frequency: float, kinetic_energy: float, in_ev: bool = False
) -> float:
"""
Calculates the work function of a surface using the given frequency and kinetic energy.

Check failure on line 68 in physics/photoelectric_effect.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

physics/photoelectric_effect.py:68:89: E501 Line too long (91 > 88)

Parameters:
frequency (float): Frequency of the electromagnetic wave.
kinetic_energy (float): Kinetic energy of the emitted electron.
in_ev (bool, optional): Set to True if frequency and kinetic energy are in eV.

Returns:
float: The calculated work function of the surface.

Raises:
ValueError: If the frequency is negative.
TypeError: If non-numeric values are provided for frequency or kinetic energy.

Usage example:
>>> calculate_work_function(1e6, 2)
-6.62607015e-28
>>> calculate_work_function(1e6, 2, True)
-8.271335392e-09
>>> calculate_work_function(1e16, 39.357, True)
24.542
>>> calculate_work_function(-9, 20)
Traceback (most recent call last):
...
ValueError: Frequency can't be negative.
>>> calculate_work_function(1000, "a")
Traceback (most recent call last):
...
TypeError: Both frequency and kinetic energy must be numbers.
"""

if frequency < 0:
raise ValueError("Frequency can't be negative.")

if in_ev:
return PLANCK_CONSTANT_EVS * frequency - kinetic_energy
return PLANCK_CONSTANT_JS * frequency - kinetic_energy


if __name__ == "__main__":
import doctest

Expand Down
Loading