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