Physics · Introductory

Simple harmonic oscillator

Written by SignalsLab

Early edition: these lessons are being reviewed.

Content

Summary

A simple harmonic oscillator is a system where the restoring force is proportional to displacement, giving sinusoidal motion at a fixed frequency. A mass on a spring is the standard example, and the same mathematics describes pendulums at small angles, molecular vibrations and oscillating electrical circuits.

Intuition

Imagine pulling a mass on a spring to the right and letting go. The spring pulls it back toward the centre, and the farther you stretched it, the harder it pulls. The mass speeds up as it approaches the centre, overshoots because of its momentum, and is then slowed by the spring on the other side. Energy swaps back and forth between stored spring energy and kinetic energy. Because the force grows in proportion to displacement, a stiffer spring gives faster oscillation and a heavier mass gives slower oscillation. Remarkably, the period does not depend on how far you pulled, only on the stiffness and the mass.

Key equations

F=−kx
(1)

Hooke's law: the restoring force is proportional to displacement and points opposite to it.

md2xdt2=−kx
(2)

Newton's second law applied to the mass on a spring.

x(t)=Acos⁡(ωt+ϕ)
(3)

General solution: sinusoidal motion with amplitude A, angular frequency ω and phase ϕ.

ω=k/m,T=2πm/k
(4)

Angular frequency and period depend only on stiffness k and mass m.

E=12kA2
(5)

Total mechanical energy is constant and set by the amplitude.

Derivation

  1. Start with Newton's second law: m a = F, and use Hooke's law F = -kx.
  2. Write acceleration as the second derivative: m x'' = -kx, so x'' = -(k/m) x.
  3. Define omega squared = k/m, giving x'' = -omega^2 x.
  4. Try x=Acos⁡(ωt+ϕ). Then x′′=−ω2Acos⁡(ωt+ϕ)=−ω2x, so it satisfies the equation.
  5. A and phi are fixed by initial conditions, namely the starting position and velocity.
  6. The period is the time for the phase to advance by 2π: T=2πω=2πmk.
  7. Velocity is v=−Aωsin⁡(ωt+ϕ). Kinetic plus potential energy gives 12mv2+12kx2=12kA2, which is constant.

Worked example

Problem

A 0.50 kg mass on a spring with k = 200 N/m is pulled 0.10 m from equilibrium and released from rest. Find the period, the maximum speed and the total energy.

  1. Angular frequency: ω=200/0.50=400=20 rad/s.
  2. Period: T=2π20=0.314 s.
  3. Maximum speed occurs at the centre: vmax=Aω=0.10×20=2.0 m/s.
  4. Total energy: E = (1/2)(200)(0.10)^2 = 1.0 J.
  5. Check: (1/2)(0.50)(2.0)^2 = 1.0 J, which agrees.

Answer T ≈ 0.31 s, maximum speed 2.0 m/s, total energy 1.0 J.

Common mistakes

  • Thinking the period depends on amplitude; for a true harmonic oscillator it does not.
  • Confusing angular frequency ω in radians per second with frequency f in hertz; ω=2πf.
  • Forgetting the minus sign in the restoring force, which would give exponential growth instead of oscillation.
  • Believing the velocity is greatest at maximum displacement; it is greatest at equilibrium, where it is zero at the turning points.
  • Applying the pendulum formula at large angles, where the small-angle approximation fails.

Animation

Animation coming soon

The animated version of this lesson is being prepared for the web. In the Signals AI app, lessons are drawn step by step.

Code

scene.py(85 lines)
import sys
from manim import *
from blocks import *
import numpy as np

# Physical parameters (SI-like units, chosen for display)
M = 1.0
K = 4.0
OMEGA = float(np.sqrt(K / M))
A = 1.0
T_MAX = 2 * np.pi / OMEGA * 2  # two periods


def x_of_t(t):
    """Displacement x(t) = A cos(omega t), solution of m x'' = -k x."""
    return A * np.cos(OMEGA * t)


def v_of_t(t):
    """Velocity v(t) = -A omega sin(omega t)."""
    return -A * OMEGA * np.sin(OMEGA * t)


def energy(t):
    """Total energy 1/2 m v^2 + 1/2 k x^2."""
    return 0.5 * M * v_of_t(t) ** 2 + 0.5 * K * x_of_t(t) ** 2


class Main(Scene):
    def construct(self):
        self.camera.background_color = "#14161c"
        TitleCard(self, "Simple harmonic oscillator", "m x'' = -k x")

        # Plot of displacement over time
        plot = CurvePlot(x_of_t, x_range=(0, float(T_MAX), 0.5), y_range=(-1.5, 1.5, 0.5),
                         x_label="t", y_label="x", color=C_MAIN,
                         x_length=8.0, y_length=3.6)
        plot.shift(UP * 0.2 + LEFT * 1.5)
        self.play(Create(plot.axes), FadeIn(plot.labels))

        # Spring-mass picture on the right
        wall = Line([5.2, -0.8, 0], [5.2, 1.6, 0], color=C_DIM, stroke_width=6)
        track = Line([3.4, -0.8, 0], [5.2, -0.8, 0], color=C_DIM)
        tracker = ValueTracker(0)

        def mass_x(t):
            return 4.3 + 0.0 * t

        block = Square(0.5, color=C_ACCENT, fill_color=C_ACCENT, fill_opacity=0.8)
        block.add_updater(lambda m: m.move_to([4.3 + 0.6 * x_of_t(tracker.get_value()), -0.5, 0]))
        spring = always_redraw(lambda: Line([5.2, -0.5, 0],
                                            block.get_right(), color=C_GOOD))
        self.play(FadeIn(wall), FadeIn(track), FadeIn(block), FadeIn(spring))

        eq = MathTex(r"x(t)=A\cos(\omega t),\ \omega=\sqrt{k/m}", font_size=30)
        eq.to_edge(DOWN, buff=1.6)
        self.play(Write(eq))

        dot = Dot(plot.axes.c2p(0, x_of_t(0)), color=C_ACCENT)
        self.add(dot)
        dot.add_updater(lambda d: d.move_to(
            plot.axes.c2p(tracker.get_value(), x_of_t(tracker.get_value()))))
        self.play(Create(plot.curve),
                  tracker.animate.set_value(float(T_MAX)),
                  run_time=8, rate_func=linear)
        block.clear_updaters()
        dot.clear_updaters()
        self.wait(0.5)
        StepCaption(self, "Restoring force -kx gives steady oscillation with period 2π/ω.")
        self.wait(1.5)


if __name__ == "__main__" and "--selftest" in sys.argv:
    assert abs(x_of_t(0) - A) < 1e-12
    assert abs(v_of_t(0)) < 1e-12
    ts = np.linspace(0, T_MAX, 200)
    assert np.allclose(energy(ts), 0.5 * K * A ** 2)
    period = 2 * np.pi / OMEGA
    assert abs(x_of_t(period) - A) < 1e-9
    # numerical check that x'' = -(k/m) x
    h = 1e-4
    t0 = 0.7
    acc = (x_of_t(t0 + h) - 2 * x_of_t(t0) + x_of_t(t0 - h)) / h ** 2
    assert abs(acc + (K / M) * x_of_t(t0)) < 1e-4
    print("ok")

The Manim scene code that draws this lesson's animation in the Signals AI app (Python, Manim Community).

ccBYNC© 2026 SignalsLab. Lesson text shared as is under CC BY-NC 4.0.