Intermediate · Pendulum wave

Lesson 5: Formulas on arrays

Build the pendulum wave in Python, and learn on the way:

  • a physics formula applied to a whole array
  • np.round and print for a quick check
  • several plots side by side with plt.subplots
  • f-strings
  • phase: where in its swing a pendulum is

Watch it first

Plays from YouTube after a click.

pendulum-wave.py
"""Pendulum wave: 24 pendulums making 51, 52, ... 74 swings in 60 seconds.
Run: python pendulum-wave.py   (needs: pip install numpy matplotlib)
"""
import numpy as np
import matplotlib.pyplot as plt

plt.style.use("dark_background")

g, cycle = 9.81, 60.0  # gravity in m/s^2, seconds until they are back in step
n = np.arange(51, 75)  # swings per cycle, one more for each pendulum
period = cycle / n
length = g * (period / (2 * np.pi)) ** 2  # small swings: T = 2 pi sqrt(L / g)
print("lengths in cm:", np.round(length * 100, 1))

fig, axes = plt.subplots(1, 4, figsize=(12, 4), sharey=True)
for ax, t in zip(axes, [0, 15, 20, 30]):
    swing = np.cos(2 * np.pi * t / period)  # where each bob is, from -1 to 1
    ax.scatter(swing, np.arange(len(n)), c=swing, cmap="cool", s=40)
    ax.set_title(f"t = {t} s")
    ax.set_xlim(-1.2, 1.2)
    ax.axis("off")
plt.show()

# Try: t = 60 s (or 75, 80, 90): the same pictures again, as every count is whole

No Python on this computer? The notebook runs the same script, the exercises and the answers in your browser, on Google Colab.

Line by line

  1. n = np.arange(51, 75)

    The 24 counts of swings per minute: 51, 52, … 74.

  2. period = cycle / n

    Each pendulum’s period: 60 seconds divided by its count.

  3. length = g * (period / (2 * np.pi)) ** 2

    The small-swing pendulum formula T = 2π√(L / g), solved for the length L.

  4. fig, axes = plt.subplots(1, 4, figsize=(12, 4), sharey=True)

    One figure with four plots side by side; the loop fills one plot per moment.

  5. swing = np.cos(2 * np.pi * t / period)

    Where each bob is at the time t, from −1 to 1: after t / period swings, the cosine of that many full turns.

  6. ax.set_title(f"t = {t} s")

    An f-string puts the value of t into the title.

Try it yourself

  1. How long are the longest and the shortest pendulum?

    Hint

    The script prints the lengths.

    Answer

    34.4 cm for 51 swings a minute, 16.3 cm for 74.

  2. Add the moment t = 12 s. How many groups does the row split into?

    Hint

    You need a fifth plot: plt.subplots(1, 5) and [0, 12, 15, 20, 30].

    Answer

    Five: at 12 s, a fifth of the minute, neighbours are a fifth of a swing apart.

  3. Make it 30 pendulums.

    Hint

    Change the end of np.arange.

    Answer

    n = np.arange(51, 81) gives 30 counts, from 51 to 80.

Quiz

1. Why are all the pendulums in step again at 60 s?
2. What does sharey=True do?
3. T = 2π√(L / g), solved for L, is…
4. With t = 15, f"t = {t} s" gives…
5. At 30 s neighbours are half a swing apart. What do you see?

Flashcards

Read the front, say the back to yourself, then open the card.

  • plt.subplots(1, 4)

    A figure and four plots (axes) in one row, to draw on one by one.

  • f"t = {t} s"

    An f-string: the value of t goes in place of {t}.

  • Pendulum period

    For small swings, T = 2π√(L / g): four times longer means twice as slow.

  • Phase

    Where a pendulum is within its swing; here, the cosine of 2π·t / T.

  • np.round(x, 1)

    Rounds every number of an array to one decimal place.

These flashcards on their own page →