Lesson 9 of 15
Simple Harmonic Motion
Simple Harmonic Motion
A mass on a spring oscillates. The restoring force is proportional to displacement:
Dividing by mass and letting :
Reducing to a System
A second-order ODE like this is converted to a first-order system by introducing velocity :
State vector: . This is the standard trick — any nth-order ODE becomes a first-order system of equations.
Symplectic Euler
For oscillatory problems, the standard Euler method adds energy over time (unstable). The symplectic (semi-implicit) variant updates velocity first, then uses the updated velocity for position:
v = v + h * (-omega**2 * x) # update v first
x = x + h * v # use new v for x
This conserves energy long-term, making it suitable for simulating oscillations.
Exact Solution
The motion is sinusoidal with period .
Your Task
Implement harmonic_motion(omega, x0, v0, t_end, n) using the symplectic Euler update. Return (x, v).
Pyodide loading...
Loading...
Click "Run" to execute your code.