|
| 1 | +import math |
| 2 | +def bragg_angle(distance: float, order: int, wavelength: float) -> float: |
| 3 | + """ |
| 4 | + Calculate the Bragg diffraction angle using the formula: |
| 5 | + sin(θ) = (n * λ) / (2 * d) |
| 6 | + |
| 7 | + Parameters: |
| 8 | + distance d (float): Distance between crystal planes (in meters). |
| 9 | + order n (int): Order of reflection. |
| 10 | + wavelength λ (float): Wavelength of the radiation (in meters). |
| 11 | + |
| 12 | + Examples: |
| 13 | + >>> bragg_angle(2.2e-10, 1, 2.2e-10) |
| 14 | + 30.0 |
| 15 | + |
| 16 | + >>> bragg_angle(5e-10, 2, 1e-10) |
| 17 | + 11.5 |
| 18 | + |
| 19 | + >>> bragg_angle(4e-10, 1, 4e-10) |
| 20 | + 30.0 |
| 21 | + |
| 22 | + # Test case for an invalid sine value (out of range) |
| 23 | + >>> bragg_angle(1e-10, 2, 3e-10) |
| 24 | + Traceback (most recent call last): |
| 25 | + ... |
| 26 | + ValueError: The calculated sine value is out of the valid range. |
| 27 | + """ |
| 28 | + sine_theta = (order * wavelength) / (2 * distance) |
| 29 | + if sine_theta > 1 or sine_theta < -1: |
| 30 | + raise ValueError("The calculated sine value is out of the valid range.") |
| 31 | + theta_radians = math.asin(sine_theta) |
| 32 | + theta_degrees = math.degrees(theta_radians) |
| 33 | + return round(theta_degrees, 1) |
| 34 | + |
| 35 | +if __name__ == "__main__": |
| 36 | + import doctest |
| 37 | + doctest.testmod(verbose=True) |
0 commit comments