Skip to content

Commit 5d3330a

Browse files
authored
Update photoelectric_effect.py
added work function calculation
1 parent 03a4251 commit 5d3330a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

physics/photoelectric_effect.py

+37
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,44 @@ def maximum_kinetic_energy(
5959
if in_ev:
6060
return max(PLANCK_CONSTANT_EVS * frequency - work_function, 0)
6161
return max(PLANCK_CONSTANT_JS * frequency - work_function, 0)
62+
def work_function(
63+
frequency: float, kinetic_energy: float, in_ev: bool = False
64+
) -> float:
65+
"""
66+
Calculates the work function of a surface using the given
67+
frequency and kinetic energy.
68+
69+
Parameters:
70+
frequency (float): Frequency of the electromagnetic wave.
71+
kinetic_energy (float): Kinetic energy of emitted electron.
72+
in_ev (bool, optional): True if frequency and
73+
kinetic energy are in eV.
74+
75+
Returns:
76+
float: The calculated work function of the surface.
6277
78+
Raises:
79+
ValueError: If the frequency is negative.
80+
81+
Usage example:
82+
>>> work_function(1e6, 2)
83+
-6.62607015e-28
84+
>>> work_function(1e6, 2, True)
85+
-8.271335392e-09
86+
>>> work_function(1e16, 39.357, True)
87+
24.542
88+
>>> work_function(-9, 20)
89+
Traceback (most recent call last):
90+
...
91+
ValueError: Frequency can't be negative.
92+
"""
93+
94+
if frequency < 0:
95+
raise ValueError("Frequency can't be negative.")
96+
97+
if in_ev:
98+
return PLANCK_CONSTANT_EVS * frequency - kinetic_energy
99+
return PLANCK_CONSTANT_JS * frequency - kinetic_energy
63100

64101
if __name__ == "__main__":
65102
import doctest

0 commit comments

Comments
 (0)