From c0c1648576c0fcb4540ecff227295dafcebb81da Mon Sep 17 00:00:00 2001 From: nikhitha79 <115790230+nikhitha79@users.noreply.github.com> Date: Tue, 15 Oct 2024 11:26:04 +0530 Subject: [PATCH 1/2] Update photoelectric_effect.py added function to calculate work function --- physics/photoelectric_effect.py | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/physics/photoelectric_effect.py b/physics/photoelectric_effect.py index 3a0138ffe045..df3a45205fc4 100644 --- a/physics/photoelectric_effect.py +++ b/physics/photoelectric_effect.py @@ -60,6 +60,45 @@ def maximum_kinetic_energy( return max(PLANCK_CONSTANT_EVS * frequency - work_function, 0) 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. + + 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 From b9481f3f9a860c1b955215be6b57526e6cb4d69e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Oct 2024 05:58:41 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/photoelectric_effect.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/physics/photoelectric_effect.py b/physics/photoelectric_effect.py index df3a45205fc4..48e7aa70a604 100644 --- a/physics/photoelectric_effect.py +++ b/physics/photoelectric_effect.py @@ -60,7 +60,10 @@ def maximum_kinetic_energy( return max(PLANCK_CONSTANT_EVS * frequency - work_function, 0) return max(PLANCK_CONSTANT_JS * frequency - work_function, 0) -def calculate_work_function(frequency: float, kinetic_energy: float, in_ev: bool = False) -> float: + +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. @@ -100,6 +103,7 @@ def calculate_work_function(frequency: float, kinetic_energy: float, in_ev: bool return PLANCK_CONSTANT_EVS * frequency - kinetic_energy return PLANCK_CONSTANT_JS * frequency - kinetic_energy + if __name__ == "__main__": import doctest