Skip to content

Commit 66a8f93

Browse files
committed
applied all suggested changes from code review
1 parent d38f52c commit 66a8f93

File tree

2 files changed

+52
-59
lines changed

2 files changed

+52
-59
lines changed

physics/period_of_pendulum.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Title : Computing the time period of a simple pendulum
3+
4+
The simple pendulum is a mechanical system that sways or moves in an
5+
oscillatory motion. The simple pendulum comprises of a small bob of
6+
mass m suspended by a thin string of length L and secured to a platform
7+
at its upper end. Its motion occurs in a vertical plane and is mainly
8+
driven by gravitational force. The period of the pendulum depends on the
9+
length of the string and the amplitude (the maximum angle) of oscillation.
10+
However, the effect of the amplitude can be ignored if the amplitude is
11+
small. It should be noted that the period does not depend on the mass of
12+
the bob.
13+
14+
For small amplitudes, the period of a simple pendulum is given by the
15+
following approximation:
16+
T ≈ 2π * √(L / g)
17+
18+
where:
19+
L = length of string from which the bob is hanging (in m)
20+
g = acceleration due to gravity (approx 9.8 m/s²)
21+
22+
Reference : https://byjus.com/jee/simple-pendulum/
23+
"""
24+
from math import pi
25+
26+
from scipy.constants import g
27+
28+
29+
def period_of_pendulum(length: float) -> float:
30+
"""
31+
>>> period_of_pendulum(1.23)
32+
2.2252155506257845
33+
>>> period_of_pendulum(2.37)
34+
3.0888278441908574
35+
>>> period_of_pendulum(5.63)
36+
4.76073193364765
37+
>>> period_of_pendulum(-12)
38+
Traceback (most recent call last):
39+
...
40+
ValueError: The length should be non-negative
41+
>>> period_of_pendulum(0)
42+
0.0
43+
"""
44+
if length < 0:
45+
raise ValueError("The length should be non-negative")
46+
return (2 * pi) * (length / g) ** 0.5
47+
48+
49+
if __name__ == "__main__":
50+
import doctest
51+
52+
doctest.testmod()

physics/time_period_simple_pendulum.py

-59
This file was deleted.

0 commit comments

Comments
 (0)