James Pelletier
MAS.864 Nature of Mathematical Modeling
Finite Differences: Ordinary Differential Equations

(7.1) To calculate the second-order approximation error of the Heun method, we compare the Taylor expansion of the original function $y(x)$ $$\begin{eqnarray} y(x + h) &=& y(x) + h \left. \frac{dy}{dx} \right|_x + \frac{h^2}{2} \left. \frac{d^2 y}{dx^2} \right|_x + \mathcal{O} \left(h^3 \right) \nonumber \\ &=& y(x) + h f(x, y(x)) + \frac{h^2}{2} \left( \frac{\partial f}{\partial x} + f \frac{\partial f}{\partial y} \right) + \mathcal{O} \left(h^3 \right) \nonumber \end{eqnarray}$$
to the Taylor expansion of the Heun expression $$\begin{eqnarray} f \big(x + h, y(x) + h f(x, y(x)) \big) &=& f(x, y(x)) + h \frac{d}{dh} \left. f \big(x + h, y(x) + h f(x, y(x)) \big) \right|_{h = 0} + \mathcal{O} \left(h^2 \right) \nonumber \\ &=& f(x, y(x)) + h \left( \frac{\partial f}{\partial x} + f \frac{\partial f}{\partial y} \right) \nonumber \\[7 pt] y(x + h) &=& y(x) + \frac{h}{2} \left\{ f(x, y(x)) + f \big(x + h, y(x) + h f(x, y(x)) \big) \right\} \nonumber \\ &=& y(x) + h f(x, y(x)) + \frac{h^2}{2} \left( \frac{\partial f}{\partial x} + f \frac{\partial f}{\partial y} \right) + \mathcal{O} \left(h^3 \right) \end{eqnarray}$$
Therefore, the Heun method agrees with the solution of the differential equation to second order without evaluating the derivative of $f$.

(7.2) code
Euler's method

4th order Runge-Kutta



(7.4) code