-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Add algorithm for Newton's Law of Gravitation #6626
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
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e5142d1
Add algorithm for Newton's Law of Gravitation
ZeroDayOwl 5dcf637
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c58d92e
Update physics/newtons_law_of_gravitation.py
ZeroDayOwl 2f4cafd
One and only one argument must be 0
cclauss e8f1a7a
Update newtons_law_of_gravitation.py
cclauss 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 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,101 @@ | ||
""" | ||
Title : Finding the value of either Gravitational Force, one of the masses or distance | ||
provided that the other three parameters are given. | ||
|
||
Description : Newton's Law of Universal Gravitation explains the presence | ||
of force of attraction between bodies having a definite mass situated at a distance. | ||
It is usually stated as that, every particle attracts every other particle | ||
in the universe with a force that is directly proportional to the product | ||
of their masses and inversely proportional to the square of the distance | ||
between their centers. The publication of the theory has become known as | ||
the "first great unification", as it marked the unification of the previously | ||
described phenomena of gravity on Earth with known astronomical | ||
behaviors. | ||
|
||
The equation for the universal gravitation is as follows: | ||
F = (G * mass_1 * mass_2) / (distance)^2 | ||
|
||
Source : | ||
- https://en.wikipedia.org/wiki/Newton%27s_law_of_universal_gravitation | ||
- Newton (1687) "Philosophiæ Naturalis Principia Mathematica" | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
# Define the Gravitational Constant G and the function | ||
GRAVITATIONAL_CONSTANT = 6.6743e-11 # unit of G : m^3 * kg^-1 * s^-2 | ||
|
||
|
||
def gravitational_law( | ||
force: float, mass_1: float, mass_2: float, distance: float | ||
) -> dict[str, float]: | ||
|
||
""" | ||
Input Parameters | ||
---------------- | ||
force : magnitude in float, unit in Newton | ||
|
||
mass_1 : magnitude in float, unit in Kilogram | ||
|
||
mass_2 : magnitude in float, unit in Kilogram | ||
|
||
distance : magnitude in float, unit in Meter | ||
|
||
Returns | ||
------- | ||
result : dict name, value pair of the parameter having Zero as it's value | ||
|
||
Returns the value of one of the parameters specified as 0, provided the values of | ||
other parameters are given. | ||
>>> gravitational_law(force=0, mass_1=5, mass_2=10, distance=20) | ||
{'force': 8.342875e-12} | ||
|
||
>>> gravitational_law(force=7367.382, mass_1=0, mass_2=74, distance=3048) | ||
{'mass_1': 1.385816317292268e+19} | ||
|
||
>>> gravitational_law(force=36337.283, mass_1=0, mass_2=0, distance=35584) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Only one argument must be 0 | ||
|
||
>>> gravitational_law(force=36337.283, mass_1=-674, mass_2=0, distance=35584) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Mass can not be negative | ||
|
||
>>> gravitational_law(force=-847938e12, mass_1=674, mass_2=0, distance=9374) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Gravitational force can not be negative | ||
""" | ||
|
||
product_of_mass = mass_1 * mass_2 | ||
|
||
if (force, mass_1, mass_2, distance).count(0) != 1: | ||
raise ValueError("Only one argument must be 0") | ||
if force < 0: | ||
raise ValueError("Gravitational force can not be negative") | ||
if distance < 0: | ||
raise ValueError("Distance can not be negative") | ||
if mass_1 < 0 or mass_2 < 0: | ||
raise ValueError("Mass can not be negative") | ||
if force == 0: | ||
force = GRAVITATIONAL_CONSTANT * product_of_mass / (distance**2) | ||
return {"force": force} | ||
elif mass_1 == 0: | ||
mass_1 = (force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_2) | ||
return {"mass_1": mass_1} | ||
elif mass_2 == 0: | ||
mass_2 = (force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_1) | ||
return {"mass_2": mass_2} | ||
elif distance == 0: | ||
distance = (GRAVITATIONAL_CONSTANT * product_of_mass / (force)) ** 0.5 | ||
return {"distance": distance} | ||
raise ValueError("One argument must be 0") | ||
|
||
|
||
# Run doctest | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OPTIONAL:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Let me know if you want this change or not and then we can merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, the suggestion looks good to me. Let me commit it.