Skip to content

Commit 95fb181

Browse files
SajeevSenthilpre-commit-ci[bot]MaximSmolskiy
authored
Add escape velocity calculator using standard physics formula (#12721)
* Added iterative solution for power calculation * Added iterative solution for power calculation * Added iterative solution for power calculation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Added iterative solution for power calculation fixes #12709 * Added iterative solution for power calculation FIXES NUMBER 12709 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Escape velocity is the minimum speed an object must have to break free from a celestial body's gravitational pull without further propulsion. Takes input as the Mass of the Celestial body (M) and Radius fron the center of mass (M) * Fix: added header comment to escape_velocity.py * Trigger re-PR with a minor change * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix: resolve Ruff linter errors and add Wikipedia reference * Delete maths/power_using_iteration.py * Test doctests * Update escape_velocity.py * Update escape_velocity.py * Update escape_velocity.py * Update escape_velocity.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
1 parent 1317655 commit 95fb181

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

physics/escape_velocity.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import math
2+
3+
4+
def escape_velocity(mass: float, radius: float) -> float:
5+
"""
6+
Calculates the escape velocity needed to break free from a celestial body's
7+
gravitational field.
8+
9+
The formula used is:
10+
v = sqrt(2 * G * M / R)
11+
12+
where:
13+
v = escape velocity (m/s)
14+
G = gravitational constant (6.67430 * 10^-11 m^3 kg^-1 s^-2)
15+
M = mass of the celestial body (kg)
16+
R = radius from the center of mass (m)
17+
18+
Source:
19+
https://en.wikipedia.org/wiki/Escape_velocity
20+
21+
Args:
22+
mass (float): Mass of the celestial body in kilograms.
23+
radius (float): Radius from the center of mass in meters.
24+
25+
Returns:
26+
float: Escape velocity in meters per second, rounded to 3 decimal places.
27+
28+
Examples:
29+
>>> escape_velocity(mass=5.972e24, radius=6.371e6) # Earth
30+
11185.978
31+
>>> escape_velocity(mass=7.348e22, radius=1.737e6) # Moon
32+
2376.307
33+
>>> escape_velocity(mass=1.898e27, radius=6.9911e7) # Jupiter
34+
60199.545
35+
>>> escape_velocity(mass=0, radius=1.0)
36+
0.0
37+
>>> escape_velocity(mass=1.0, radius=0)
38+
Traceback (most recent call last):
39+
...
40+
ZeroDivisionError: Radius cannot be zero.
41+
"""
42+
gravitational_constant = 6.67430e-11 # m^3 kg^-1 s^-2
43+
44+
if radius == 0:
45+
raise ZeroDivisionError("Radius cannot be zero.")
46+
47+
velocity = math.sqrt(2 * gravitational_constant * mass / radius)
48+
return round(velocity, 3)
49+
50+
51+
if __name__ == "__main__":
52+
import doctest
53+
54+
doctest.testmod()
55+
print("Calculate escape velocity of a celestial body...\n")
56+
57+
try:
58+
mass = float(input("Enter mass of the celestial body (in kgs): ").strip())
59+
radius = float(input("Enter radius from the center of mass (in ms): ").strip())
60+
61+
velocity = escape_velocity(mass=mass, radius=radius)
62+
print(f"Escape velocity is {velocity} m/s")
63+
64+
except ValueError:
65+
print("Invalid input. Please enter valid numeric values.")
66+
except ZeroDivisionError as e:
67+
print(e)

0 commit comments

Comments
 (0)