Skip to content

Commit 3644ec7

Browse files
authored
Create Bragg_angle.py
1 parent 59ff87d commit 3644ec7

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

physics/Bragg_angle.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
Returns:
13+
float: The Bragg diffraction angle θ in degrees, rounded to the specified number of decimal places.
14+
15+
Examples:
16+
>>> bragg_angle(2.2e-10, 1, 2.2e-10)
17+
30.0
18+
19+
>>> bragg_angle(5e-10, 2, 1e-10)
20+
11.5
21+
22+
>>> bragg_angle(4e-10, 1, 4e-10)
23+
30.0
24+
25+
# Test case for an invalid sine value (out of range)
26+
>>> bragg_angle(1e-10, 2, 3e-10)
27+
Traceback (most recent call last):
28+
...
29+
ValueError: The calculated sine value is out of the valid range.
30+
"""
31+
sin_theta = (order * wavelength) / (2 * distance)
32+
if sin_theta > 1 or sin_theta < -1:
33+
raise ValueError("The calculated sine value is out of the valid range.")
34+
theta_radians = math.asin(sin_theta)
35+
theta_degrees = math.degrees(theta_radians)
36+
return round(theta_degrees, 1)
37+
38+
if __name__ == "__main__":
39+
import doctest
40+
doctest.testmod(verbose=True)

0 commit comments

Comments
 (0)