Skip to content

Physics orbital_transfer_work #12728

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 22 commits into from
May 13, 2025
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f1a39f3
Added iterative solution for power calculation
SajeevSenthil May 6, 2025
89ad00e
Added iterative solution for power calculation
SajeevSenthil May 6, 2025
ee3f4be
Added iterative solution for power calculation
SajeevSenthil May 6, 2025
a55058b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 6, 2025
1b86189
Added iterative solution for power calculation fixes #12709
SajeevSenthil May 6, 2025
53ca0d3
Merge branch 'optimized-power-iteration' of https://github.com/Sajeev…
SajeevSenthil May 6, 2025
bde1393
Added iterative solution for power calculation FIXES NUMBER 12709
SajeevSenthil May 6, 2025
935febe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 6, 2025
a908d03
Escape velocity is the minimum speed an object must have to break fre…
SajeevSenthil May 10, 2025
3c57056
Merge branch 'optimized-power-iteration' of https://github.com/Sajeev…
SajeevSenthil May 10, 2025
f33964a
Fix: added header comment to escape_velocity.py
SajeevSenthil May 10, 2025
58273e7
Merge branch 'TheAlgorithms:master' into physics
SajeevSenthil May 10, 2025
c6cfddc
Trigger re-PR with a minor change
SajeevSenthil May 10, 2025
5854d54
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 10, 2025
fa2597a
Fix: resolve Ruff linter errors and add Wikipedia reference
SajeevSenthil May 10, 2025
83e5ba8
Add: work done calculation for orbital transfer between orbits
SajeevSenthil May 12, 2025
26093f7
Merge branch 'master' into physics-orbit-workdone
SajeevSenthil May 12, 2025
3ec7eef
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 12, 2025
87f4e8c
Merge branch 'TheAlgorithms:master' into physics-orbit-workdone
SajeevSenthil May 12, 2025
30fcdd4
Update escape_velocity.py
cclauss May 13, 2025
7614e30
Delete maths/power_using_iteration.py
cclauss May 13, 2025
2611425
Update and rename workdone.py to orbital_transfer_work.py
cclauss May 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions physics/orbital_transfer_work.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
def orbital_transfer_work(
mass_central: float, mass_object: float, r_initial: float, r_final: float
) -> str:
"""
Calculates the work required to move an object from one orbit to another in a
gravitational field based on the change in total mechanical energy.

The formula used is:
W = (G * M * m / 2) * (1/r_initial - 1/r_final)

where:
W = work done (Joules)
G = gravitational constant (6.67430 * 10^-11 m^3 kg^-1 s^-2)
M = mass of the central body (kg)
m = mass of the orbiting object (kg)
r_initial = initial orbit radius (m)
r_final = final orbit radius (m)

Args:
mass_central (float): Mass of the central body (kg)
mass_object (float): Mass of the object being moved (kg)
r_initial (float): Initial orbital radius (m)
r_final (float): Final orbital radius (m)

Returns:
str: Work done in Joules as a string in scientific notation (3 decimals)

Examples:
>>> orbital_transfer_work(5.972e24, 1000, 6.371e6, 7e6)
'2.811e+09'
>>> orbital_transfer_work(5.972e24, 500, 7e6, 6.371e6)
'-1.405e+09'
>>> orbital_transfer_work(1.989e30, 1000, 1.5e11, 2.28e11)
'1.514e+11'
"""
gravitational_constant = 6.67430e-11

if r_initial <= 0 or r_final <= 0:
raise ValueError("Orbital radii must be greater than zero.")

work = (gravitational_constant * mass_central * mass_object / 2) * (
1 / r_initial - 1 / r_final
)
return f"{work:.3e}"


if __name__ == "__main__":
import doctest

doctest.testmod()
print("Orbital transfer work calculator\n")

try:
M = float(input("Enter mass of central body (kg): ").strip())
if M <= 0:
r1 = float(input("Enter initial orbit radius (m): ").strip())
if r1 <= 0:
raise ValueError("Initial orbit radius must be greater than zero.")

r2 = float(input("Enter final orbit radius (m): ").strip())
if r2 <= 0:
raise ValueError("Final orbit radius must be greater than zero.")
m = float(input("Enter mass of orbiting object (kg): ").strip())
if m <= 0:
raise ValueError("Mass of the orbiting object must be greater than zero.")
r1 = float(input("Enter initial orbit radius (m): ").strip())
r2 = float(input("Enter final orbit radius (m): ").strip())

result = orbital_transfer_work(M, m, r1, r2)
print(f"Work done in orbital transfer: {result} Joules")

except ValueError as e:
print(f"Input error: {e}")