Euler uses the rate at the start of a step. What if the rate changes substantially before the step ends? We can predict an endpoint, estimate the rate there, and use the average of the two rates.
NoteReading and code
Read Module 6.3, “Runge-Kutta 2 Method,” in Introduction to Computational Science. This module calls the particular RK2 method here Euler’s predictor-corrector (EPC). Download the complete R script after coding along.
Start where Euler left off
We will reuse Module 6.2’s model: \(P(0)=100\) and \(dP/dt=0.1P\) people per year. Take one deliberately large, eight-year step so the difference between methods is easy to see.
At the start, the rate is \(0.1(100)=10\) people/year. Euler predicts
\[
P_{\text{Euler}}(8)=100+10(8)=180.
\]
This is a prediction, not yet our RK2 answer. The population is growing, so its rate at the end of the step is probably greater than 10. Before continuing, predict: should an improved estimate be above or below 180?
Correct the prediction
We do not know the actual population at year 8, so use Euler’s prediction of 180 to estimate the ending rate: \(0.1(180)=18\) people/year. Average the starting and predicted ending rates:
euler_prediction helps us find the second rate. We do not replace old_population with it. The corrected step still starts from the old population of 100.
The green segment uses an average rate, so it reaches a better endpoint here. It is still a straight-line approximation over the step; it is not the exact curved path.
Repeat the correction at every step
For several steps, each corrected value becomes the starting value of the next step. Use two-year steps from year 0 to year 8. First, calculate the next value by hand: what are the starting rate, Euler prediction, predicted ending rate, and corrected population after two years?
The first corrected value should be 122: start rate 10, predicted population 120, predicted end rate 12, average rate 11, and change \(11(2)=22\). Compare that hand calculation with the second row.
With two-year steps, Euler estimates 207.36 and RK2 estimates about 221.53 at year 8; the exact value is about 222.55.
Does the time step still matter?
Predict what will happen to both methods if we shorten the step. Then change delta_t above to 8, 1, and 0.5 and rerun the two loops, keeping the model and end time fixed.
Step, years
Euler at year 8
RK2 at year 8
Exact at year 8
8
180.00
212.00
222.55
2
207.36
221.53
222.55
1
214.36
222.28
222.55
0.5
218.29
222.48
222.55
For this smooth model and these step sizes, RK2 has less numerical error than Euler at the same step size. It uses two rate evaluations per step instead of one. A better numerical answer does not prove that \(0.1P\) accurately describes a real population.
TipCheck your understanding
In the loop, which value is the predictor, and which is the corrected population?
Why must predicted_end_rate use euler_prediction, not old_population?
If the rate also depended explicitly on time, at which time would you evaluate the second rate?
Could a smaller Euler step outperform a much larger RK2 step? What comparison would you run?
Try a different rate from the book
Module 6.3’s Quick Review Question 1 returns to \(dP/dt=10+P/5\), starting at \(P=500\), with a step of \(0.1\). Predict the corrected value before running this code. The Euler prediction is 511; what ending rate does that predict?
The corrected result is 511.11. The sequence is the same for any rate formula: start rate → Euler prediction → predicted end rate → average rate → corrected update. This particular predictor-corrector scheme is one version of RK2, not the only possible RK2 formula.