This project began as a UCL Summer Project to compare numerical methods for solving the pendulum ODE. It has been extended into a reflection on the "correct" choice of algorithm, balancing short-term accuracy, long-term stability, and computational complexity.
This project's narrative argues that for physical simulations, "structure-preserving" algorithms (like Symplectic Euler) are often superior to "high-accuracy" algorithms (like RK4), especially over long time horizons.
This repository is organized around a central narrative. The scripts in the analysis/ folder are numbered to follow this "story."
- Script:
analysis/plot_00_linear_approx.py - Goal: Justify why numerical methods are needed.
- Analysis: Compares the analytical "small angle" solution (sin(θ) ≈ θ) against the high-accuracy RK4 solver.
- Finding: The approximation holds for small angles (e.g., 18°) but fails significantly at larger angles (e.g., 30°), proving we must solve the full non-linear ODE.
- Script:
analysis/plot_01_euler_comparison.py - Goal: Compare the three base Euler methods.
- Analysis: Uses Energy Plots and Phase Portraits to compare Explicit, Implicit, and Symplectic Euler.
- Finding: Explicit Euler is unstable (gains energy). Implicit Euler is "damped" (loses energy). Symplectic Euler is the only one that conserves energy (oscillates around the true value), making it the "winner" for physical simulation.
- Evidence:
plot_02_implicit_stability.py(proves Implicit loses energy long-term) andplot_03_symplectic_stability.py(proves Symplectic conserves energy long-term).
- Script:
analysis/plot_04_rk4_reflection.py - Goal: A deeper reflection comparing the "best" Euler (Symplectic) against the "gold standard" (RK4).
- Analysis: Runs a long-term simulation (
T=200) with a large step-size (h=0.05). - Finding: This reveals the core reflection of the project:
- The Trajectory Plot becomes a useless, overlapping mess.
- The Phase Portrait is the only tool that shows the truth:
RK4(non-symplectic) slowly drifts and spirals away (energy drift).Symplectic Euler(structure-preserving) remains perfectly stable and bounded. - Conclusion:
RK4wins on short-term accuracy, butSymplectic Eulerwins on long-term stability.
- Script:
scratchpad/newton_method_exploration.py - Purpose: A "learning note" exploring how Newton's Method works from scratch. This was done to understand the
scipy.fsolvesolver used by the Implicit Euler method.
main.py: A simple "dashboard" script that runs all 3 Euler methods and plots the 4-panel comparison.src/solvers.py: The Single Source of Truth. Contains all core solver functions (Euler, RK4, small_angle).analysis/: Contains all the specialized, "publication-ready" plots for the narrative.scratchpad/: Contains early drafts and learning explorations (likenewton.py).requirements.txt: A list of all Python dependencies..gitignore: Configured to ignore.venv/and__pycache__/.
- Clone the repository and
cdinto it. - Create and activate a virtual environment:
python3 -m venv .venv source .venv/bin/activate - Install dependencies:
pip install -r requirements.txt
- Run the main dashboard:
python main.py
- Run a specific analysis from the narrative:
python analysis/plot_01_euler_comparison.py
This project's structure is now clean, but the analysis is not yet complete.
- Refactor Imports: The scripts in
analysis/still need to be fully refactored toimporttheir solvers fromsrc/solvers.py. - Parameter Tuning: The
analysis/plots (especiallyplot_04_rk4_reflection.py) need theirhandTparameters carefully "tuned" to ensure their trends are "not blurry" and persuasively demonstrate the scientific argument. - Big O Analysis: Add a formal Big O complexity analysis to this README, comparing the computational cost of each method (e.g.,
Implicitvs.Symplectic).