Skip to content

Commit 83e5ba8

Browse files
committed
Add: work done calculation for orbital transfer between orbits
1 parent fa2597a commit 83e5ba8

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

physics/workdone.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import math
2+
3+
def orbital_transfer_work(mass_central: float, mass_object: float,
4+
r_initial: float, r_final: float) -> str:
5+
"""
6+
Calculates the work required to move an object from one orbit to another in a
7+
gravitational field based on the change in total mechanical energy.
8+
9+
The formula used is:
10+
W = (G * M * m / 2) * (1/r_initial - 1/r_final)
11+
12+
where:
13+
W = work done (Joules)
14+
G = gravitational constant (6.67430 * 10^-11 m^3 kg^-1 s^-2)
15+
M = mass of the central body (kg)
16+
m = mass of the orbiting object (kg)
17+
r_initial = initial orbit radius (m)
18+
r_final = final orbit radius (m)
19+
20+
Args:
21+
mass_central (float): Mass of the central body (kg)
22+
mass_object (float): Mass of the object being moved (kg)
23+
r_initial (float): Initial orbital radius (m)
24+
r_final (float): Final orbital radius (m)
25+
26+
Returns:
27+
str: Work done in Joules as a string in scientific notation (3 decimals)
28+
29+
Examples:
30+
>>> orbital_transfer_work(5.972e24, 1000, 6.371e6, 7e6)
31+
'2.811e+09'
32+
>>> orbital_transfer_work(5.972e24, 500, 7e6, 6.371e6)
33+
'-1.405e+09'
34+
>>> orbital_transfer_work(1.989e30, 1000, 1.5e11, 2.28e11)
35+
'1.514e+11'
36+
"""
37+
gravitational_constant = 6.67430e-11
38+
39+
if r_initial <= 0 or r_final <= 0:
40+
raise ValueError("Orbital radii must be greater than zero.")
41+
42+
work = (gravitational_constant * mass_central * mass_object / 2) * (1 / r_initial - 1 / r_final)
43+
return f"{work:.3e}"
44+
45+
46+
if __name__ == "__main__":
47+
import doctest
48+
doctest.testmod()
49+
print("Orbital transfer work calculator\n")
50+
51+
try:
52+
M = float(input("Enter mass of central body (kg): ").strip())
53+
if M <= 0:
54+
r1 = float(input("Enter initial orbit radius (m): ").strip())
55+
if r1 <= 0:
56+
raise ValueError("Initial orbit radius must be greater than zero.")
57+
58+
r2 = float(input("Enter final orbit radius (m): ").strip())
59+
if r2 <= 0:
60+
raise ValueError("Final orbit radius must be greater than zero.")
61+
m = float(input("Enter mass of orbiting object (kg): ").strip())
62+
if m <= 0:
63+
raise ValueError("Mass of the orbiting object must be greater than zero.")
64+
r1 = float(input("Enter initial orbit radius (m): ").strip())
65+
r2 = float(input("Enter final orbit radius (m): ").strip())
66+
67+
result = orbital_transfer_work(M, m, r1, r2)
68+
print(f"Work done in orbital transfer: {result} Joules")
69+
70+
except ValueError as e:
71+
print(f"Input error: {e}")

0 commit comments

Comments
 (0)