Differential EquationsQualitative and Numerical Methods3 of 3
Qualitative and Numerical Methods · 3

Euler's Method and Runge-Kutta

Most differential equations cannot be solved with algebra for an exact formula y(t)\truth{y(t)}, so we need a way to trace the solution curve step by step using only its slopes.

1

Tracing a Curve with Euler's Method

To see how stepping along slopes works, we will estimate y(1)\truth{y(1)} for the differential equation

dydt\displaystyle \slope{\frac{dy}{dt}} =y,starting at (0,1)\displaystyle = y, \quad \text{starting at } (0, 1)

using a single step of size 11, and compare our estimate to the exact solution y=et\truth{y = e^t}.

Tracing a Curve with Euler's Method
dydt=t+y\displaystyle \htmlClass{lc-pink}{\frac{dy}{dt}} = t + y
y(t)=  ?\htmlClass{lc-blue}{y(t)} = \;?
t\htmlClass{lc-mute}{t}
y\htmlClass{lc-mute}{y}
(0, 1)(0,\ 1)

Every Euler step moves forward from the current point along its tangent slope:

New y\displaystyle \text{New } y =Old y+(Slope×Step)\displaystyle = \text{Old } y + (\slope{\text{Slope}} \times \text{Step})
Text solution
Find the slope at the starting pointPlug (0,1)(0, 1) into dydt=y\slope{\frac{dy}{dt}} = y to get the tangent slope right at the start.
Slope\displaystyle \slope{\text{Slope}} =1\displaystyle = \slope{1}
Step forward along the tangent lineMultiply the starting slope by the step size of 11 and add that change to the starting yy-value.
New y\displaystyle \text{New } y =1⏟Old y+(1⏟Slope×1⏟Step)\displaystyle = \underbrace{1}_{\text{Old } y} + (\underbrace{\slope{1}}_{\slope{\text{Slope}}} \times \underbrace{1}_{\text{Step}}) =2\displaystyle = 2

So one Euler step estimates y(1)≈2\truth{y(1)} \approx 2, whereas the true curve y=et\truth{y = e^t} reaches e≈2.718\truth{e \approx 2.718}.

2

Taking Multiple Euler Steps

A single large step drifts away from a bending curve, and most differential equations depend on both tt and yy. To follow the curve more closely, we break the interval into a chain of smaller steps and recalculate the slope from both tt and yy at each new point we reach.

Here we want to estimate y(0.6)\truth{y(0.6)} in three steps of size 0.20.2 for

dydt\displaystyle \slope{\frac{dy}{dt}} =t+y,starting at (0,1)\displaystyle = t + y, \quad \text{starting at } (0, 1)
Taking Multiple Euler Steps
0.2\htmlClass{lc-mute}{0.2}
0.4\htmlClass{lc-mute}{0.4}
0.6\htmlClass{lc-mute}{0.6}
1.5\htmlClass{lc-mute}{1.5}
2\htmlClass{lc-mute}{2}
t\htmlClass{lc-mute}{t}
y\htmlClass{lc-mute}{y}
(0, 1)(0,\ 1)
dydt=t+y\displaystyle \htmlClass{lc-pink}{\frac{dy}{dt}} = t + y
Step=0.2\text{Step} = 0.2
y(0.6)=  ?\htmlClass{lc-blue}{y(0.6)} = \;?
Text solution
Step 1: From t=0t = 0 to t=0.2t = 0.2Plug the starting point (0,1)(0, 1) into t+yt + y to find the first slope and move yy forward by one step of 0.20.2.
Slope=0+1=1New y=1+(1×0.2)=1.2\displaystyle \begin{aligned} \slope{\text{Slope}} &= 0 + 1 = \slope{1} \\ \text{New } y &= 1 + (\slope{1} \times 0.2) = 1.2 \end{aligned}
Step 2: From t=0.2t = 0.2 to t=0.4t = 0.4Plug the new point (0.2,1.2)(0.2, 1.2) into t+yt + y to update the slope before taking the second step of 0.20.2.
Slope=0.2+1.2=1.4New y=1.2+(1.4×0.2)=1.2+0.28=1.48\displaystyle \begin{aligned} \slope{\text{Slope}} &= 0.2 + 1.2 = \slope{1.4} \\ \text{New } y &= 1.2 + (\slope{1.4} \times 0.2) = 1.2 + 0.28 = 1.48 \end{aligned}
Step 3: From t=0.4t = 0.4 to t=0.6t = 0.6Plug (0.4,1.48)(0.4, 1.48) into t+yt + y for the third slope and take the final step of 0.20.2.
Slope=0.4+1.48=1.88New y=1.48+(1.88×0.2)=1.48+0.376=1.856\displaystyle \begin{aligned} \slope{\text{Slope}} &= 0.4 + 1.48 = \slope{1.88} \\ \text{New } y &= 1.48 + (\slope{1.88} \times 0.2) = 1.48 + 0.376 = 1.856 \end{aligned}

So after three steps of size 0.20.2, Euler's method estimates y(0.6)≈1.856\truth{y(0.6)} \approx 1.856.

3

Concavity and Overestimates vs. Underestimates

In both examples so far, Euler's estimate landed below the true curve. We want to know without solving the differential equation whether Euler's method will give an overestimate or an underestimate.

t\htmlClass{lc-mute}{t}
y\htmlClass{lc-mute}{y}
d2ydt2>0\htmlClass{lc-orange}{\dfrac{d^2y}{dt^2}} > 0
concave up\htmlClass{lc-orange}{\text{concave up}}
Underestimate\text{Underestimate}
t\htmlClass{lc-mute}{t}
y\htmlClass{lc-mute}{y}
d2ydt2<0\htmlClass{lc-orange}{\dfrac{d^2y}{dt^2}} < 0
concave down\htmlClass{lc-orange}{\text{concave down}}
Overestimate\text{Overestimate}

Remember, the sign of the second derivative d2ydt2\curv{\frac{d^2y}{dt^2}} tells us which way a curve bends compared to its tangent line. Because the differential equation already gives the first derivative dydt\slope{\frac{dy}{dt}}, we can differentiate both sides with respect to tt to find d2ydt2\curv{\frac{d^2y}{dt^2}} at the starting point.

We will check whether Euler's method overestimates or underestimates near the start for

dydt\displaystyle \slope{\frac{dy}{dt}} =2−y,starting at (0,0)\displaystyle = 2 - y, \quad \text{starting at } (0, 0)
Concavity and Overestimates vs. Underestimates
1\htmlClass{lc-mute}{1}
0.6\htmlClass{lc-mute}{0.6}
t\htmlClass{lc-mute}{t}
y\htmlClass{lc-mute}{y}
t\htmlClass{lc-mute}{t}
y\htmlClass{lc-mute}{y}
dydt=y\displaystyle \htmlClass{lc-pink}{\frac{dy}{dt}} = y
dydt=t+y\displaystyle \htmlClass{lc-pink}{\frac{dy}{dt}} = t + y
2.718\htmlClass{lc-blue}{2.718}
22
2.044\htmlClass{lc-blue}{2.044}
1.8561.856
Always too low?\text{Always too low?}
Text solution
Find the first derivative at the starting pointPlug (0,0)(0, 0) into the differential equation to get the starting slope.
dydt\displaystyle \slope{\frac{dy}{dt}} =2−0\displaystyle = 2 - 0 =2\displaystyle = \slope{2}
Differentiate both sides with respect to tt and plug in dydt\slope{\frac{dy}{dt}}Take the tt-derivative of 2−y2 - y (remembering that y\truth{y} is a function of tt, so its derivative is dydt\slope{\frac{dy}{dt}}, not 11) to find the concavity at (0,0)(0, 0).
d2ydt2\displaystyle \curv{\frac{d^2y}{dt^2}} =−dydt\displaystyle = -\slope{\frac{dy}{dt}} =−2<0\displaystyle = \curv{-2} < 0

Because d2ydt2\curv{\frac{d^2y}{dt^2}} is negative, the solution curve is concave down and bends underneath its tangent line, so Euler's method gives an overestimate.

For dydt=2−y\slope{\frac{dy}{dt}} = 2 - y, solution curves starting above y=2y = 2 are concave up so the Euler steps stay underneath them, solution curves starting below y=2y = 2 are concave down so the Euler steps stay above them, and right on y=2y = 2 the curve is a flat line and Euler is exact.

Every solution curve here bends toward the flat line at height 2, so whether Euler’s straight steps land above or below the true curve depends on which side of that line you start on.
4

Heun's Method and the Midpoint Method

Euler's method drifts off a curving solution because it only looks at the slope at the very beginning of the step. Even on a large step of size 11 for

dydt\displaystyle \slope{\frac{dy}{dt}} =y,starting at (0,1)\displaystyle = y, \quad \text{starting at } (0, 1)

we can get much closer to y(1)=e≈2.718\truth{y(1) = e \approx 2.718} if we use a trial Euler step to see how the slope changes before we take our real step from (0,1)(0, 1).

Heun's Method and the Midpoint Method
1\htmlClass{lc-mute}{1}
t\htmlClass{lc-mute}{t}
y\htmlClass{lc-mute}{y}
(0, 1)(0,\ 1)
dydt=y\displaystyle \htmlClass{lc-pink}{\frac{dy}{dt}} = y
e≈2.718\htmlClass{lc-blue}{e \approx 2.718}
Euler  2\text{Euler}\ \ 2
A better slope?\text{A better } \htmlClass{lc-pink}{\text{slope}}\text{?}

In both methods below, the first Euler step is only a trial to find a better slope: you always return to the starting point (0,1)(0, 1) to take the actual step.

Text solution
Heun's Method: Find the end slope and averageTake a trial Euler step of 11 from (0,1)(0, 1) to find the slope at the trial endpoint (1,2)(1, 2), then step from (0,1)(0, 1) using the average of the start and end slopes.
Trial end y=1+(1×1)=2  ⟹  End slope=2Average Slope=1+22=1.5New y=1+(1.5×1)=2.5\displaystyle \begin{aligned} \text{Trial end } y &= 1 + (\slope{1} \times 1) = 2 \implies \slope{\text{End slope}} = \slope{2} \\ \slope{\text{Average Slope}} &= \frac{\slope{1} + \slope{2}}{2} = \slope{1.5} \\ \text{New } y &= 1 + (\slope{1.5} \times 1) = 2.5 \end{aligned}
Midpoint Method: Find the slope halfway acrossTake a trial Euler half-step of 0.50.5 from (0,1)(0, 1) to find the slope at the midpoint (0.5,1.5)(0.5, 1.5), then take the full step of 11 from (0,1)(0, 1) using that midpoint slope.
Midpoint y=1+(1×0.5)=1.5  ⟹  Midpoint slope=1.5New y=1+(1.5×1)=2.5\displaystyle \begin{aligned} \text{Midpoint } y &= 1 + (\slope{1} \times 0.5) = 1.5 \implies \slope{\text{Midpoint slope}} = \slope{1.5} \\ \text{New } y &= 1 + (\slope{1.5} \times 1) = 2.5 \end{aligned}

Both methods estimate y(1)≈2.5\truth{y(1)} \approx 2.5 in a single step of size 11, cutting Euler's error by more than half.

Along the trial Euler step from t=0t = 0 to t=1t = 1 on dydt=y\slope{\frac{dy}{dt}} = y, the slope at the start (t=0t = 0) is too flat and lands well below y=et\truth{y = e^t}, the slope at the trial end (t=1t = 1) is steeper, and the slope halfway across (t=0.5t = 0.5, the Midpoint Method) is in between and lands much closer to the true curve.

Taking a slope from halfway along the trial Euler step and using it from the start gives a balanced step that lands much closer to the true curve.
5

Fourth-Order Runge-Kutta (RK4)

The Midpoint Method has one weak spot: it uses the starting slope to step halfway across, so its guess for where the midpoint sits is already slightly off. Fourth-Order Runge-Kutta (RK4) fixes this by checking four slopes across the interval and averaging them.

We will test one RK4 step of size 11 on

dydt\displaystyle \slope{\frac{dy}{dt}} =y,starting at (0,1)\displaystyle = y, \quad \text{starting at } (0, 1)
Fourth-Order Runge-Kutta (RK4)
k1,k2,k3,k4\displaystyle \slope{k_1}, \quad \slope{k_2}, \quad \slope{k_3}, \quad \slope{k_4}
dydt\displaystyle \slope{\frac{dy}{dt}} =y,(0, 1),Step\displaystyle = y, \quad (0,\ 1), \quad \text{Step} =1\displaystyle = 1
RK4:New y\displaystyle \text{RK4:}\quad \text{New } y =  ?\displaystyle = \;?
Text solution
Slope 1 at the startAt (0,1)(0, 1), read the starting slope from dydt=y\slope{\frac{dy}{dt}} = y.
Slope 1\displaystyle \slope{\text{Slope } 1} =1\displaystyle = \slope{1}
Slope 2 at the first midpoint guessTake a half-step of 0.50.5 from y=1y = 1 using Slope 1 to estimate the midpoint yy and the slope there.
y\displaystyle y =1+(1×0.5)\displaystyle = 1 + (\slope{1} \times 0.5) =1.5  ⟹  Slope 2\displaystyle = 1.5 \implies \slope{\text{Slope } 2} =1.5\displaystyle = \slope{1.5}
Slope 3 at the corrected midpoint guessReturn to y=1y = 1 and take a half-step of 0.50.5 using Slope 2 to find the corrected midpoint yy and its slope.
y\displaystyle y =1+(1.5×0.5)\displaystyle = 1 + (\slope{1.5} \times 0.5) =1.75  ⟹  Slope 3\displaystyle = 1.75 \implies \slope{\text{Slope } 3} =1.75\displaystyle = \slope{1.75}
Slope 4 at the end guessReturn to y=1y = 1 and take a full step of 11 using Slope 3 to estimate the endpoint yy and its slope.
y\displaystyle y =1+(1.75×1)\displaystyle = 1 + (\slope{1.75} \times 1) =2.75  ⟹  Slope 4\displaystyle = 2.75 \implies \slope{\text{Slope } 4} =2.75\displaystyle = \slope{2.75}
Step from the start with the 1,2,2,11, 2, 2, 1 weighted average slopeGive the two midpoint slopes double weight, divide by the total weight of 66, and take the full step of 11 from y=1y = 1.
Average Slope=Slope 1+2(Slope 2)+2(Slope 3)+Slope 46=1+2(1.5)+2(1.75)+2.756=10.256≈1.70833New y=1+(1.70833×1)=2.70833\displaystyle \begin{aligned} \slope{\text{Average Slope}} &= \frac{\slope{\text{Slope } 1} + 2(\slope{\text{Slope } 2}) + 2(\slope{\text{Slope } 3}) + \slope{\text{Slope } 4}}{6} \\ &= \frac{\slope{1} + 2(\slope{1.5}) + 2(\slope{1.75}) + \slope{2.75}}{6} = \frac{10.25}{6} \approx \slope{1.70833} \\ \text{New } y &= 1 + (\slope{1.70833} \times 1) = 2.70833 \end{aligned}

In a single step of size 11, RK4 estimates y(1)≈2.70833\truth{y(1)} \approx 2.70833, landing within 0.01\err{0.01} of the exact value e≈2.71828\truth{e \approx 2.71828}.

6

An RK4 Step with Both tt and yy

When the differential equation depends on both tt and yy, we also have to plug in the right tt-value for where each trial point sits: at the start, halfway across, or at the end of the step.

Now we will find y(0.2)\truth{y(0.2)} in one RK4 step of size 0.20.2 for

dydt\displaystyle \slope{\frac{dy}{dt}} =t+y,starting at (0,1)\displaystyle = t + y, \quad \text{starting at } (0, 1)
An RK4 Step with Both t and y
0\htmlClass{lc-mute}{0}
0.1\htmlClass{lc-mute}{0.1}
0.2\htmlClass{lc-mute}{0.2}
1.1\htmlClass{lc-mute}{1.1}
1.2\htmlClass{lc-mute}{1.2}
t\htmlClass{lc-mute}{t}
y\htmlClass{lc-mute}{y}
(0, 1)(0,\ 1)
dydt=t+y\displaystyle \htmlClass{lc-pink}{\frac{dy}{dt}} = t + y
y(0.2)=  ?\htmlClass{lc-blue}{y(0.2)} = \;?
Slope 1\htmlClass{lc-pink}{\text{Slope 1}}
Slope 2\htmlClass{lc-pink}{\text{Slope 2}}
Slope 3\htmlClass{lc-pink}{\text{Slope 3}}
Slope 4\htmlClass{lc-pink}{\text{Slope 4}}
Text solution
Slope 1 at the start (t=0t = 0)Plug (0,1)(0, 1) into t+yt + y.
Slope 1\displaystyle \slope{\text{Slope } 1} =0+1\displaystyle = 0 + 1 =1.0\displaystyle = \slope{1.0}
Slope 2 at the first midpoint (t=0.1t = 0.1)Move tt forward by a half-step to 0.10.1, take a half-step of 0.10.1 from y=1y = 1 using Slope 1, and plug into t+yt + y.
ymid1=1+(1.0×0.1)=1.1Slope 2=0.1+1.1=1.2\displaystyle \begin{aligned} y_{\text{mid1}} &= 1 + (\slope{1.0} \times 0.1) = 1.1 \\ \slope{\text{Slope } 2} &= 0.1 + 1.1 = \slope{1.2} \end{aligned}
Slope 3 at the corrected midpoint (t=0.1t = 0.1)Keep tt at the halfway mark 0.10.1, take a new half-step of 0.10.1 from y=1y = 1 using Slope 2, and plug into t+yt + y.
ymid2=1+(1.2×0.1)=1.12Slope 3=0.1+1.12=1.22\displaystyle \begin{aligned} y_{\text{mid2}} &= 1 + (\slope{1.2} \times 0.1) = 1.12 \\ \slope{\text{Slope } 3} &= 0.1 + 1.12 = \slope{1.22} \end{aligned}
Slope 4 at the end (t=0.2t = 0.2)Move tt forward by a full step to 0.20.2, take a full step of 0.20.2 from y=1y = 1 using Slope 3, and plug into t+yt + y.
yend=1+(1.22×0.2)=1.244Slope 4=0.2+1.244=1.444\displaystyle \begin{aligned} y_{\text{end}} &= 1 + (\slope{1.22} \times 0.2) = 1.244 \\ \slope{\text{Slope } 4} &= 0.2 + 1.244 = \slope{1.444} \end{aligned}
Step from the start with the weighted average slopeAverage the four slopes with weights 1,2,2,11, 2, 2, 1 and take the real step of 0.20.2 from y=1y = 1.
Average Slope=1.0+2(1.2)+2(1.22)+1.4446=1.214New y=1+(1.214×0.2)=1.2428\displaystyle \begin{aligned} \slope{\text{Average Slope}} &= \frac{\slope{1.0} + 2(\slope{1.2}) + 2(\slope{1.22}) + \slope{1.444}}{6} = \slope{1.214} \\ \text{New } y &= 1 + (\slope{1.214} \times 0.2) = 1.2428 \end{aligned}

So one RK4 step gives y(0.2)≈1.2428\truth{y(0.2)} \approx 1.2428, matching the exact value 1.2428055\truth{1.2428055} to five digits. (When finding Slope 3, be careful not to add another half-step to tt: Slope 2 and Slope 3 both sit at the exact same halfway time t=0.1t = 0.1.)

7

Halving the Step Size and Order of Accuracy

Besides comparing methods on the same step size, we want to know how fast each method's error at a target time shrinks when we cut the step size in half.

Halving the Step Size and Order of Accuracy
dydt=y\displaystyle \htmlClass{lc-pink}{\frac{dy}{dt}} = y
y(1)=e≈2.718282\htmlClass{lc-blue}{y(1) = e \approx 2.718282}
error\htmlClass{lc-orange}{\text{error}}
hh
Euler\text{Euler}
Heun,\text{Heun,}
Midpoint\text{Midpoint}
RK4\text{RK4}
11
?\htmlClass{lc-orange}{?}
?\htmlClass{lc-orange}{?}
?\htmlClass{lc-orange}{?}
0.50.5
?\htmlClass{lc-orange}{?}
?\htmlClass{lc-orange}{?}
?\htmlClass{lc-orange}{?}
0.250.25
?\htmlClass{lc-orange}{?}
?\htmlClass{lc-orange}{?}
?\htmlClass{lc-orange}{?}
0.1250.125
?\htmlClass{lc-orange}{?}
?\htmlClass{lc-orange}{?}
?\htmlClass{lc-orange}{?}

Once the steps are small, each time you halve the step size, the error at the target time gets multiplied by a factor that matches the order of the method:

Euler (First-Order):Halving the step size multiplies the error by about 121=12Heun and Midpoint (Second-Order):Halving the step size multiplies the error by about 122=14RK4 (Fourth-Order):Halving the step size multiplies the error by about 124=116\displaystyle \begin{aligned} \text{Euler (First-Order):} \quad &\text{Halving the step size multiplies the error by about } \err{\frac{1}{2^1}} = \err{\frac{1}{2}} \\ \text{Heun and Midpoint (Second-Order):} \quad &\text{Halving the step size multiplies the error by about } \err{\frac{1}{2^2}} = \err{\frac{1}{4}} \\ \text{RK4 (Fourth-Order):} \quad &\text{Halving the step size multiplies the error by about } \err{\frac{1}{2^4}} = \err{\frac{1}{16}} \end{aligned}

As the equal steps from t=0t = 0 to t=1t = 1 on dydt=y\slope{\frac{dy}{dt}} = y are cut in half, Euler's error at t=1t = 1 shrinks only to between about two-thirds and a half of what it was each time, the Midpoint Method's error drops to between about a third and a quarter, and RK4's estimate gets very close to y=et\truth{y = e^t} almost right away.

Each time you cut the step width in half, Euler’s error gap shrinks only to between about two-thirds and a half of its height, the Midpoint Method’s to between about a third and a quarter, and RK4 sits right on the true curve almost right away.
8

Standard Notation

You will usually see the slope formula written as dydt=f(t,y)\slope{\frac{dy}{dt}} = \slope{f}(t, y), the step size as hh, the current point as (tn,yn)(t_n, y_n), the next point as (tn+1,yn+1)(t_{n+1}, y_{n+1}) where tn+1=tn+ht_{n+1} = t_n + h, and the slopes as k1,k2,k3,k4\slope{k_1}, \slope{k_2}, \slope{k_3}, \slope{k_4}.

Standard Textbook Notation and Summary
tn\htmlClass{lc-mute}{t_n}
tn+1\htmlClass{lc-mute}{t_{n+1}}
t\htmlClass{lc-mute}{t}
y\htmlClass{lc-mute}{y}
dydt=f(t, y)\displaystyle \htmlClass{lc-pink}{\frac{dy}{dt}} = \htmlClass{lc-pink}{f}(t,\ y)
(tn, yn)(t_n,\ y_n)
k1\htmlClass{lc-pink}{k_1}
hh
yn+1=  ?y_{n+1} = \;?

Written in these symbols, every method follows the exact same steps we just did in words:

Euler’s Method:yn+1⏟New y=yn⏟Old y+h⏟Step⋅f(tn,yn)⏟Start slopeHeun’s Method:k1=f(tn,yn)k2=f(tn+h, yn+hk1)yn+1=yn+h(k1+k22)Midpoint Method:k1=f(tn,yn)k2=f ⁣(tn+12h, yn+12hk1)yn+1=yn+h⋅k2RK4:k1=f(tn,yn)⏟Start slopek2=f ⁣(tn+12h, yn+12hk1)⏟First midpoint slopek3=f ⁣(tn+12h, yn+12hk2)⏟Corrected midpoint slopek4=f(tn+h, yn+hk3)⏟End slopeyn+1=yn+h(k1+2k2+2k3+k46)\displaystyle \begin{aligned} \textbf{Euler's Method:} \quad \underbrace{y_{n+1}}_{\text{New } y} &= \underbrace{y_n}_{\text{Old } y} + \underbrace{h}_{\text{Step}} \cdot \underbrace{\slope{f}(t_n, y_n)}_{\slope{\text{Start slope}}} \\[10pt] \textbf{Heun's Method:} \quad \slope{k_1} &= \slope{f}(t_n, y_n) \\ \slope{k_2} &= \slope{f}(t_n + h, \, y_n + h \slope{k_1}) \\ y_{n+1} &= y_n + h\left(\frac{\slope{k_1} + \slope{k_2}}{2}\right) \\[10pt] \textbf{Midpoint Method:} \quad \slope{k_1} &= \slope{f}(t_n, y_n) \\ \slope{k_2} &= \slope{f}\!\left(t_n + \tfrac{1}{2}h, \, y_n + \tfrac{1}{2}h \slope{k_1}\right) \\ y_{n+1} &= y_n + h \cdot \slope{k_2} \\[10pt] \textbf{RK4:} \quad \slope{k_1} &= \underbrace{\slope{f}(t_n, y_n)}_{\slope{\text{Start slope}}} \\ \slope{k_2} &= \underbrace{\slope{f}\!\left(t_n + \tfrac{1}{2}h, \, y_n + \tfrac{1}{2}h \slope{k_1}\right)}_{\slope{\text{First midpoint slope}}} \\ \slope{k_3} &= \underbrace{\slope{f}\!\left(t_n + \tfrac{1}{2}h, \, y_n + \tfrac{1}{2}h \slope{k_2}\right)}_{\slope{\text{Corrected midpoint slope}}} \\ \slope{k_4} &= \underbrace{\slope{f}(t_n + h, \, y_n + h \slope{k_3})}_{\slope{\text{End slope}}} \\ y_{n+1} &= y_n + h\left(\frac{\slope{k_1} + 2\slope{k_2} + 2\slope{k_3} + \slope{k_4}}{6}\right) \end{aligned}

You might also see the step size hh multiplied inside each k\slope{k} from the start, written as k1=hf(tn,yn)\slope{k_1} = h \slope{f}(t_n, y_n), k2=hf(tn+12h,yn+12k1)\slope{k_2} = h \slope{f}(t_n + \tfrac{1}{2}h, y_n + \tfrac{1}{2}\slope{k_1}), and so on, so that each k\slope{k} is the change in yy (Δy\Delta y) rather than a slope. When hh is already inside each k\slope{k}, do not multiply by hh again at the end:

yn+1\displaystyle y_{n+1} =yn+16(k1+2k2+2k3+k4)\displaystyle = y_n + \frac{1}{6}(\slope{k_1} + 2\slope{k_2} + 2\slope{k_3} + \slope{k_4})

Key concepts

Every method steps across an interval of width hh using New y=Old y+(Slope×Step)\text{New } y = \text{Old } y + (\slope{\text{Slope}} \times \text{Step}); the only difference is how each method uses dydt=f(t,y)\slope{\frac{dy}{dt}} = \slope{f}(t, y) to pick a slope for the step. You don't need to run any steps at all if a problem only asks whether Euler's method overestimates or underestimates: differentiate dydt\slope{\frac{dy}{dt}} with respect to tt to check concavity instead (on dydt=2−y\slope{\frac{dy}{dt}} = 2 - y at (0,0)(0, 0), d2ydt2=−dydt=−2<0\curv{\frac{d^2y}{dt^2}} = -\slope{\frac{dy}{dt}} = \curv{-2} < 0, so the curve is concave down and Euler overestimates).

When you do step forward from (tn,yn)(t_n, y_n):

Euler's Method (1st order)

steps straight along the starting slope k1=f(tn,yn)\slope{k_1} = \slope{f}(t_n, y_n) (on dydt=y\slope{\frac{dy}{dt}} = y at (0,1)(0, 1) with h=1h = 1, y1=1+1(1)=2y_1 = 1 + 1(\slope{1}) = 2), and halving hh cuts the error to about 12\err{\frac{1}{2}}.

Heun's Method and the Midpoint Method (2nd order)

both take one trial Euler step from (tn,yn)(t_n, y_n) to find a better slope before returning to (tn,yn)(t_n, y_n) for the real step, and halving hh cuts their error to about (12)2=14\left(\err{\frac{1}{2}}\right)^2 = \err{\frac{1}{4}}. Heun's steps all the way to tn+ht_n + h to average the start and trial-end slopes k1+k22\frac{\slope{k_1} + \slope{k_2}}{2}, while the Midpoint Method steps halfway to tn+12ht_n + \tfrac{1}{2}h and uses the midpoint slope k2=f(tn+12h,yn+12hk1)\slope{k_2} = \slope{f}(t_n + \tfrac{1}{2}h, y_n + \tfrac{1}{2}h \slope{k_1}) for the full step.

RK4 (4th order)

takes every trial step from (tn,yn)(t_n, y_n) to find four slopes (k1\slope{k_1} at tnt_n, k2\slope{k_2} at tn+12ht_n + \tfrac{1}{2}h using k1\slope{k_1}, k3\slope{k_3} still at tn+12ht_n + \tfrac{1}{2}h using k2\slope{k_2}, and k4\slope{k_4} at tn+ht_n + h using k3\slope{k_3}) and steps with the weighted average k1+2k2+2k3+k46\frac{\slope{k_1} + 2\slope{k_2} + 2\slope{k_3} + \slope{k_4}}{6}, shrinking the error to about (12)4=116\left(\err{\frac{1}{2}}\right)^4 = \err{\frac{1}{16}} each time hh is halved. Always check whether the given k\slope{k}-values already have hh multiplied inside them (k1=hf(tn,yn)\slope{k_1} = h \slope{f}(t_n, y_n)) before you multiply by hh a second time.

9

Practice

1

Use Euler's method with a step size of 0.50.5 to estimate y(1.5)\truth{y(1.5)} for dydt=2t−y\slope{\frac{dy}{dt}} = 2t - y, starting at (0,2)(0, 2).

Break the interval from t=0t = 0 to t=1.5t = 1.5 into three steps of 0.50.5, and update both tt and yy in 2t−y2t - y at the start of each step.

Step 1 from t=0t = 0 to 0.50.5 uses slope 2(0)−2=−22(0) - 2 = \slope{-2} to get y=2+(−2)(0.5)=1y = 2 + (\slope{-2})(0.5) = 1. Step 2 from t=0.5t = 0.5 to 1.01.0 uses slope 2(0.5)−1=02(0.5) - 1 = \slope{0} to get y=1+(0)(0.5)=1y = 1 + (\slope{0})(0.5) = 1 (watch out: forgetting to move tt forward to 0.50.5 in 2t−y2t - y gives a slope of −1-1 here and leads to 0.250.25 at the end). Step 3 from t=1.0t = 1.0 to 1.51.5 uses slope 2(1.0)−1=12(1.0) - 1 = \slope{1} to get y=1+(1)(0.5)=1.5y = 1 + (\slope{1})(0.5) = 1.5.
2

For the differential equation dydt=y−3\slope{\frac{dy}{dt}} = y - 3 starting at (0,1)(0, 1), use the second derivative to tell without solving the equation whether Euler's method gives an overestimate or an underestimate near the start.

Find the value of dydt\slope{\frac{dy}{dt}} at (0,1)(0, 1) first, then differentiate both sides of dydt=y−3\slope{\frac{dy}{dt}} = y - 3 with respect to tt.

At (0,1)(0, 1), dydt=1−3=−2\slope{\frac{dy}{dt}} = 1 - 3 = \slope{-2}, and differentiating both sides with respect to tt gives d2ydt2=dydt=−2<0\curv{\frac{d^2y}{dt^2}} = \slope{\frac{dy}{dt}} = \curv{-2} < 0 (watch out: differentiating the right side with respect to yy instead of tt gives d2ydt2=1>0\curv{\frac{d^2y}{dt^2}} = 1 > 0, which gets the concavity backward). Because the second derivative is negative, the solution curve is concave down and its tangent lines sit above the curve, so Euler's method gives an overestimate.
3

Use one step of Heun's Method (Improved Euler) with a step size of 0.50.5 to estimate y(0.5)\truth{y(0.5)} for dydt=t+2y\slope{\frac{dy}{dt}} = t + 2y, starting at (0,1)(0, 1).

Use a trial Euler step of 0.50.5 to estimate yy at t=0.5t = 0.5, find the slope at that trial endpoint, and average it with the starting slope.

The starting slope at (0,1)(0, 1) is 0+2(1)=20 + 2(1) = \slope{2}. A trial Euler step of 0.50.5 gives y=1+2(0.5)=2y = 1 + \slope{2}(0.5) = 2 at t=0.5t = 0.5, where the end slope is 0.5+2(2)=4.50.5 + 2(2) = \slope{4.5} (watch out: leaving t=0t = 0 instead of updating to t=0.5t = 0.5 at the end of the trial step gives an end slope of 44 and a wrong answer of 2.52.5). The average slope is 2+4.52=3.25\frac{\slope{2} + \slope{4.5}}{2} = \slope{3.25}, giving New y=1+3.25(0.5)=2.625\text{New } y = 1 + \slope{3.25}(0.5) = 2.625.
4

Use one step of the Midpoint Method with a step size of 0.40.4 to estimate y(0.4)\truth{y(0.4)} for dydt=t⋅y\slope{\frac{dy}{dt}} = t \cdot y, starting at (0,2)(0, 2).

How far forward in tt is the midpoint of this step, and where does a trial half-step using the starting slope at (0,2)(0, 2) land?

The starting slope at (0,2)(0, 2) is 0⋅2=00 \cdot 2 = \slope{0}, so a trial half-step of 0.20.2 lands at the midpoint (0.2,2)(0.2, 2), where the midpoint slope is 0.2⋅2=0.40.2 \cdot 2 = \slope{0.4}. Taking a full step of 0.40.4 from (0,2)(0, 2) with slope 0.4\slope{0.4} (watch out: multiplying the midpoint slope by the half-step 0.20.2 instead of the full step 0.40.4 gives 2.082.08) gives New y=2+0.4(0.4)=2.16\text{New } y = 2 + \slope{0.4}(0.4) = 2.16.
5

Use one step of Fourth-Order Runge-Kutta (RK4) with a step size of 0.20.2 to estimate y(0.2)\truth{y(0.2)} for dydt=2t+y\slope{\frac{dy}{dt}} = 2t + y, starting at (0,1)(0, 1).

Use the right tt-value for each slope (t=0t = 0 at the start, t=0.1t = 0.1 at both midpoints, and t=0.2t = 0.2 at the end) and start every trial step from y=1y = 1.

Slope 1 at (0,1)(0, 1) is 2(0)+1=12(0) + 1 = \slope{1}. A half-step of 0.10.1 gives y=1+1(0.1)=1.1y = 1 + \slope{1}(0.1) = 1.1 at t=0.1t = 0.1, so Slope 2 is 2(0.1)+1.1=1.32(0.1) + 1.1 = \slope{1.3}. A corrected half-step of 0.10.1 from y=1y = 1 gives y=1+1.3(0.1)=1.13y = 1 + \slope{1.3}(0.1) = 1.13 at t=0.1t = 0.1 (watch out: stepping from the previous midpoint y=1.1y = 1.1 instead of returning to y=1y = 1 gives y=1.23y = 1.23 and leads to 1.27151.2715), so Slope 3 is 2(0.1)+1.13=1.332(0.1) + 1.13 = \slope{1.33}. A full step of 0.20.2 from y=1y = 1 gives y=1+1.33(0.2)=1.266y = 1 + \slope{1.33}(0.2) = 1.266 at t=0.2t = 0.2, so Slope 4 is 2(0.2)+1.266=1.6662(0.2) + 1.266 = \slope{1.666}. The weighted average slope is 1+2(1.3)+2(1.33)+1.6666=1.321\frac{\slope{1} + 2(\slope{1.3}) + 2(\slope{1.33}) + \slope{1.666}}{6} = \slope{1.321}, giving New y=1+1.321(0.2)=1.2642\text{New } y = 1 + \slope{1.321}(0.2) = 1.2642.
6

Suppose the RK4 values are defined with the step size hh multiplied inside each k\slope{k}: k1=hf(tn,yn)\slope{k_1} = h \slope{f}(t_n, y_n), k2=hf(tn+12h,yn+12k1)\slope{k_2} = h \slope{f}(t_n + \tfrac{1}{2}h, y_n + \tfrac{1}{2}\slope{k_1}), and so on. On a step of size h=0.5h = 0.5 starting from yn=3y_n = 3, you find k1=0.6\slope{k_1} = \slope{0.6}, k2=0.8\slope{k_2} = \slope{0.8}, k3=1.0\slope{k_3} = \slope{1.0}, and k4=1.2\slope{k_4} = \slope{1.2}. Find yn+1y_{n+1} and the starting slope f(tn,yn)\slope{f}(t_n, y_n).

Since h=0.5h = 0.5 is already multiplied inside each k\slope{k}, check whether you should multiply by hh again when finding yn+1y_{n+1}, and use k1=hf(tn,yn)\slope{k_1} = h \slope{f}(t_n, y_n) to find the starting slope.

Because hh is already inside each k\slope{k}, do not multiply the weighted average by h=0.5h = 0.5 a second time (which would mistakenly give yn+1=3.45y_{n+1} = 3.45, while forgetting to divide k1\slope{k_1} by hh would give a starting slope of 0.60.6): yn+1=3+0.6+2(0.8)+2(1.0)+1.26=3+5.46=3.9y_{n+1} = 3 + \frac{\slope{0.6} + 2(\slope{0.8}) + 2(\slope{1.0}) + \slope{1.2}}{6} = 3 + \frac{5.4}{6} = 3.9, and the starting slope is f(tn,yn)=k1h=0.60.5=1.2\slope{f}(t_n, y_n) = \frac{\slope{k_1}}{h} = \frac{\slope{0.6}}{0.5} = 1.2.
7

With a step size of h=0.4h = 0.4, Euler's method has an error of 0.32\err{0.32}, the Midpoint Method has an error of 0.08\err{0.08}, and RK4 has an error of 0.0256\err{0.0256}. If you reduce the step size to h=0.1h = 0.1 (cutting the step size in half twice), roughly what error should you expect from each method?

Going from h=0.4h = 0.4 to h=0.2h = 0.2 to h=0.1h = 0.1 halves the step size twice, so multiply by each method's halving factor two times in a row.

Halving the step size twice multiplies Euler's error by roughly (12)2=14\left(\err{\frac{1}{2}}\right)^2 = \err{\frac{1}{4}}, the Midpoint Method's error by roughly (14)2=116\left(\err{\frac{1}{4}}\right)^2 = \err{\frac{1}{16}}, and RK4's error by roughly (116)2=1256\left(\err{\frac{1}{16}}\right)^2 = \err{\frac{1}{256}} (watch out: multiplying by 12\err{\frac{1}{2}}, 14\err{\frac{1}{4}}, and 116\err{\frac{1}{16}} only once gives 0.160.16, 0.020.02, and 0.00160.0016). Multiplying the starting errors by 14\err{\frac{1}{4}}, 116\err{\frac{1}{16}}, and 1256\err{\frac{1}{256}} gives Euler: about 0.080.08; Midpoint: about 0.0050.005; RK4: about 0.00010.0001.