What is Runge-Kutta method?

A family of numerical methods that gets a more accurate step in a simulation by sampling the rate of change several times within each step.

Runge-Kutta method diagram example

Public diagram from the Machinations Community. Open it on its own page

The Runge-Kutta method is a way to move a simulation forward in time that is much more accurate than the basic Euler step. Instead of using the rate at the start of the step only, it combines several estimates of the rate taken inside the step. It matters when a model must stay accurate with fairly large time steps, such as long forecasts or fast-changing systems.

How it works

The most common version, called RK4, uses four slope estimates per step. The first uses the rate at the start. The second uses the rate at the midpoint, found by moving halfway along the first slope. The third repeats the midpoint using the second slope. The fourth uses the rate at the end, found with the third slope. The new value is the old value plus DT times a weighted average: one sixth of the first, two sixths each of the second and third, and one sixth of the fourth.

RK4 costs four rate calculations per step instead of one, but its error falls with the fourth power of the step size, so it often wins on accuracy per unit of work. Compare it with Euler integration, where error falls only in proportion to the time step. Neither fixes a badly structured model, so accuracy of the method does not replace validation.

Example

Take a quantity of 100 that grows at 10 percent per unit of time, with DT of 1. The four slopes are 10, 10.5, 10.525, and 11.05. Their weighted average is about 10.517, so the new value is 110.517. The exact answer is 110.517 to three decimals, while Euler gave 110. Over 10 steps RK4 stays within a fraction of a percent of the true curve. This is an illustration with round numbers.

How to model it in Machinations

Machinations steps are simple updates, so build the four slopes explicitly. Use four Registers for the slopes, each computed by a formula from the previous one and the current Pool value, and a Converter that adds the weighted average to the Pool. Chart it against a plain Euler Pool and Play. A Lever on the step size shows how much better RK4 holds up.

See Runge-Kutta method in a system you can Play

Build the rules in a Machinations diagram, change a value and Play it to watch how the system responds.