Skip to content

Commit a1ff9a9

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

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

physics/Bragg_angle.py

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

0 commit comments

Comments
 (0)