Home > Python > Ordinary Differential Equation-1

Ch. 6.2 Motion of a Harmonic Oscillator

Chapters Index
Program 1

Motion of a Harmonic Oscillator

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

This Python program simulates the motion of a one-dimensional harmonic oscillator using numerical integration (Euler's method). The harmonic oscillator follows Hooke's Law, where the force acting on the object is proportional to its displacement and is given by:

\[ F = -kx \]

Where:

  • \( F \) is the force acting on the object
  • \( k \) is the spring constant
  • \( x \) is the displacement of the object from the equilibrium position
Theoretical Principles & Computational Physics Guide

Simple Harmonic Oscillator: Conservative Dynamics & Phase Space Orbits

Dr. Alorika Chatterjee & Dr. Anirban Shaw Science & Physics Learners Python 3 / NumPy / SciPy

1. Core Principles & Theoretical Framework

Restoring Force: Models systems oscillating around stable potential minima under Hooke's law ($F = -kx$).
Phase Space Invariance: Conservative trajectories trace closed elliptical orbits in phase space $(x, v)$, conserving $E = \frac{1}{2}mv^2 + \frac{1}{2}kx^2$.
Symplectic Euler-Cromer: Updating velocity first and using updated velocity for position bounds energy fluctuations indefinitely.

2. Mathematical Formulation & Algorithmic Pipeline

Coupled first-order state-space system and mechanical energy invariant:

$$ \frac{dx}{dt} = v, \quad \frac{dv}{dt} = -\omega^2 x, \quad E = \frac{1}{2}m v^2 + \frac{1}{2}k x^2 = \text{constant} $$

3. Numerical Mechanics & Implementation Logic

  • Coupled Decomposition: Decompose the 2nd-order ODE into two coupled 1st-order equations.
  • Euler-Cromer Stepping: Update velocity $v_{n+1} = v_n - \omega^2 x_n h$; then update position $x_{n+1} = x_n + v_{n+1} h$.
  • Energy Monitoring: Calculate and log $E_n = \frac{1}{2}m v_n^2 + \frac{1}{2}k x_n^2$ at each step to verify stability.

4. Relevant Computational Practice Exercises

Exercise 1
Simulate 50 complete periods of an oscillator with both Euler and Euler-Cromer methods, comparing energy preservation.
Exercise 2
Add a linear damping term $F_d = -b v$ and simulate underdamped, critically damped, and overdamped regimes.
Exercise 3
Plot the phase space portrait $(x, v)$ showing how closed conservative orbits transform into inward spiral attractors under damping.
Standard Literature & References:
  • David Morin, "Introduction to Classical Mechanics", Cambridge University Press (2008).
  • Mark Newman, "Computational Physics", University of Michigan (2013).