Web Analytics
Simple pendulum in p5.js – measuring the period

Simple pendulum in p5.js – when the formula stops working

The fifth lesson of the guide Program a physics simulation. The formula for the period of a pendulum T = 2π · √(l / g) only works for small angles. What happens at large angles? There is no simple formula for that – but the simulation can easily measure the period. Change the initial angle with the ↑ and ↓ arrow keys (click the canvas first).

Physics: simple pendulum – restoring force m · g · sin θ, period, small and large angles.

Programming: angle and angular velocity as the state, Euler–Cromer, measuring the time between two events (crossings of the equilibrium), interpolation.

You should know: lesson 4 (mass and spring).

Step 1: the state of a pendulum is an angle

The bob of a pendulum moves along a circle, so its position is described by one number – the angle θ from the vertical. Instead of velocity we track the angular velocity ω (how fast the angle changes per second). The restoring force m · g · sin θ gives the angular acceleration:

α = −(g / l) · sin θ

In the program angles are in radians (radians()), because sin() and cos() need them.

Step 2: the simulation step

The same Euler–Cromer as for the spring, just with an angle instead of a position. Notice that we use sin(theta), not the approximation sin θ ≈ θ – that is why the program also works for large angles.

const alpha = -g / L * sin(theta);
const oldAngle = theta;
omega = omega + alpha * dt;
theta = theta + omega * dt;
t = t + dt;

We draw the bob using sine and cosine: x = x₀ + l · sin θ, y = y₀ + l · cos θ (y on the canvas grows downwards, so the bob is below the pivot).

Step 3: how to measure the period

The period is the time between two identical events. We choose the moment when the pendulum passes through the equilibrium position to the right: in the previous step the angle was negative, and now it is zero or positive. Since this happened somewhere within the step dt, we estimate the exact moment by linear interpolation – so the measurement is more accurate than 1/60 s.

if (oldAngle < 0 && theta >= 0) {
  const tCross = t - dt * theta / (theta - oldAngle);
  if (lastCrossing !== null) {
    period = tCross - lastCrossing;
  }
  lastCrossing = tCross;
}

Step 4: the result

For l = 1 m the formula gives T = 2.006 s. The simulation at different initial angles:

initial anglemeasured periodlonger than formula
10°2.010 s0.2%
20°2.021 s0.7%
40°2.069 s3%
70°2.211 s10%
100°2.472 s23%
170°4.894 s144%

For small angles the formula is excellent, for large ones it is completely wrong. At 170° the bob “hangs” almost at the top for a long time before it starts back. This is an example where simulation is the only simple way to get the answer.

Whole program

Copy it into the p5.js editor and delete the line canvas.parent('lesson-canvas');.

// Simple pendulum – program from the lesson “Program a physics simulation” (simulacije.svetprogramiranja.com)
const g = 9.81;          // acceleration due to gravity, m/s²
const L = 1;             // length of the string, m
const SCALE = 130;       // how many pixels make one metre
const dt = 1 / 60;       // time step, s

let angle0 = 20;         // initial angle, in degrees
let theta, omega, t;     // angle (rad), angular velocity (rad/s), time (s)
let lastCrossing, period;

function setup() {
  const canvas = createCanvas(320, 330);
  canvas.parent('lesson-canvas');
  start();
}

function start() {
  theta = radians(angle0);
  omega = 0;
  t = 0;
  lastCrossing = null;
  period = null;
}

function draw() {
  background(248, 250, 252);

  // Euler–Cromer: angular acceleration → new angular velocity → new angle
  const alpha = -g / L * sin(theta);
  const oldAngle = theta;
  omega = omega + alpha * dt;
  theta = theta + omega * dt;
  t = t + dt;

  // crossing the equilibrium position to the right: one period has passed since the previous such crossing
  if (oldAngle < 0 && theta >= 0) {
    const tCross = t - dt * theta / (theta - oldAngle);
    if (lastCrossing !== null) {
      period = tCross - lastCrossing;
    }
    lastCrossing = tCross;
  }

  // drawing: the pivot is in the middle of the canvas
  const x0 = width / 2;
  const y0 = 170;
  const x = x0 + L * SCALE * sin(theta);
  const y = y0 + L * SCALE * cos(theta);
  stroke(51, 65, 85);
  strokeWeight(2);
  line(x0, y0, x, y);
  noStroke();
  fill(51, 65, 85);
  circle(x0, y0, 8);
  fill(249, 115, 22);
  circle(x, y, 26);

  // numbers
  const tFormula = 2 * PI * sqrt(L / g);
  fill(11, 19, 36);
  textSize(14);
  text('initial angle = ' + angle0 + '° (arrows ↑ ↓)', 10, 22);
  text('T from formula = ' + tFormula.toFixed(3) + ' s', 10, 42);
  text('T measured = ' + (period === null ? '…' : period.toFixed(3) + ' s'), 10, 62);
}

function keyPressed() {
  if (keyCode === UP_ARROW && angle0 < 170) {
    angle0 = angle0 + 10;
    start();
    return false;
  }
  if (keyCode === DOWN_ARROW && angle0 > 10) {
    angle0 = angle0 - 10;
    start();
    return false;
  }
}

function mousePressed() {
  if (mouseX >= 0 && mouseX <= width && mouseY >= 0 && mouseY <= height) {
    start();
  }
}

Tasks for you

  1. Change the length of the string to 0.25 m (and increase SCALE to 400, so the pendulum is easy to see). What do you expect for the period, and what does the simulation show?
    Solution

    The period is half as long (T is proportional to √l): 1.003 s by the formula, and the simulation at 20° gives 1.010 s – again about 0.7% longer, as for the long pendulum.

  2. Take the pendulum to the Moon (g = 1.62 m/s²).
    Solution

    By the formula T = 2π · √(1 / 1.62) ≈ 4.937 s, and the simulation at 20° gives 4.974 s (again 0.7% longer because of the 20° angle).

  3. Replace Euler–Cromer with plain Euler (new angle from the old angular velocity). What happens?
    Solution

    The amplitude grows: a pendulum released from 20° swings up to 41.8° after 10 s, and almost to the top after 30 s (173.7°). The same problem as with the spring.

  4. Add damping (air resistance): const alpha = -g / L * sin(theta) - 0.1 * omega;. Does the period change as the amplitude decreases?
    Solution

    The amplitude slowly decreases, and the measured period gets closer and closer to the value from the formula, because the angle becomes small: at an initial angle of 100° the first measured periods are 2.314 s, 2.237 s, 2.183 s…, and after a minute 2.006 s.

Next