Heun's method (source code)

= Heun's method
{c}
{title2=$y_{n+1}=y_n+h(k_1+k_2)/2$}
{wiki}

Here Heun's method means the explicit endpoint-averaging <Runge-Kutta method>: take an Euler predictor $Y_2=y_n+hf(t_n,y_n)$, sample $k_2=f(t_n+h,Y_2)$ there, and average it with $k_1=f(t_n,y_n)$ for the output. The second <Runge-Kutta stage> is evaluated at the predictor, not at the corrected endpoint. In particular it requires no implicit solve as the ODE <trapezoidal rule> does. Naming conventions vary between books; the formulas specify the method unambiguously.<Numerical solution of ordinary differential equations: One step methods by André Jürgen Massing>[André Massing, Numerical solution of ordinary differential equations: One step methods, October 3, 2021, page 1, Example 1.1: Euler and Heun increment functions]{ref}

For smooth $f$, a <Taylor expansion> about $(t_n,y_n)$ gives $k_2=f+h(f_t+f_yf)+O(h^2)$, where $f_y$ is the state <Jacobian matrix>. Consequently the output is $y_n+hf+h^2(f_t+f_yf)/2+O(h^3)$, matching the exact solution through degree two by the <chain rule>. This gives <local truncation error> $O(h^3)$ and, with the usual bounded smoothness and <Lipschitz continuity> hypotheses on a fixed finite interval, <global error> $O(h^2)$. The <Euler-Heun embedded pair> compares this output with its Euler predictor.

\Image[/euler-heun-slopes-and-stage-construction.png]
{title=Euler-Heun stage geometry for y prime equals y: sample at the Euler predictor, then average slopes for the corrected endpoint}

For $y'=y$, $y_n=1$ and $h=0.2$, the sampled slopes are $1$ and $1.2$. Transporting the second slope to the starting point would end at $1.24$; the average slope $1.1$ instead ends at $1.22$. The exact <exponential function> reaches $e^{0.2}\approx1.22140275816$. The magnified endpoint comparison separates the pair difference $0.02$ from the actual Heun local defect $0.00140275816$.