Skip to content

Commit 8439fa8

Browse files
pluto-tofutianyizheng02pre-commit-ci[bot]
authored
Added the algorithm to compute the time period of a simple pendulum (TheAlgorithms#10265)
* Added the algorithm to compute the time period of a simple pendulum * imported g form scipy and changed doctests accordingly * fixed formatting * applied all suggested changes from code review * Apply suggestions from code review * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Tianyi Zheng <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 8767d1d commit 8439fa8

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

physics/period_of_pendulum.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
25+
from math import pi
26+
27+
from scipy.constants import g
28+
29+
30+
def period_of_pendulum(length: float) -> float:
31+
"""
32+
>>> period_of_pendulum(1.23)
33+
2.2252155506257845
34+
>>> period_of_pendulum(2.37)
35+
3.0888278441908574
36+
>>> period_of_pendulum(5.63)
37+
4.76073193364765
38+
>>> period_of_pendulum(-12)
39+
Traceback (most recent call last):
40+
...
41+
ValueError: The length should be non-negative
42+
>>> period_of_pendulum(0)
43+
0.0
44+
"""
45+
if length < 0:
46+
raise ValueError("The length should be non-negative")
47+
return 2 * pi * (length / g) ** 0.5
48+
49+
50+
if __name__ == "__main__":
51+
import doctest
52+
53+
doctest.testmod()

0 commit comments

Comments
 (0)