I used to give this example of an initial-value problem whenever numerical solutions of ODE came up in class:
The exact solution is . Using Euler’s method with step (admittedly large) we get, starting with :
The numerical solution drops down to zero at once and stays there. Not good at all.
The trapezoidal version of RK2 (also known as improved Euler, or Heun’s method) treats the above value of merely as prediction , and computes the actual using the average of the slopes at and :
… which is right on target. Let’s do it again: the prediction and correction
… which is , compared to the exact solution . Nice!
I like the example, because all numbers are manageable with mental arithmetics for two steps. And it’s clear that is pretty close to .
But the fact that the relative error stays below throughout the computation despite the large step size seems… too good to be true. Consider that for the exact solution, the expression can differ by the factor of between two -values at distance .
The midpoint version of RK2, which generally performs about as well as the trapezoidal version, is nowhere as close in this example. Indeed, using the same data as above, we get
and so forth: the relative error reaches at the second step. That’s 16 times worse than the trapezoidal version.
What happens here has less to do with numerical analysis than with algebra of rational functions. Using in trapezoidal RK2, we are in effect iterating the function
The exact solution would be obtained by iterating
Two functions just happen to coincide at , which is our starting point here.
From there we get to , and on they are really close anyway, due to another coincidence: the truncation error is instead of as it is normally for second-order methods.
The midpoint method with amounts to iterating
which is not substantially further away from , but does not enjoy the lucky coincidence at .
The tables are turned with the initial value . The exact solution is , which drops sharply from to ; its slope decreases by the factor of during one step. Still, midpoint-RK2 does a decent job with :
while trapezoidal RK2 outputs , , and promptly overflows.
With a reasonable step size, like , normality is restored: both methods perform about equally well, with error for trapezoidal and for midpoint.