Skip to content
Tactiverge

Learning Rooms/✦ Level 5 · Research Engineer/Computing and algorithms

Coding a Fourth-Order Runge-Kutta Integrator in Python and Matlab

Steve Brunton · Length: see YouTube · captions on YouTube · nothing loads until you press play

By the end you can…

  • Implement explicit Euler, semi-implicit Euler and classical RK4 for a first-order system.
  • Show that explicit Euler multiplies the oscillator's energy by (1 + h²) every step.
  • Compare accuracy and long-time behaviour (energy drift) of the three methods.
?Predict▶Watch✓Practise⚙Build

Watch it all: no segment has been checked by hand for this video yet.

1.Predict before you press play

Simulate x″ = −x for one full period with explicit Euler and h = 0.1. The true energy is 0.5. Roughly what does Euler report?

Pick one before you press play. A wrong guess is fine: it is what makes the video stick.

2.See the idea first

Play with this diagram for a minute. It gives you something to hang the video on.

Loading the interactive…

Can't use the diagram? Read a description instead

The integrator diagram plots energy against time for explicit Euler, semi-implicit Euler (dashed) and RK4, with the exact energy 0.5 as a dashed line. Text lists each method's final energy and the Euler growth factor per step.

3.Watch, actively

Writes an RK4 integrator from scratch and tests it on a dynamical system, the same steps as this term's numerical study.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

Watch for…

  • → Watch how the four slope evaluations are combined, and how the code is tested.

Pause whenever you like. Rewatching a hard minute is a good habit, not a failure.

Can't stream it? Read the idea instead

Euler's method steps forward using the slope at the start of each step and drifts quickly. Fourth-order Runge–Kutta samples the slope four times within the step and combines them, giving far smaller error for the same step size.

What the video covers (our own note, written when we verified it): Writes an RK4 integrator from scratch and tests it on a dynamical system, the same steps as this term's numerical study.

Written by Tactiverge in our own words, not a transcript. The full lesson below explains the idea without the video.

Jot down one thing that surprised you and one question you still have.

Private: saved only on this device, never sent to Tactiverge.

4.Practise what you just saw

These come from the lesson that use this video and are checked on the server. Wrong answers get a hint and another try.

Practice 1 · a fresh version every time

For dy/dt = -2y with y(0) = 3, take one classical RK4 step with h = 0.1. What is y₁? (4 decimal places.)

Practice 2

Explicit Euler on x″ = −x with h = 0.05: by what factor does the energy grow per step?

Practice 3

Why might a long satellite-orbit simulation use a symplectic method instead of RK4?

Practice 4 · a fresh version every time

For dy/dt = 0.5y + 1 with y(0) = 2, take one explicit Euler step of size h = 0.05. What is y₁?

6.Build something with it

Follow-up project

Implement all three methods in the Code Lab with a common interface step(f, t, y, h). Reproduce the energy plot from the diagram for h = 0.1 over 20 periods and add the error in x(t) against cos t.

Where this video lives

In the course catalogue: CSE-510 Numerical solvers and complexity

The video is by Steve Brunton, embedded from YouTube with privacy-enhanced mode and never autoplayed. The room around it (prompts, notes, practice, projects) is Tactiverge's, written with AI assistance and checked by automated tests only.