Runge-Kutta Methods: Order of Accuracy
Claude prompt
This page was generated by Claude Code from the following prompt (with additional prompting):
let's create a demo, similar to the Euler one, but for multiple integration schemes. This one can be javascript instead of julia because we will not be showing code. Let's integrate a second-order equation, so show graphs of x and xdot and integrate the vector {x}. Show Euler by default, but also have midpoint, Ralston, and classical RK4. Below the time-series plot, also show the log-log error plots with all the trendlines plotted in light grey and the current one bolded
Test problem: a damped pendulum, \[ \ddot{x} = -\sin x - 0.1\,\dot{x}, \qquad x(0) = 2.5,\quad \dot{x}(0) = 0, \] written as a first-order system in the state vector \(\vec{x} = [x,\ \dot{x}]^T\): \[ \dot{\vec{x}} = \vec{f}(t, \vec{x}) = \begin{bmatrix} \dot{x} \\ -\sin x - 0.1\,\dot{x} \end{bmatrix}. \] Every method below applies the same update to the whole vector at once. There is no closed-form solution, so the "exact" curve is RK4 with \(h = 2.5\times10^{-4}\).
One slope per step, taken at the start. First order: global error \(O(h)\).
Predict the end of the step with Euler, then average the slopes at both ends (one corrector iteration). Second order: global error \(O(h^2)\).
Use the Euler slope to reach the midpoint, then step with the slope there. Second order: global error \(O(h^2)\).
Same cost as midpoint with a different sample point and weights chosen to minimize the truncation error bound. Second order: global error \(O(h^2)\).
Four slope evaluations per step. Fourth order: global error \(O(h^4)\).
Black: reference solution. Colored dots: numerical steps. The red bar at \(t_f = 10\) is the global error in each component.
Global error \(\lVert \vec{x}_N - \vec{x}(t_f) \rVert\) at \(t_f\) against \(h\) on log-log axes. The slope of each line is the order of the method.