From 6eb35566c57aa9a24fa5f5aaad7c93c2ae4d3b87 Mon Sep 17 00:00:00 2001 From: pluto-tofu Date: Wed, 11 Oct 2023 11:59:55 +0530 Subject: [PATCH 1/6] Added the algorithm to compute the time period of a simple pendulum --- physics/time_period_simple_pendulum.py | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 physics/time_period_simple_pendulum.py diff --git a/physics/time_period_simple_pendulum.py b/physics/time_period_simple_pendulum.py new file mode 100644 index 000000000000..ac21f9e170e6 --- /dev/null +++ b/physics/time_period_simple_pendulum.py @@ -0,0 +1,58 @@ +""" +Title : Computing the time period of a simple pendulum + +A simple pendulum is a mechanical arrangement that demonstrates +periodic motion. The simple pendulum comprises of a small bob of mass 'm' +suspended by a thin string secured to a platform at its upper end of +length L. + +The simple pendulum is a mechanical system that sways or moves in an +oscillatory motion. This motion occurs in a vertical plane and is mainly +driven by gravitational force. Interestingly, the bob that is suspended at +the end of a thread is very light; somewhat, we can say it is even +massless. The period of a simple pendulum can be made extended by +increasing the length string while taking the measurements from the point +of suspension to the middle of the bob. However, it should be noted that if +the mass of the bob is changed, the period will remain unchanged. The +period is influenced mainly by the position of the pendulum in relation to +Earth, as the strength of the gravitational field is not uniform everywhere. + +The Time Period of a simple pendulum is given by : + +T = 2 * π * (l/g)^0.5 + +where : + +T = Time period of the simple pendulum (in s) +π = Mathematical constant (value taken : 3.14159) +l = length of string from which the bob is hanging (in m) +g = Acceleration due to gravity (value taken : 9.8 m/s^2) + +Reference : https://byjus.com/jee/simple-pendulum/ +""" + + +def time_period_simple_pendulum(length: float) -> float: + """ + >>> time_period_simple_pendulum(1.23) + 2.2259685262423705 + >>> time_period_simple_pendulum(2.37) + 3.089873051721361 + >>> time_period_simple_pendulum(5.63) + 4.762342885477521 + >>> time_period_simple_pendulum(-12) + Traceback (most recent call last): + ... + ValueError: The length should be non-negative + >>> time_period_simple_pendulum(0) + 0.0 + """ + if length < 0: + raise ValueError("The length should be non-negative") + return (2 * 3.14159) * (length / 9.8) ** 0.5 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From ebb4ebd297bfdd425a3d4786f1bb30f30c6a46cb Mon Sep 17 00:00:00 2001 From: pluto-tofu Date: Mon, 23 Oct 2023 11:20:44 +0530 Subject: [PATCH 2/6] imported g form scipy and changed doctests accordingly --- physics/time_period_simple_pendulum.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/physics/time_period_simple_pendulum.py b/physics/time_period_simple_pendulum.py index ac21f9e170e6..ff63fd9b2550 100644 --- a/physics/time_period_simple_pendulum.py +++ b/physics/time_period_simple_pendulum.py @@ -30,16 +30,16 @@ Reference : https://byjus.com/jee/simple-pendulum/ """ - +from scipy.constants import g def time_period_simple_pendulum(length: float) -> float: """ >>> time_period_simple_pendulum(1.23) - 2.2259685262423705 + 2.2252136710666166 >>> time_period_simple_pendulum(2.37) - 3.089873051721361 + 3.088825235169592 >>> time_period_simple_pendulum(5.63) - 4.762342885477521 + 4.760727912429414 >>> time_period_simple_pendulum(-12) Traceback (most recent call last): ... @@ -49,7 +49,7 @@ def time_period_simple_pendulum(length: float) -> float: """ if length < 0: raise ValueError("The length should be non-negative") - return (2 * 3.14159) * (length / 9.8) ** 0.5 + return (2 * 3.14159) * (length / g) ** 0.5 if __name__ == "__main__": From d38f52c99714410aca2231c3b23072734e1b62e5 Mon Sep 17 00:00:00 2001 From: pluto-tofu Date: Mon, 23 Oct 2023 11:25:31 +0530 Subject: [PATCH 3/6] fixed formatting --- physics/time_period_simple_pendulum.py | 1 + 1 file changed, 1 insertion(+) diff --git a/physics/time_period_simple_pendulum.py b/physics/time_period_simple_pendulum.py index ff63fd9b2550..6b31b7f0dbff 100644 --- a/physics/time_period_simple_pendulum.py +++ b/physics/time_period_simple_pendulum.py @@ -32,6 +32,7 @@ """ from scipy.constants import g + def time_period_simple_pendulum(length: float) -> float: """ >>> time_period_simple_pendulum(1.23) From 66a8f93c0c51aa3a69be96414718b56563335d47 Mon Sep 17 00:00:00 2001 From: pluto-tofu Date: Sat, 28 Oct 2023 11:30:13 +0530 Subject: [PATCH 4/6] applied all suggested changes from code review --- physics/period_of_pendulum.py | 52 +++++++++++++++++++++++ physics/time_period_simple_pendulum.py | 59 -------------------------- 2 files changed, 52 insertions(+), 59 deletions(-) create mode 100644 physics/period_of_pendulum.py delete mode 100644 physics/time_period_simple_pendulum.py diff --git a/physics/period_of_pendulum.py b/physics/period_of_pendulum.py new file mode 100644 index 000000000000..8a6c05c954f7 --- /dev/null +++ b/physics/period_of_pendulum.py @@ -0,0 +1,52 @@ +""" +Title : Computing the time period of a simple pendulum + +The simple pendulum is a mechanical system that sways or moves in an +oscillatory motion. The simple pendulum comprises of a small bob of +mass m suspended by a thin string of length L and secured to a platform +at its upper end. Its motion occurs in a vertical plane and is mainly +driven by gravitational force. The period of the pendulum depends on the +length of the string and the amplitude (the maximum angle) of oscillation. +However, the effect of the amplitude can be ignored if the amplitude is +small. It should be noted that the period does not depend on the mass of +the bob. + +For small amplitudes, the period of a simple pendulum is given by the +following approximation: +T ≈ 2π * √(L / g) + +where: +L = length of string from which the bob is hanging (in m) +g = acceleration due to gravity (approx 9.8 m/s²) + +Reference : https://byjus.com/jee/simple-pendulum/ +""" +from math import pi + +from scipy.constants import g + + +def period_of_pendulum(length: float) -> float: + """ + >>> period_of_pendulum(1.23) + 2.2252155506257845 + >>> period_of_pendulum(2.37) + 3.0888278441908574 + >>> period_of_pendulum(5.63) + 4.76073193364765 + >>> period_of_pendulum(-12) + Traceback (most recent call last): + ... + ValueError: The length should be non-negative + >>> period_of_pendulum(0) + 0.0 + """ + if length < 0: + raise ValueError("The length should be non-negative") + return (2 * pi) * (length / g) ** 0.5 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/physics/time_period_simple_pendulum.py b/physics/time_period_simple_pendulum.py deleted file mode 100644 index 6b31b7f0dbff..000000000000 --- a/physics/time_period_simple_pendulum.py +++ /dev/null @@ -1,59 +0,0 @@ -""" -Title : Computing the time period of a simple pendulum - -A simple pendulum is a mechanical arrangement that demonstrates -periodic motion. The simple pendulum comprises of a small bob of mass 'm' -suspended by a thin string secured to a platform at its upper end of -length L. - -The simple pendulum is a mechanical system that sways or moves in an -oscillatory motion. This motion occurs in a vertical plane and is mainly -driven by gravitational force. Interestingly, the bob that is suspended at -the end of a thread is very light; somewhat, we can say it is even -massless. The period of a simple pendulum can be made extended by -increasing the length string while taking the measurements from the point -of suspension to the middle of the bob. However, it should be noted that if -the mass of the bob is changed, the period will remain unchanged. The -period is influenced mainly by the position of the pendulum in relation to -Earth, as the strength of the gravitational field is not uniform everywhere. - -The Time Period of a simple pendulum is given by : - -T = 2 * π * (l/g)^0.5 - -where : - -T = Time period of the simple pendulum (in s) -π = Mathematical constant (value taken : 3.14159) -l = length of string from which the bob is hanging (in m) -g = Acceleration due to gravity (value taken : 9.8 m/s^2) - -Reference : https://byjus.com/jee/simple-pendulum/ -""" -from scipy.constants import g - - -def time_period_simple_pendulum(length: float) -> float: - """ - >>> time_period_simple_pendulum(1.23) - 2.2252136710666166 - >>> time_period_simple_pendulum(2.37) - 3.088825235169592 - >>> time_period_simple_pendulum(5.63) - 4.760727912429414 - >>> time_period_simple_pendulum(-12) - Traceback (most recent call last): - ... - ValueError: The length should be non-negative - >>> time_period_simple_pendulum(0) - 0.0 - """ - if length < 0: - raise ValueError("The length should be non-negative") - return (2 * 3.14159) * (length / g) ** 0.5 - - -if __name__ == "__main__": - import doctest - - doctest.testmod() From 69d76244ed279c8e9f4e3c00dd788b0a80826b0d Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Mon, 30 Dec 2024 16:42:52 -0800 Subject: [PATCH 5/6] Apply suggestions from code review --- physics/period_of_pendulum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physics/period_of_pendulum.py b/physics/period_of_pendulum.py index 8a6c05c954f7..6e496a691adb 100644 --- a/physics/period_of_pendulum.py +++ b/physics/period_of_pendulum.py @@ -43,7 +43,7 @@ def period_of_pendulum(length: float) -> float: """ if length < 0: raise ValueError("The length should be non-negative") - return (2 * pi) * (length / g) ** 0.5 + return 2 * pi * (length / g) ** 0.5 if __name__ == "__main__": From fa3503c98ee5123ce0640a7033016770fc88cde5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 31 Dec 2024 00:43:20 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/period_of_pendulum.py | 1 + 1 file changed, 1 insertion(+) diff --git a/physics/period_of_pendulum.py b/physics/period_of_pendulum.py index 6e496a691adb..2e3c7bc3ef1e 100644 --- a/physics/period_of_pendulum.py +++ b/physics/period_of_pendulum.py @@ -21,6 +21,7 @@ Reference : https://byjus.com/jee/simple-pendulum/ """ + from math import pi from scipy.constants import g