ROBOTICS 101Concept explainer

Numerical Integration: Euler versus RK4

A simulator moves a robot forward in time with small steps. How it takes each step matters. The simplest method, Euler, is easy to write but adds energy that is not really there, so a simulated pendulum swings higher and higher.

Three ways to take a step

The pendulum obeys \(\ddot\theta = -\tfrac{g}{L}\sin\theta\). Write the state as \(\mathbf{x} = (\theta, \dot\theta)\) and its rate of change as \(\dot{\mathbf{x}} = f(\mathbf{x})\).

$$\text{Euler:}\quad \mathbf{x}_{k+1} = \mathbf{x}_k + h\,f(\mathbf{x}_k)$$

Semi-implicit Euler updates the velocity first, then uses the new velocity to update the angle. RK4 samples the slope four times inside the step and averages them:

$$\mathbf{x}_{k+1} = \mathbf{x}_k + \tfrac{h}{6}\left(k_1 + 2k_2 + 2k_3 + k_4\right)$$

What to notice

The energy of a real pendulum with no friction never changes. Euler's energy climbs every swing, so its pendulum ends up spinning over the top. Semi-implicit Euler wobbles but stays bounded, which is why many physics engines use it. RK4 stays very close to the true energy, even with large steps. Raise the time step and see each method break down.