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), 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) for the differential equation
dtdy=y,starting at (0,1)
using a single step of size 1, and compare our estimate to the exact solution y=et.
Tracing a Curve with Euler's Method
Every Euler step moves forward from the current point along its tangent slope:
New y=Old y+(Slope×Step)
Text solution
Find the slope at the starting pointPlug (0,1) into dtdy=y to get the tangent slope right at the start.
Slope=1
Step forward along the tangent lineMultiply the starting slope by the step size of 1 and add that change to the starting y-value.
New y=Old y1+(Slope1×Step1)=2
So one Euler step estimates y(1)≈2, whereas the true curve y=et reaches e≈2.718.
2
Taking Multiple Euler Steps
A single large step drifts away from a bending curve, and most differential equations depend on both t and y. To follow the curve more closely, we break the interval into a chain of smaller steps and recalculate the slope from both t and y at each new point we reach.
Here we want to estimate y(0.6) in three steps of size 0.2 for
dtdy=t+y,starting at (0,1)
Taking Multiple Euler Steps
Text solution
Step 1: From t=0 to t=0.2Plug the starting point (0,1) into t+y to find the first slope and move y forward by one step of 0.2.
SlopeNew y=0+1=1=1+(1×0.2)=1.2
Step 2: From t=0.2 to t=0.4Plug the new point (0.2,1.2) into t+y to update the slope before taking the second step of 0.2.
So after three steps of size 0.2, Euler's method estimates y(0.6)≈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.
Remember, the sign of the second derivative dt2d2y tells us which way a curve bends compared to its tangent line. Because the differential equation already gives the first derivative dtdy, we can differentiate both sides with respect to t to find dt2d2y at the starting point.
We will check whether Euler's method overestimates or underestimates near the start for
dtdy=2−y,starting at (0,0)
Concavity and Overestimates vs. Underestimates
Text solution
Find the first derivative at the starting pointPlug (0,0) into the differential equation to get the starting slope.
dtdy=2−0=2
Differentiate both sides with respect to t and plug in dtdyTake the t-derivative of 2−y (remembering that y is a function of t, so its derivative is dtdy, not 1) to find the concavity at (0,0).
dt2d2y=−dtdy=−2<0
Because dt2d2y is negative, the solution curve is concave down and bends underneath its tangent line, so Euler's method gives an overestimate.
For dtdy=2−y, solution curves starting above y=2 are concave up so the Euler steps stay underneath them, solution curves starting below y=2 are concave down so the Euler steps stay above them, and right on y=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 1 for
dtdy=y,starting at (0,1)
we can get much closer to y(1)=e≈2.718 if we use a trial Euler step to see how the slope changes before we take our real step from (0,1).
Heun's Method and the Midpoint Method
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) to take the actual step.
Text solution
Heun's Method: Find the end slope and averageTake a trial Euler step of 1 from (0,1) to find the slope at the trial endpoint (1,2), then step from (0,1) using the average of the start and end slopes.
Trial end yAverage SlopeNew y=1+(1×1)=2⟹End slope=2=21+2=1.5=1+(1.5×1)=2.5
Midpoint Method: Find the slope halfway acrossTake a trial Euler half-step of 0.5 from (0,1) to find the slope at the midpoint (0.5,1.5), then take the full step of 1 from (0,1) using that midpoint slope.
Both methods estimate y(1)≈2.5 in a single step of size 1, cutting Euler's error by more than half.
Along the trial Euler step from t=0 to t=1 on dtdy=y, the slope at the start (t=0) is too flat and lands well below y=et, the slope at the trial end (t=1) is steeper, and the slope halfway across (t=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 1 on
dtdy=y,starting at (0,1)
Fourth-Order Runge-Kutta (RK4)
k1,k2,k3,k4
dtdy=y,(0,1),Step=1
RK4:New y=?
Text solution
Slope 1 at the startAt (0,1), read the starting slope from dtdy=y.
Slope 1=1
Slope 2 at the first midpoint guessTake a half-step of 0.5 from y=1 using Slope 1 to estimate the midpoint y and the slope there.
y=1+(1×0.5)=1.5⟹Slope 2=1.5
Slope 3 at the corrected midpoint guessReturn to y=1 and take a half-step of 0.5 using Slope 2 to find the corrected midpoint y and its slope.
y=1+(1.5×0.5)=1.75⟹Slope 3=1.75
Slope 4 at the end guessReturn to y=1 and take a full step of 1 using Slope 3 to estimate the endpoint y and its slope.
y=1+(1.75×1)=2.75⟹Slope 4=2.75
Step from the start with the 1,2,2,1 weighted average slopeGive the two midpoint slopes double weight, divide by the total weight of 6, and take the full step of 1 from y=1.
Average SlopeNew y=6Slope 1+2(Slope 2)+2(Slope 3)+Slope 4=61+2(1.5)+2(1.75)+2.75=610.25≈1.70833=1+(1.70833×1)=2.70833
In a single step of size 1, RK4 estimates y(1)≈2.70833, landing within 0.01 of the exact value e≈2.71828.
6
An RK4 Step with Both t and y
When the differential equation depends on both t and y, we also have to plug in the right t-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) in one RK4 step of size 0.2 for
dtdy=t+y,starting at (0,1)
An RK4 Step with Both t and y
Text solution
Slope 1 at the start (t=0)Plug (0,1) into t+y.
Slope 1=0+1=1.0
Slope 2 at the first midpoint (t=0.1)Move t forward by a half-step to 0.1, take a half-step of 0.1 from y=1 using Slope 1, and plug into t+y.
ymid1Slope 2=1+(1.0×0.1)=1.1=0.1+1.1=1.2
Slope 3 at the corrected midpoint (t=0.1)Keep t at the halfway mark 0.1, take a new half-step of 0.1 from y=1 using Slope 2, and plug into t+y.
ymid2Slope 3=1+(1.2×0.1)=1.12=0.1+1.12=1.22
Slope 4 at the end (t=0.2)Move t forward by a full step to 0.2, take a full step of 0.2 from y=1 using Slope 3, and plug into t+y.
yendSlope 4=1+(1.22×0.2)=1.244=0.2+1.244=1.444
Step from the start with the weighted average slopeAverage the four slopes with weights 1,2,2,1 and take the real step of 0.2 from y=1.
Average SlopeNew y=61.0+2(1.2)+2(1.22)+1.444=1.214=1+(1.214×0.2)=1.2428
So one RK4 step gives y(0.2)≈1.2428, matching the exact value 1.2428055 to five digits. (When finding Slope 3, be careful not to add another half-step to t: Slope 2 and Slope 3 both sit at the exact same halfway time t=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
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):Heun and Midpoint (Second-Order):RK4 (Fourth-Order):Halving the step size multiplies the error by about 211=21Halving the step size multiplies the error by about 221=41Halving the step size multiplies the error by about 241=161
As the equal steps from t=0 to t=1 on dtdy=y are cut in half, Euler's error at t=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 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 dtdy=f(t,y), the step size as h, the current point as (tn,yn), the next point as (tn+1,yn+1) where tn+1=tn+h, and the slopes as k1,k2,k3,k4.
Standard Textbook Notation and Summary
Written in these symbols, every method follows the exact same steps we just did in words:
You might also see the step size h multiplied inside each k from the start, written as k1=hf(tn,yn),k2=hf(tn+21h,yn+21k1), and so on, so that each k is the change in y (Δy) rather than a slope. When h is already inside each k, do not multiply by h again at the end:
yn+1=yn+61(k1+2k2+2k3+k4)
Key concepts
Every method steps across an interval of width h using New y=Old y+(Slope×Step); the only difference is how each method uses dtdy=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 dtdy with respect to t to check concavity instead (on dtdy=2−y at (0,0),dt2d2y=−dtdy=−2<0, so the curve is concave down and Euler overestimates).
When you do step forward from (tn,yn):
Euler's Method (1st order)
steps straight along the starting slope k1=f(tn,yn) (on dtdy=y at (0,1) with h=1,y1=1+1(1)=2), and halving h cuts the error to about 21.
Heun's Method and the Midpoint Method (2nd order)
both take one trial Euler step from (tn,yn) to find a better slope before returning to (tn,yn) for the real step, and halving h cuts their error to about (21)2=41. Heun's steps all the way to tn+h to average the start and trial-end slopes 2k1+k2, while the Midpoint Method steps halfway to tn+21h and uses the midpoint slope k2=f(tn+21h,yn+21hk1) for the full step.
RK4 (4th order)
takes every trial step from (tn,yn) to find four slopes (k1 at tn,k2 at tn+21h using k1,k3 still at tn+21h using k2, and k4 at tn+h using k3) and steps with the weighted average 6k1+2k2+2k3+k4, shrinking the error to about (21)4=161 each time h is halved. Always check whether the given k-values already have h multiplied inside them (k1=hf(tn,yn)) before you multiply by h a second time.
9
Practice
1
Use Euler's method with a step size of 0.5 to estimate y(1.5) for dtdy=2t−y, starting at (0,2).
Watch out: forgetting to move t forward to 0.5 in 2t−y gives a slope of −1 here and leads to 0.25 at the end.
Break the interval from t=0 to t=1.5 into three steps of 0.5, and update both t and y in 2t−y at the start of each step.
Step 1 from t=0 to 0.5 uses slope 2(0)−2=−2 to get y=2+(−2)(0.5)=1. Step 2 from t=0.5 to 1.0 uses slope 2(0.5)−1=0 to get y=1+(0)(0.5)=1 (watch out: forgetting to move t forward to 0.5 in 2t−y gives a slope of −1 here and leads to 0.25 at the end). Step 3 from t=1.0 to 1.5 uses slope 2(1.0)−1=1 to get y=1+(1)(0.5)=1.5.
2
For the differential equation dtdy=y−3 starting at (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 dtdy at (0,1) first, then differentiate both sides of dtdy=y−3 with respect to t.
At (0,1),dtdy=1−3=−2, and differentiating both sides with respect to t gives dt2d2y=dtdy=−2<0 (watch out: differentiating the right side with respect to y instead of t gives dt2d2y=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.5 to estimate y(0.5) for dtdy=t+2y, starting at (0,1).
Watch out: leaving t=0 instead of updating to t=0.5 at the end of the trial step gives an end slope of 4 and a wrong answer of 2.5.
Use a trial Euler step of 0.5 to estimate y at t=0.5, find the slope at that trial endpoint, and average it with the starting slope.
The starting slope at (0,1) is 0+2(1)=2. A trial Euler step of 0.5 gives y=1+2(0.5)=2 at t=0.5, where the end slope is 0.5+2(2)=4.5 (watch out: leaving t=0 instead of updating to t=0.5 at the end of the trial step gives an end slope of 4 and a wrong answer of 2.5). The average slope is 22+4.5=3.25, giving New y=1+3.25(0.5)=2.625.
4
Use one step of the Midpoint Method with a step size of 0.4 to estimate y(0.4) for dtdy=t⋅y, starting at (0,2).
Watch out: multiplying the midpoint slope by the half-step 0.2 instead of the full step 0.4 gives 2.08.
How far forward in t is the midpoint of this step, and where does a trial half-step using the starting slope at (0,2) land?
The starting slope at (0,2) is 0⋅2=0, so a trial half-step of 0.2 lands at the midpoint (0.2,2), where the midpoint slope is 0.2⋅2=0.4. Taking a full step of 0.4 from (0,2) with slope 0.4 (watch out: multiplying the midpoint slope by the half-step 0.2 instead of the full step 0.4 gives 2.08) gives New y=2+0.4(0.4)=2.16.
5
Use one step of Fourth-Order Runge-Kutta (RK4) with a step size of 0.2 to estimate y(0.2) for dtdy=2t+y, starting at (0,1).
Watch out: stepping from the previous midpoint y=1.1 instead of returning to y=1 gives y=1.23 and leads to 1.2715.
Use the right t-value for each slope (t=0 at the start, t=0.1 at both midpoints, and t=0.2 at the end) and start every trial step from y=1.
Slope 1 at (0,1) is 2(0)+1=1. A half-step of 0.1 gives y=1+1(0.1)=1.1 at t=0.1, so Slope 2 is 2(0.1)+1.1=1.3. A corrected half-step of 0.1 from y=1 gives y=1+1.3(0.1)=1.13 at t=0.1 (watch out: stepping from the previous midpoint y=1.1 instead of returning to y=1 gives y=1.23 and leads to 1.2715), so Slope 3 is 2(0.1)+1.13=1.33. A full step of 0.2 from y=1 gives y=1+1.33(0.2)=1.266 at t=0.2, so Slope 4 is 2(0.2)+1.266=1.666. The weighted average slope is 61+2(1.3)+2(1.33)+1.666=1.321, giving New y=1+1.321(0.2)=1.2642.
6
Suppose the RK4 values are defined with the step size h multiplied inside each k:k1=hf(tn,yn),k2=hf(tn+21h,yn+21k1), and so on. On a step of size h=0.5 starting from yn=3, you find k1=0.6,k2=0.8,k3=1.0, and k4=1.2. Find yn+1 and the starting slope f(tn,yn).
Because h is already inside each k, do not multiply the weighted average by h=0.5 a second time (which would mistakenly give yn+1=3.45).
Since h=0.5 is already multiplied inside each k, check whether you should multiply by h again when finding yn+1, and use k1=hf(tn,yn) to find the starting slope.
Because h is already inside each k, do not multiply the weighted average by h=0.5 a second time (which would mistakenly give yn+1=3.45, while forgetting to divide k1 by h would give a starting slope of 0.6): yn+1=3+60.6+2(0.8)+2(1.0)+1.2=3+65.4=3.9, and the starting slope is f(tn,yn)=hk1=0.50.6=1.2.
7
With a step size of h=0.4, Euler's method has an error of 0.32, the Midpoint Method has an error of 0.08, and RK4 has an error of 0.0256. If you reduce the step size to h=0.1 (cutting the step size in half twice), roughly what error should you expect from each method?
Watch out: multiplying by 21,41, and 161 only once gives 0.16,0.02, and 0.0016.
Going from h=0.4 to h=0.2 to h=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 (21)2=41, the Midpoint Method's error by roughly (41)2=161, and RK4's error by roughly (161)2=2561 (watch out: multiplying by 21,41, and 161 only once gives 0.16,0.02, and 0.0016). Multiplying the starting errors by 41,161, and 2561 gives Euler: about 0.08; Midpoint: about 0.005; RK4: about 0.0001.