Video Demo: (https://youtu.be/zUA1SjwEXEw)
"The Lagrange Point Calculator" is a Python-based command-line tool designed to calculate and visualize the five Lagrange points in a two-body system (e.g., Earth-Sun or Earth-Moon).
This project is inspired by my undergraduate thesis (Final Degree Project) on chaos in celestial mechanics. Lagrange points are equilibrium solutions to the restricted three-body problem, where the gravitational forces of two large bodies and the centrifugal force balance each other out. These points are critical for space exploration, serving as stable parking spots for telescopes like the James Webb Space Telescope (located at L2).
The tool allows users to input the masses of two primary bodies and the distance between them. It then numerically solves for the positions of L1, L2, and L3 using the Newton-Raphson method and geometrically determines L4 and L5. Finally, it generates a visualization of the system using matplotlib, saving the result as an image file.
-
project.py: The core application containing the main logic. It is organized into three primary functions:get_user_input(): Handles user interaction via the console, ensuring inputs (masses and distance) are valid positive floats. It parses string inputs and handles exceptions.calculate_lagrange_points(m1, m2, r): The physics engine. It calculates the reduced mass parameter (mu) and solves the Euler's quintic equations usingscipy.optimize.newtonto find the collinear points (L1, L2, L3). It also computes the coordinates for the triangular points (L4, L5).plot_system(...): Handles the visualization. It usesmatplotlibto plot the primary bodies and the calculated Lagrange points. It saves the output aslagrange_plot.png.
-
test_project.py: Contains unit tests usingpytestto verify the accuracy and robustness of the program.- It tests physical constants (e.g., ensuring the L1 point for the Earth-Sun system is calculated at approximately 1.5 million km from Earth).
- It verifies geometric properties (e.g., L4 forming an equilateral triangle in an equal-mass system).
- It tests input validation using mocking to simulate user input.
-
requirements.txt: Lists the external libraries required to run the project (numpy,scipy,matplotlib).
1. Numerical Solver (SciPy):
Finding the location of L1, L2, and L3 requires finding the roots of a fifth-degree polynomial (Euler's quintic equation), which has no general closed-form algebraic solution. While I could have implemented a manual iterative method, I chose to use scipy.optimize.newton. This function provides a robust and faster convergence, which is essential when dealing with astronomical masses where floating-point precision issues can arise due to the vast difference in scales (e.g., 10^30 kg vs 10^24 kg).
2. Visualization Strategy:
Space is vast, and drawing celestial bodies to scale often results in invisible dots. In plot_system, I made the design choice to use fixed-size markers (scatter with specific s values) rather than drawing the bodies to physical scale. This ensures that the user can clearly see the relative positions of the sun, the planet, and the Lagrange points, prioritizing readability over strict visual scale. I also added a coordinate adjustment to align the visualization (where Mass 1 is at 0,0) with the mathematical model (which calculates based on the Center of Mass).
3. Input Handling: I decided to allow users to input all three parameters in a single line (space-separated) rather than three separate prompts. This makes the user experience faster and smoother for repeated calculations, similar to standard scientific command-line tools.
-
Install the required dependencies:
pip install -r requirements.txt
-
Run the program:
python project.py
-
Run the tests:
pytest test_project.py