diff --git a/DIRECTORY.md b/DIRECTORY.md index 5c9cba5a461a..4f0e137a8148 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1116,6 +1116,7 @@ * [Center Of Mass](physics/center_of_mass.py) * [Centripetal Force](physics/centripetal_force.py) * [Coulombs Law](physics/coulombs_law.py) + * [Diffraction](physics/diffraction.py) * [Doppler Frequency](physics/doppler_frequency.py) * [Escape Velocity](physics/escape_velocity.py) * [Faraday Lenz Law](physics/faraday_lenz_law.py) @@ -1479,7 +1480,6 @@ * [Sol1](project_euler/problem_800/sol1.py) ## [Quantum](quantum) - * [Q Fourier Transform](quantum/q_fourier_transform.py) * [Shor Algorithm](quantum/shor_algorithm.py) ## [Scheduling](scheduling) diff --git a/physics/diffraction.py b/physics/diffraction.py new file mode 100644 index 000000000000..12a205bae07a --- /dev/null +++ b/physics/diffraction.py @@ -0,0 +1,98 @@ +import math + + +def check_min_intensity( + slit_width: float = 1.0, diff_angle: float = 0.0, wavelength: float = 100.0 +) -> bool: + """ + Checks whether the intensity is at a minimum in a diffraction pattern. + + Args: + slit_width: The width of the slit in millimeters. + diff_angle: The diffraction angle in radians. + wavelength: The wavelength of light in nanometers. + + Returns: + True if minimum intensity is met; otherwise, False. + + >>> check_min_intensity(4, 0.25, 300) + False + >>> check_min_intensity(1, 0.0001, 100) + True + """ + wavelength *= 10**-6 + n_val = round(slit_width * (diff_angle) / wavelength, 5) + r_val = n_val - math.floor(n_val) == 0 + return r_val + + +def check_max_intensity( + slit_width: float = 1.0, diff_angle: float = 0.0, wavelength: float = 100.0 +) -> bool: + """ + Checks whether the intensity is at a maximum in a diffraction pattern. + + Args: + slit_width: The width of the slit in millimeters. + diff_angle: The diffraction angle in radians. + wavelength: The wavelength of light in nanometers. + + Returns: + True if maximum intensity is met; otherwise, False. + + >>> check_max_intensity(1, 0.001, 100) + False + >>> check_max_intensity(1, 0.00005, 100) + True + """ + wavelength *= 10**-6 + n_val = round(((2 * slit_width * diff_angle) - wavelength) / (2 * wavelength), 4) + return n_val - math.floor(n_val) == 0 + + +def intensity_single_slit( + slit_width: float = 1.0, diff_angle: float = 0.0, wavelength: float = 100.0 +) -> float: + """ + Computes the intensity for a single-slit diffraction pattern. + + Args: + slit_width: The width of the slit in millimeters. + diff_angle: The diffraction angle in radians. + wavelength: The wavelength of light in nanometers. + + Returns: + The intensity of the diffraction pattern. + + >>> intensity_single_slit(1, 0.0005, 100) + 0.9999999999177533 + """ + beta = math.pi * slit_width * (math.sin(diff_angle) / wavelength) + return (math.sin(beta) / beta) ** 2 + + +def intensity_double_slit(path_diff: float = 0, intensity_max: float = 1.0) -> float: + """ + Computes the intensity for a double-slit diffraction pattern. + + Args: + path_diff: The path difference in the two waves. + intensity_max: The maximum intensity. + + Returns: + The intensity of the diffraction pattern. + + >>> intensity_double_slit(0, 1) + 4.0 + >>> intensity_double_slit(0.001, 1) + 3.999999000000084 + >>> intensity_double_slit(0) + 4.0 + """ + return 4 * intensity_max * (math.cos(path_diff / 2)) ** 2 + + +if __name__ == "__main__": + import doctest + + doctest.testmod()