Skip to content

Commit 301a520

Browse files
SparshRastogipre-commit-ci[bot]ChrisO345CaedenPH
authored
Create potential_energy.py (TheAlgorithms#7666)
* Create potential_energy.py Finding the gravitational potential energy of an object with reference to the earth, by taking its mass and height above the ground as input * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update physics/potential_energy.py Co-authored-by: Chris O <[email protected]> * Update physics/potential_energy.py Co-authored-by: Chris O <[email protected]> * Update physics/potential_energy.py Co-authored-by: Caeden Perelli-Harris <[email protected]> * Update physics/potential_energy.py Co-authored-by: Caeden Perelli-Harris <[email protected]> * Update physics/potential_energy.py Co-authored-by: Caeden Perelli-Harris <[email protected]> * Update physics/potential_energy.py Co-authored-by: Caeden Perelli-Harris <[email protected]> * Update physics/potential_energy.py Co-authored-by: Caeden Perelli-Harris <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Chris O <[email protected]> Co-authored-by: Caeden Perelli-Harris <[email protected]>
1 parent cf08d9f commit 301a520

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

physics/potential_energy.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from scipy.constants import g
2+
3+
"""
4+
Finding the gravitational potential energy of an object with reference
5+
to the earth,by taking its mass and height above the ground as input
6+
7+
8+
Description : Gravitational energy or gravitational potential energy
9+
is the potential energy a massive object has in relation to another
10+
massive object due to gravity. It is the potential energy associated
11+
with the gravitational field, which is released (converted into
12+
kinetic energy) when the objects fall towards each other.
13+
Gravitational potential energy increases when two objects
14+
are brought further apart.
15+
16+
For two pairwise interacting point particles, the gravitational
17+
potential energy U is given by
18+
U=-GMm/R
19+
where M and m are the masses of the two particles, R is the distance
20+
between them, and G is the gravitational constant.
21+
Close to the Earth's surface, the gravitational field is approximately
22+
constant, and the gravitational potential energy of an object reduces to
23+
U=mgh
24+
where m is the object's mass, g=GM/R² is the gravity of Earth, and h is
25+
the height of the object's center of mass above a chosen reference level.
26+
27+
Reference : "https://en.m.wikipedia.org/wiki/Gravitational_energy"
28+
"""
29+
30+
31+
def potential_energy(mass: float, height: float) -> float:
32+
# function will accept mass and height as parameters and return potential energy
33+
"""
34+
>>> potential_energy(10,10)
35+
980.665
36+
>>> potential_energy(0,5)
37+
0.0
38+
>>> potential_energy(8,0)
39+
0.0
40+
>>> potential_energy(10,5)
41+
490.3325
42+
>>> potential_energy(0,0)
43+
0.0
44+
>>> potential_energy(2,8)
45+
156.9064
46+
>>> potential_energy(20,100)
47+
19613.3
48+
"""
49+
if mass < 0:
50+
# handling of negative values of mass
51+
raise ValueError("The mass of a body cannot be negative")
52+
if height < 0:
53+
# handling of negative values of height
54+
raise ValueError("The height above the ground cannot be negative")
55+
return mass * g * height
56+
57+
58+
if __name__ == "__main__":
59+
from doctest import testmod
60+
61+
testmod(name="potential_energy")

0 commit comments

Comments
 (0)