Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Stepping through time

The question. The torque on a swinging weight changes at every instant. With no formula for the motion, how does a computer find where the weight will be?

It takes many small steps, and in each one it pretends that things change at a steady rate. Every model from here on is advanced this way, because in every one the forces depend on where the parts are and how fast they move. The rule used for a single step decides whether the result can be trusted.

By the end you will be able to step a model by hand with three different rules, say what each rule does to the model’s energy, and choose a step size.

The problem

Take the weight from the last chapter: 2 kg on a light rod of length m, at an angle from hanging straight down. The torque chapter supplies what we need. The weight’s torque about the pivot is , turning the rod back toward the bottom, and the rotational inertia is . So the angular acceleration is

In words: the angular acceleration is set by the angle, and it always points back toward the bottom. Here per second squared.

The acceleration depends on the angle, and the angle on all the acceleration so far, so the shortcut for a steady force no longer works.

The state of a model is the set of numbers that says everything about it at one instant. Here it is two numbers, the angle and the angular velocity . A time step is a short interval of time. A stepping rule takes the state at one moment and produces the state a time later.

Explicit Euler

The simplest rule pretends that, for one step, the velocity and the acceleration keep the values they have at its start:

In words: the angle moves on by the old angular velocity times the step, and the angular velocity by the old angular acceleration times the step. This is the explicit Euler rule (Fjordholm, chapter 2).

Two steps by hand

Release the weight from rest at 60°, which is 1.0472 rad, with a step of 20 ms. The angular acceleration at 60° is rad/s².

AngleAngular velocity
Start1.0472 rad0 rad/s
After step 1 rad rad/s
After step 2 rad rad/s

The second step uses rad/s² again, because the angle did not change in the first. The angle after two steps is 59.44°.

Why it gains energy

Look at the state after step 1. The weight has not moved, so its potential energy is what it was. Yet it now has angular velocity, and with it a kinetic energy of J. The rule has made energy out of nothing.

For a small swing this can be shown exactly. When is small and measured in radians, is close to itself (at 0.1 rad it is 0.0998), so with . The height of the weight, , is then close to , and the energy is times the quantity , whose first term comes from the kinetic energy and whose second from the potential. Put the new values from one step into that quantity:

Multiplied out, the two terms in on the left cancel and the rest are all positive. Wherever the weight is in its swing, one step multiplies the energy by . With s that factor is 1.0112: a gain of 1.1% in every step, fifty times a second. A computed example of explicit Euler adding energy to an oscillator is in Fjordholm, example 2.2.

Semi-implicit Euler

Swap the two lines, and let the second use the result of the first:

In words: update the angular velocity first, then move the angle on by the new angular velocity. This is the semi-implicit Euler rule; Fjordholm, proposition 5.5 treats it under the name symplectic Euler. It costs exactly as much as explicit Euler.

Take the same small swing, and write and for the new values. Then

The second line multiplies out the first, where the terms in cancel. The fourth uses , and the last uses .

In words: the quantity is exactly the same after the step as before it. It is the energy quantity plus one small term, and that term changes sign four times in every swing. So the energy wobbles about its true value and cannot drift away from it (Fjordholm, section 5.2 and example 5.7).

Runge–Kutta 4

Both Euler rules read the rates of change once per step. The Runge–Kutta 4 rule reads them four times. Write for the state, here the pair , and for its rates of change, the pair :

In words: read the rates at the start; use them to reach the middle of the step and read the rates there; use those to reach the middle again and read them again; use those to reach the end and read them a fourth time; then take the whole step with a weighted average of the four readings (Fjordholm, example 4.3). Each step costs four times as much and is far more accurate.

Step size and accuracy

A smaller step gives a smaller error. How much smaller depends on the rule. The table gives the size of the error in the angle 0.48 s after release from 60°, measured against a run with steps of a quarter of a millisecond.

StepExplicit EulerSemi-implicit EulerRunge–Kutta 4
20 ms4.62°2.06°0.000 084°
10 ms2.36°1.03°0.000 005 0°
5 ms1.19°0.51°0.000 000 30°

Halving the step halves the error of both Euler rules. Their error is in proportion to the step, and they are called first-order rules. Halving the step divides the error of Runge–Kutta 4 by about 16, which is . Its error goes as the fourth power of the step, and it is a fourth-order rule (Fjordholm, lemma 2.5 for explicit Euler, proposition 5.5 for the semi-implicit rule and example 4.3 for Runge–Kutta 4). In this sense semi-implicit Euler is no more accurate than explicit Euler. Its advantage is that its energy does not drift.

When the step is too large

A rule is stable when its results stay within fixed limits however long it runs. Past a certain step size a rule stops being merely inaccurate and becomes unstable (Fjordholm, chapter 3, shows this for explicit Euler on problems whose exact solution decays). For semi-implicit Euler on a small swing, the quantity that the rule keeps constant shows where the limit lies. Completing the square,

While is less than 4, both terms on the right are positive. Their sum is fixed, so neither can grow large, and the state is fenced in. Once exceeds 4 the second term is negative, and and can both grow without limit while the sum stays the same. The rule is stable only when

In words: the step must be shorter than 2 divided by the square root of . For this pendulum per second, and the limit is 0.378 s.

The number measures how sharply the acceleration responds to a change in position. A stiff part, such as a tendon that pulls back hard when stretched a little, has a large and forces a small step on the whole model.

The other rules fail in their own ways. Explicit Euler has no safe step on a swing without friction, since exceeds 1 for every . Runge–Kutta 4, at a step too large to be accurate but too small to run away, drains energy until the swing dies.

Try it

Each press of Step moves time forward 20 ms, which is one step at the starting settings.

  1. Check the hand-worked steps. With explicit Euler, 20 ms and 60°, press Step once. The angle still reads 60°, the angular velocity is −0.485 rad/s, and the energy has risen from 3.433 J to 3.462 J. Press it again: 59.44° and −0.971 rad/s.
  2. Let it run. Press Play. The energy climbs without pause, and on the third plot the state spirals outward where the reference goes round a closed loop. After 4.72 s the weight swings over the top and the run stops. It was let go at 60°, so nothing but the rule could have carried it there.
  3. Predict, then check. Choose semi-implicit Euler. Will the energy now stay exactly right? Press Play. Not exactly: it wobbles, but it stays within about 5% of the true value for the whole run, and the spiral has become a closed loop.
  4. Read the rates four times. Choose Runge–Kutta 4 and play. The energy plot is a flat line, and after 5 s the error is less than a thousandth of one per cent.
  5. Halve the step. Go back to explicit Euler at 20 ms and read the last two readouts: the error after about half a second is −4.624°, and halving the step would divide it by 1.96. Switch to Runge–Kutta 4: the divisor is 16.85. The last plot shows the same thing as a shape, a straight line for the Euler rules and a steep curve for Runge–Kutta 4.
  6. Cross the limit. Choose semi-implicit Euler and a starting angle of 10°, and play. With a step of 320 ms the motion is jagged but stays a small swing for the whole run. With 640 ms the weight is over the top on its second step.
  7. Drain the energy. Choose Runge–Kutta 4 at 320 ms and play. Nothing in the model removes energy, yet the swing has all but stopped after six seconds.

Choosing a step

  1. Choose the rule for the job: semi-implicit Euler when a model must run long without its energy drifting, Runge–Kutta 4 when each step must be accurate.
  2. Find the fastest motion in the model, and start with a step that is a small fraction of the time it takes.
  3. Halve the step and run again. If the result you care about changes by less than you can accept, the step is small enough.
  4. Keep the energy account. If it does not close, change the step or the rule.

What this model leaves out

  • Steps that change size. A rule can estimate its own error and adjust its step as it goes. The simulator has such a rule for runs that must meet a stated accuracy.
  • Rules for stiff parts. Deformable tissue is so stiff that the rules here would need impractically small steps. The part on solving introduces rules that solve an equation in every step.
  • More than one coordinate. A real arm has many angles that affect one another. The equations of a moving chain gives the accelerations that these rules then step.

Summary

  • When acceleration depends on position, the motion is found by taking small steps.
  • Explicit Euler uses old values for everything, and gains energy at every step.
  • Semi-implicit Euler updates velocity first and uses it to move the position. Its energy wobbles but does not drift.
  • Runge–Kutta 4 reads the rates of change four times in a step. Halving its step divides its error by 16, against 2 for the Euler rules.
  • A step that is too large makes a rule unstable. The stiffest part of a model sets the limit.

Next: with force, torque, energy and stepping in hand, we can describe the parts of a skeleton: bones as rigid bodies.