Swing Time

How long does one full swing take, and how do we know the loop gets it right?

Table of contents
  1. The question
  2. Why it isn’t one substitution
  3. Reading the swing time from the loop
  4. A second way, by hand, for small swings
    1. Small angles
    2. A shape that does that
    3. Two trig functions, two jobs
  5. The swing, told by the formula
  6. Loop against formula
  7. Math, not physics

The question

Hold the rod at \(+20^\circ\) and let go at \(t = 0\). It falls, swings through the bottom, rises to \(-20^\circ\) on the other side, and comes back. How long until it is at \(+20^\circ\) again?

That return trip is one full swing. The other side, \(-20^\circ\), is its counterpart, and the rod reaches it half-way through.


Why it isn’t one substitution

The equation of motion gives the angular acceleration:

\[\ddot{\theta} = -\frac{3g}{2L}\sin\theta \approx -49\,\sin\theta \ \ \text{rad/s}^2\]

Look at what it takes as input: the angle, not the time. It is a snapshot. Tell it where the rod is right now, and it tells you how hard the rod is being pulled back.

So to know the acceleration at, say, \(t = 0.3\) s, you need the angle at \(0.3\) s. But the angle at \(0.3\) s is exactly what we are trying to find. The acceleration depends on the angle, and the angle depends on the acceleration. It is a circle.

That is also why the familiar constant-acceleration formulas, like \(s = ut + \tfrac{1}{2}at^2\), can’t carry us across the whole swing. They assume \(a\) stays put, and here it changes every time the angle does. Within one tiny step, though, the angle barely moves, so the acceleration is nearly constant and those formulas are fine. That is the opening the loop uses.

The loop in The Simulation breaks the circle one snapshot at a time:

  1. Angle now gives the acceleration now.
  2. Over one tiny step \(\Delta t\) the acceleration barely changes, so it updates the angular velocity: \(\omega \leftarrow \omega + \alpha\,\Delta t\)
  3. The new angular velocity updates the angle: \(\theta \leftarrow \theta + \omega\,\Delta t\)
  4. Take a new snapshot at the new angle, and repeat.

String the snapshots together and you have the angle as a function of time. Methods like this are called numerical integration, and they are the standard way engineers solve equations of this kind. Robot simulators step their machines forward in small time steps in exactly the same way.

The order of steps 2 and 3 matters. The loop updates \(\omega\) first, then uses the new \(\omega\) to move the angle. That order is called semi-implicit Euler, and it behaves well for swinging systems: the swing neither slowly grows nor slowly dies away. The plain version, which moves the angle with the old \(\omega\), slowly gains energy, so the rod would swing a little higher every time.


Reading the swing time from the loop

The chart in The Simulation shows the swing, but reading a time off a picture is guesswork. The loop’s own numbers do better.

At the top of each swing the rod stops for an instant and turns back. At that moment its angular velocity \(\omega\) flips from positive to negative. The loop already stores \(\omega\) at every step, so we only need to watch for that flip and note the time:

if omega_before > 0 and omega_now <= 0:
    peaks.append(t)

Running this on the 0.3 m rod, released from rest at 20° with \(\Delta t = 1\) ms for 5 s, the rod is back at the top at:

Peak Time (s)
1 0.905
2 1.809
3 2.713
4 3.617
5 4.521

The peaks are evenly spaced, about 0.904 s apart. That spacing is one full swing. The last digit wobbles by a millisecond only because the loop moves in 1 ms steps, so it can only land on a peak to within one step.

The number is only as good as the loop that made it, though. To trust it, we want the same answer a second, independent way.


A second way, by hand, for small swings

Small angles

For small angles, \(\sin\theta\) and \(\theta\) (in radians) are almost the same number. At our release angle:

\[20^\circ = 0.349 \ \text{rad}, \qquad \sin(0.349) = 0.342\]

Close enough to swap one for the other. Writing the angle explicitly as a function of time, the equation becomes:

\[\ddot{\theta}(t) = -49\,\theta(t)\]

In words: differentiate the angle twice, and you get \(-49\) times the angle back.

A shape that does that

Cosine has exactly this habit. Each time you differentiate \(\cos(kt)\), the chain rule pulls out one \(k\):

\[\frac{d}{dt}\cos(kt) = -k\sin(kt), \qquad \frac{d^2}{dt^2}\cos(kt) = -k^2\cos(kt)\]

After two derivatives, the \(k\) has come out twice, a minus sign has appeared, and the original \(\cos(kt)\) is still there. Side by side, with the same clock \(t\) (time since release) in both columns:

At time \(t\) Our equation Cosine
angle \(\theta(t)\) \(\cos(kt)\)
angular acceleration \(\ddot{\theta}(t)\) \(-k^2\cos(kt)\)
the rule \(\ddot{\theta}(t) = -49\,\theta(t)\) \(-k^2\cos(kt) = -k^2 \times \cos(kt)\)

Both rules say the same thing: differentiated twice = a negative number \(\times\) itself. They match when

\[k^2 = 49, \qquad k = 7 \ \text{rad/s}\]

The 7 is a steady turning rate, in radians per second, not an acceleration. It sets how fast the cosine runs through its cycle.

Scaling the cosine by the release angle \(\theta_0 = 20^\circ\) makes it start in the right place, and gives the angle at any time:

\[\theta(t) = \theta_0\cos(7t) = 20^\circ\cos(7t)\]

The 49 is the \(\frac{3g}{2L}\) from the equation of motion, so the 7 is built entirely from \(g\) and \(L\). A longer rod gives a smaller number and a slower swing. The mass already cancelled out when the equation was built, so it plays no part here either.

Two trig functions, two jobs

It is easy to mix these up, so it is worth saying plainly:

  • \(\sin\theta\) takes the rod’s angle as its input. It comes from the geometry of the force, and tells us how much of gravity turns the rod.
  • \(\cos(7t)\) takes time as its input, and gives back the angle.

The swing, told by the formula

Follow \(\theta(t) = 20^\circ\cos(7t)\) from release to return:

  • \(t = 0\). \(\cos(0) = 1\), so the rod is at \(20^\circ\), exactly where it was let go. It is at rest, but its angular acceleration is at its largest, because the angle is.
  • \(7t = \pi/2\), about 0.22 s. The cosine is 0: the rod hangs straight down. The angular acceleration is zero and the rod is at its top speed.
  • \(7t = \pi\), about 0.45 s. The cosine is \(-1\): the rod is at \(-20^\circ\), its counterpart, stopped for an instant. Half a swing.
  • \(7t = 2\pi\), about 0.90 s. Back at \(20^\circ\). One full swing.

So the formula’s swing time is:

\[7t = 2\pi \quad\Rightarrow\quad t = \frac{2\pi}{7} = 0.898 \ \text{s}\]

No loop, no steps. Pick any time, put it in, and the angle comes out.


Loop against formula

\(t\) (s) Loop \(\theta\) (°) \(20\cos(7t)\) (°)
0 20.00 20.00
0.1 15.33 15.30
0.2 3.54 3.40
0.3 −9.92 −10.10
0.45 −20.00 −20.00
0.6 −10.28 −9.81
0.9 19.99 20.00
  Swing time
Loop (with \(\sin\theta\)) 0.904 s
Formula (with \(\theta\)) 0.898 s

The two agree to about 0.7%. The small gap is not a bug: the formula treats 20° as a small angle, and the loop doesn’t. The loop’s rod swings very slightly slower, because the real pull, \(\sin\theta\), is a little weaker than \(\theta\).

This is how the formula earns its place. Where it is valid, it is the reference that checks the loop. Once the loop passes, we can trust it where the formula can’t go: large release angles, where \(\sin\theta \approx \theta\) breaks down, and friction, which the formula doesn’t include at all.


Math, not physics

Both answers, 0.904 s and 0.898 s, are math done on the model. They agree with each other, which tells us the loop is doing what the equation says. They do not tell us the equation describes the real rod.

Only the real rod can do that. The ESP32 timestamps every MT6835 encoder reading, the same \(\omega\) flip marks each turning point, and the spacing between them is the real swing time. That comparison is Model vs Reality.


This site uses Just the Docs, a documentation theme for Jekyll.