Lesson 3 of 15
Runge-Kutta 4 (RK4)
Runge-Kutta 4th Order
Euler's method uses only the slope at the start of each step. The Runge-Kutta 4 method (RK4) samples the slope at four points within the step and takes a weighted average — achieving fourth-order accuracy.
The Formula
- : slope at the start
- : slope at the midpoint using
- : slope at the midpoint using (refined)
- : slope at the end using
The weights follow Simpson's rule for numerical integration.
Why RK4?
With the same step size, RK4 is dramatically more accurate than Euler. For with :
- Euler: error ~0.005 per step
- RK4: error ~0.000000002 per step
RK4 is the workhorse of scientific computing. It is used in physics simulations, orbital mechanics, and engineering systems everywhere Euler is too inaccurate.
Your Task
Implement rk4_step(f, t, y, h) that returns the next y value using one RK4 step.
Pyodide loading...
Loading...
Click "Run" to execute your code.