-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added the algorithm to compute the time period of a simple pendulum #10265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tianyizheng02
merged 6 commits into
TheAlgorithms:master
from
pluto-tofu:time_period_simple_pendulum
Dec 31, 2024
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6eb3556
Added the algorithm to compute the time period of a simple pendulum
pluto-tofu ebb4ebd
imported g form scipy and changed doctests accordingly
pluto-tofu d38f52c
fixed formatting
pluto-tofu 66a8f93
applied all suggested changes from code review
pluto-tofu 69d7624
Apply suggestions from code review
tianyizheng02 fa3503c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.