Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 47 ยท Applications and Simulation

Numerical integration: Euler, semi-implicit Euler and RK4

Compare explicit Euler, semi-implicit Euler and fourth-order Runge-Kutta on an oscillator, measure their order of accuracy and see why energy drifts.

The previous lesson said a simulation advances the state by small steps. How you take each step matters a great deal. The same oscillator, simulated with the same step size, can decay, stay steady or explode, depending only on the integrator.

This lesson compares the three methods you will meet most often, shows how to measure their accuracy, and gives you a rule for choosing among them.

Plot of the exact cosine, explicit Euler, semi-implicit Euler and RK4 for an oscillator with ฮ”t = 0.2
With the same step, Euler gains energy, semi-implicit Euler stays bounded and RK4 follows the exact answer.

What you will learn

  • Write explicit Euler, semi-implicit Euler and RK4 updates for แบ = f(x, v).
  • Explain order of accuracy and measure it numerically.
  • Recognize energy drift and choose a method that fits the problem.
  • Select a step size.

Key terms

Order p: a method of order p has global error proportional to ฮ”t^p.

Truncation error: the error from cutting off the Taylor series.

Energy drift: slow change of the conserved energy caused by the method.

Stiff problem: one with widely different time scales, where explicit methods need tiny steps.

Explicit Euler

Replace the derivative by a difference using the state at the start of the step:

Text
x_(n+1) = x_n + ฮ”t ยท v_n
v_(n+1) = v_n + ฮ”t ยท a(x_n, v_n)

It is easy, but only first-order accurate: halve ฮ”t and the error halves. For an undamped oscillator it is also unstable in a gentle way. Each step multiplies the amplitude by โˆš(1 + (ฯ‰ฮ”t)ยฒ), always greater than 1, so the energy grows exponentially. For ฯ‰ = 1 and ฮ”t = 0.2, the factor is 1.0198 per step; after 100 steps (about three periods) the amplitude is about seven times larger, matching the pink curve in the figure.

Semi-implicit (symplectic) Euler

Update velocity first, then use the new velocity for the position:

Text
v_(n+1) = v_n + ฮ”t ยท a(x_n, v_n)
x_(n+1) = x_n + ฮ”t ยท v_(n+1)

Still first-order, but it conserves a slightly modified energy, so the amplitude stays bounded for oscillators. It costs the same as Euler and is the default choice in games and in long orbital or vibrational simulations. The method works best for forces that depend on position only.

Runge-Kutta 4 (RK4)

RK4 evaluates the derivative four times per step and combines them. For y' = f(t, y):

Text
kโ‚ = f(t, y)
kโ‚‚ = f(t + ฮ”t/2, y + ฮ”t/2 ยท kโ‚)
kโ‚ƒ = f(t + ฮ”t/2, y + ฮ”t/2 ยท kโ‚‚)
kโ‚„ = f(t + ฮ”t, y + ฮ”t ยท kโ‚ƒ)
y_(n+1) = y + ฮ”t/6 ยท (kโ‚ + 2kโ‚‚ + 2kโ‚ƒ + kโ‚„)

It is fourth-order: halving ฮ”t divides the error by 16. With ฮ”t = 0.2 on the oscillator, the result is visually indistinguishable from the exact cosine over three periods. The price is four force evaluations per step, which is cheap compared with the gain, unless each evaluation is very expensive (a finite-element model). RK4 does not conserve energy exactly, but its drift is tiny for reasonable ฮ”t.

Measuring the order

A good habit: run the same problem with ฮ”t, ฮ”t/2, ฮ”t/4, record the error against the exact answer, and plot error against ฮ”t on log axes. The slope is the order p. If it does not match the theory, there is a bug. This "convergence study" is the best verification test for an integrator.

Choosing a method

Situation Good choice
Quick, rough, or real-time (games) Semi-implicit Euler with a small step
Smooth problem, accuracy matters RK4
Long-time oscillation, orbits Symplectic methods (semi-implicit Euler, velocity Verlet)
Stiff (fast decays mixed with slow motion) Implicit methods or adaptive solvers such as SciPy's solve_ivp with Radau
Unknown Use an adaptive library solver and tight tolerances

Common misconceptions

"Euler is fine if the step is small." For long runs on oscillators the energy still grows; the error accumulates.

"RK4 is always the best." It is expensive per step and not energy-conserving, so for very long runs symplectic methods may do better.

"A smaller step cures instability." It helps for explicit methods on stable problems, but the steps required for stiff problems can be absurdly small.

"Global error equals the error of one step." Errors accumulate over many steps; one order of accuracy is lost (local ฮ”t^(p+1), global ฮ”t^p).

Try it

This program implements the three methods and measures the largest position error over five periods for several time steps, then estimates the order from the slopes. Euler and semi-implicit Euler should show a slope near 1; RK4 near 4.

Python Position error versus step size

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Now a nonlinear case, a pendulum swung from 150ยฐ. We compare RK4 and semi-implicit Euler with a reference from SciPy and print the energy drift of each over 20 seconds.

Python Large-angle pendulum: three solvers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. If you halve the time step of a fourth-order method, the error becomes about:

    Error โˆ ฮ”tโด, so halving ฮ”t divides it by 2โด = 16.
  2. 2. What does explicit Euler do to the energy of an undamped oscillator?

    Each step multiplies the amplitude by โˆš(1 + (ฯ‰ฮ”t)ยฒ) > 1.
  3. 3. How does semi-implicit Euler differ from explicit Euler?

    Using the updated velocity for the position keeps oscillator energy bounded.
  4. 4. How many derivative evaluations does one RK4 step need?

    kโ‚ through kโ‚„ each need one evaluation.
  5. 5. A convergence study plots error against ฮ”t on log-log axes. What does the slope give?

    Error โˆ ฮ”t^p is a straight line with slope p.

Summary

  • Euler is first order and gains energy on oscillators; semi-implicit Euler is first order with bounded energy; RK4 is fourth order.
  • Measure the order with a convergence study; a wrong slope signals a bug.
  • Pick by problem: real-time and long oscillations favor semi-implicit Euler, smooth accuracy favors RK4, stiff problems need implicit or adaptive solvers.
  • Always check energy and halve ฮ”t as a sanity test.

Next: The slider-crank mechanism