Unit 10, lesson 128

Euler and Runge-Kutta Methods

How Euler's method and its improved and Runge-Kutta versions step along a solution of y' = f(x, y), how their errors shrink as the step gets smaller, and when a step is too large to be stable.

  • Theory part 255
  • Practice part 256
  • Numerical Differential Equations

A differential equation y′=f(x,y)y' = f(x, y) tells you the slope of the solution at every point, but most such equations have no solution formula. You can still follow the slopes. Stand at the starting point, walk a short way in the direction the equation points, look at the new slope, and walk again. That is Euler’s method, and every method here is a smarter way of choosing the direction for each step.

Key terms

Euler polygon

Definition Consider the initial value problem y′=f(x,y)y' = f(x, y), y(x0)=y0y(x_0) = y_0. Choose a step size h>0h > 0 and set xn=x0+nhx_n = x_0 + nh. Euler’s method computes

yn+1=yn+h f(xn,yn),n=0,1,2,…y_{n+1} = y_n + h \, f(x_n, y_n), \qquad n = 0, 1, 2, \dots

The Euler polygon is the broken line joining the points (x0,y0),(x1,y1),(x2,y2),…(x_0, y_0), (x_1, y_1), (x_2, y_2), \dots. Each segment has the slope that the equation assigns to its left endpoint.

In plain words Follow the tangent line for a distance hh, then recompute the slope where you landed and follow the new tangent.

Example Take y′=x+yy' = x + y, y(0)=1y(0) = 1, with h=0.5h = 0.5. Two steps give

y1=1+0.5(0+1)=1.5,y2=1.5+0.5(0.5+1.5)=2.5.y_1 = 1 + 0.5(0 + 1) = 1.5, \qquad y_2 = 1.5 + 0.5(0.5 + 1.5) = 2.5.

The exact solution is y=2ex−x−1y = 2e^x - x - 1, with y(0.5)≈1.797y(0.5) \approx 1.797 and y(1)≈3.437y(1) \approx 3.437. The polygon runs below the curve because the solutions are concave up here, so each tangent segment undershoots.

Improved Euler method

Definition The improved Euler method first predicts with an Euler step and then averages the slopes at both ends of the step.

k1=f(xn,yn),k2=f(xn+h, yn+hk1),yn+1=yn+h2(k1+k2).k_1 = f(x_n, y_n), \qquad k_2 = f(x_n + h, \, y_n + h k_1), \qquad y_{n+1} = y_n + \frac{h}{2} (k_1 + k_2).

It is the trapezoidal rule applied to

y(xn+1)=y(xn)+∫xnxn+1f(t,y(t)) dt,y(x_{n+1}) = y(x_n) + \int_{x_n}^{x_{n+1}} f(t, y(t)) \, dt,

with the unknown endpoint value replaced by the Euler prediction.

In plain words Take a trial step, look at the slope where it lands, and then redo the step using the average of the old and new slopes.

Example For y′=x+yy' = x + y, y(0)=1y(0) = 1 and h=0.5h = 0.5, one step gives k1=1k_1 = 1, a trial value 1+0.5⋅1=1.51 + 0.5 \cdot 1 = 1.5, k2=f(0.5,1.5)=2k_2 = f(0.5, 1.5) = 2, and

y1=1+0.25(1+2)=1.75.y_1 = 1 + 0.25(1 + 2) = 1.75.

The error at x=0.5x = 0.5 is about 0.0470.047, compared with 0.2970.297 for Euler’s method.

The Runge-Kutta idea

Definition A Runge-Kutta method computes several slopes k1,…,ksk_1, \dots, k_s at trial points inside the step, each built from the previous ones, and advances with a weighted average of them. The weights are chosen so that the step agrees with the Taylor expansion of the true solution through as high a power of hh as possible. The classical fourth order method is

k1=f(xn,yn),k2=f ⁣(xn+h2, yn+h2k1),k3=f ⁣(xn+h2, yn+h2k2),k4=f(xn+h, yn+hk3),\begin{aligned} k_1 &= f(x_n, y_n), & k_2 &= f\!\left(x_n + \tfrac{h}{2}, \, y_n + \tfrac{h}{2} k_1\right), \\ k_3 &= f\!\left(x_n + \tfrac{h}{2}, \, y_n + \tfrac{h}{2} k_2\right), & k_4 &= f(x_n + h, \, y_n + h k_3), \end{aligned}yn+1=yn+h6(k1+2k2+2k3+k4).y_{n+1} = y_n + \frac{h}{6} (k_1 + 2k_2 + 2k_3 + k_4).

When ff depends only on xx, this step is exactly Simpson’s rule on [xn,xn+1][x_n, x_{n+1}] with the midpoint xn+h2x_n + \frac{h}{2}.

In plain words Sample the slope at the start, twice in the middle and at the end, and average them with the middle counting double.

Example For y′=x+yy' = x + y, y(0)=1y(0) = 1 and h=0.5h = 0.5, the four slopes are

k1=1,k2=f(0.25,1.25)=1.5,k3=f(0.25,1.375)=1.625,k4=f(0.5,1.8125)=2.3125,k_1 = 1, \quad k_2 = f(0.25, 1.25) = 1.5, \quad k_3 = f(0.25, 1.375) = 1.625, \quad k_4 = f(0.5, 1.8125) = 2.3125,

so

y1=1+0.56(1+3+3.25+2.3125)=1.796875.y_1 = 1 + \frac{0.5}{6}(1 + 3 + 3.25 + 2.3125) = 1.796875.

The exact value is 1.7974431.797443, so a single large step is already off by only 0.00060.0006.

Local and global error

Definition The local truncation error of a one-step method at step nn is the error made in that one step when it starts from the exact value, that is y(xn+1)y(x_{n+1}) minus the result of the method’s step applied to y(xn)y(x_n). The global error at xnx_n is en=y(xn)−yne_n = y(x_n) - y_n, the accumulated difference after all steps. A method has order pp if its local error is O(hp+1)O(h^{p+1}), and then for a smooth problem its global error on a fixed bounded interval is O(hp)O(h^p). Euler’s method has order 11, improved Euler order 22 and classical Runge-Kutta order 44.

For Euler’s method there is an explicit bound. Suppose ff is continuous for x0≤x≤bx_0 \le x \le b and all real yy, and satisfies the Lipschitz condition

∣f(x,u)−f(x,v)∣≤L∣u−v∣\lvert f(x, u) - f(x, v) \rvert \le L \lvert u - v \rvert

there for all uu and vv, with a constant L>0L > 0. Suppose also that the exact solution exists on [x0,b][x_0, b], is twice differentiable there, and has ∣y′′∣≤M\lvert y'' \rvert \le M on [x0,b][x_0, b]. If the arithmetic is exact and the method starts from the exact value y0=y(x0)y_0 = y(x_0), then for every xn≤bx_n \le b

∣en∣≤hM2L(eL(xn−x0)−1).\lvert e_n \rvert \le \frac{hM}{2L} \left( e^{L (x_n - x_0)} - 1 \right).

In plain words Each step adds a small new error, and the old errors are carried along. About 1h\frac{1}{h} steps of size hp+1h^{p+1} add up to an error of size hph^p.

Example For y′=yy' = y, y(0)=1y(0) = 1 on [0,1][0, 1] we have L=1L = 1 and ∣y′′∣=ex≤e\lvert y'' \rvert = e^x \le e. With h=0.1h = 0.1 the bound is

0.1⋅e2(e−1)≈0.234.\frac{0.1 \cdot e}{2}(e - 1) \approx 0.234.

Euler’s method gives y10=1.110≈2.593742y_{10} = 1.1^{10} \approx 2.593742, and the actual global error e−2.593742≈0.1245e - 2.593742 \approx 0.1245 is within it.

Step-size refinement

Definition Step-size refinement means repeating a computation with hh replaced by h2\frac{h}{2} and comparing. Write yhy_h for the result at a fixed point xx computed with step hh. For a method of order pp the global error at xx is about ChpCh^p for small hh, with CC independent of hh, so halving hh divides the error by about 2p2^p, and the error of the finer result is estimated by

y(x)−yh/2≈yh/2−yh2p−1.y(x) - y_{h/2} \approx \frac{y_{h/2} - y_h}{2^p - 1}.

In plain words Halve the step and see how much the answer moves. The size of the change tells you roughly how wrong the finer answer still is.

Example For y′=yy' = y, y(0)=1y(0) = 1, here are the errors at x=1x = 1.

hhEulerImproved EulerRunge-Kutta
0.10.11.25×10−11.25 \times 10^{-1}4.20×10−34.20 \times 10^{-3}2.08×10−62.08 \times 10^{-6}
0.050.056.50×10−26.50 \times 10^{-2}1.09×10−31.09 \times 10^{-3}1.36×10−71.36 \times 10^{-7}
0.0250.0253.32×10−23.32 \times 10^{-2}2.78×10−42.78 \times 10^{-4}8.67×10−98.67 \times 10^{-9}

Each halving divides the errors by about 22, 44 and 1616. For Euler, y0.1≈2.593742y_{0.1} \approx 2.593742 and y0.05≈2.653298y_{0.05} \approx 2.653298 differ by 0.05960.0596. With p=1p = 1 that difference is the estimate for the error of y0.05y_{0.05}, and it is close, since the actual error is 0.06500.0650.

Stability

Definition Apply a method to the test equation y′=λyy' = \lambda y with λ<0\lambda < 0, whose solutions all decay to 00. The method is absolutely stable for a step hh if its numerical solution also tends to 00 for every starting value. Euler’s method gives yn+1=(1+hλ)yny_{n+1} = (1 + h\lambda) y_n, so yn=(1+hλ)ny0y_n = (1 + h\lambda)^n y_0, and it is absolutely stable exactly when

∣1+hλ∣<1,that is0<h<2∣λ∣.\lvert 1 + h\lambda \rvert < 1, \quad \text{that is} \quad 0 < h < \frac{2}{\lvert \lambda \rvert}.

Every explicit Runge-Kutta method has some such limit. On the test equation its step multiplies yny_n by a polynomial in hλh\lambda that is not constant, and a nonconstant polynomial grows without bound in absolute value as hλ→−∞h\lambda \to -\infty, so a large enough step always makes the numbers grow.

In plain words If the step is too large for how quickly the true solution changes, the numbers overshoot, flip sign and blow up even though the true solution quietly goes to zero.

Example For y′=−10yy' = -10y, y(0)=1y(0) = 1 the limit is h<0.2h < 0.2. With h=0.25h = 0.25 the factor is 1−2.5=−1.51 - 2.5 = -1.5, giving 1,−1.5,2.25,−3.375,5.06251, -1.5, 2.25, -3.375, 5.0625 at x=0,0.25,0.5,0.75,1x = 0, 0.25, 0.5, 0.75, 1, while the true value at x=1x = 1 is e−10≈4.5×10−5e^{-10} \approx 4.5 \times 10^{-5}. With h=0.05h = 0.05 the factor is 0.50.5, and the values 1,0.5,0.25,0.125,0.06251, 0.5, 0.25, 0.125, 0.0625 at x=0,0.05,0.1,0.15,0.2x = 0, 0.05, 0.1, 0.15, 0.2 decay as they should.

Numerical versus exact solution

Definition An exact solution is a differentiable function yy that satisfies y′=f(x,y)y' = f(x, y) and the initial condition on an interval containing x0x_0. A numerical solution is a finite list of values yny_n at the grid points xnx_n, which depends on the method and on hh and approximates y(xn)y(x_n) only where the exact solution exists and the method converges. Between grid points it gives no values unless you interpolate.

In plain words The computer always returns numbers, even when they mean nothing. Theory tells you whether a solution exists and where.

Example The problem y′=y2y' = y^2, y(0)=1y(0) = 1 has the exact solution

y=11−x,y = \frac{1}{1 - x},

which blows up at x=1x = 1 and does not exist past it. Euler’s method with h=0.25h = 0.25 happily gives 1.251.25, 1.6411.641, 2.3142.314, 3.6523.652 and 6.9856.985 at x=0.25,0.5,0.75,1,1.25x = 0.25, 0.5, 0.75, 1, 1.25. The last two numbers approximate nothing, and nothing in the list itself warns you.

Check yourself

  1. Do two Euler steps with h=0.1h = 0.1 for y′=2yy' = 2y, y(0)=1y(0) = 1, and compare with the exact value at x=0.2x = 0.2.
Show the answer

The two steps give

y1=1+0.1⋅2=1.2,y2=1.2+0.1⋅2.4=1.44.y_1 = 1 + 0.1 \cdot 2 = 1.2, \qquad y_2 = 1.2 + 0.1 \cdot 2.4 = 1.44.

The exact solution is e2xe^{2x}, and e0.4≈1.4918e^{0.4} \approx 1.4918, so the error is about 0.0520.052.

  1. A second order method has a global error of 0.0080.008 at some point with step hh. About what error do you expect with step h2\frac{h}{2}?
Show the answer

Order 22 means halving hh divides the error by about 22=42^2 = 4, so about 0.0020.002.

  1. For which step sizes is Euler’s method absolutely stable on y′=−50yy' = -50y?
Show the answer

You need ∣1−50h∣<1\lvert 1 - 50h \rvert < 1, so 0<h<250=0.040 < h < \frac{2}{50} = 0.04.

How this lesson connects

It also uses iteration and error.