Differential Equations Day 18 -- Runge-Kutta Numerical Method for Systems and Second Order Differential Equations
Overview
In the last lesson we saw how to use Euler's Method to numerically estimate solutions of systemifications of second order differential equations.
In this lesson we'll see the Runge Kutta (4) Method, or RK4 for short, another numerical tool for estimating solutions, but which performs considerably better than Euler's Method.
The RK4 method is actually the fourth member of a class of numerical methods. The first member of the class is actually Euler's Method!
We won't cover the theory of RK4 in these notes. Instead, in the rest of this lesson we'll study what RK4 is, and how RK4 works in a GeoGebra applet. That said, know that the mathematical practice of RK4 is motivated by the same intuitive principle as Euler's Method:
The slope field of a differential equation can be used as a guide to move the estimated solution forward.Euler's Method implements this intuitive principle in a "linear" fashion. Euler's Method observes the slope field once, and uses this one piece of information to head off in that direction linearly for a predetermined step size in the independent variable (that predetermined step size was referred to as
h
in previous lessons).
On the other hand, RK4 systematically observes the slope field four times, and uses these four pieces of information to head off in a direction that represents motion of a fourth order polynomial approximation of the solution function. RK4 also advances for a predetermined step size in the independent variable.
The details of how exactly to do this in a systematic fashion were worked out at the dawn of the 20th century by German mathematicians Carl Runge and Wilhelm Kutta. It's amazing to think of them doing this work prior to the advent of the computer. The RK4 Method
Here are the steps for implementing RK4 on a system of first order differential equations
with initial conditions and .
Note that the only difference between RK4 and Euler's method is in step 5. The RK4 formulas are about 4 times as complex as the Euler's Method formulas.
- Put the system of first order equations in slope field form (also sometimes called normal form)and identify and . What this means in practice is that the equations are as written above with on the left, and everything else involving , and on the right.
- Select a step count, . This is how many steps of an estimated solution you need.
- Select a step size (sometimes also called or )
- The first estimate point is , the initial conditions on and . The first time point is , the initial time (it's often the case that ).
- Apply the RK4 formulas to calculate the next estimate point and the next time point (see below for the RK4 formulas)
- Continue for steps.
Because of the complexities of these RK4 formulas, it's best to think of them as describing a computer program: Job 1 is to compute the next time point, . Job 2 is to compute the next pair of intermediate point and . Keep performing the jobs until you get to the final job which is to compute the next estimate point . Note that the formulas for the next estimate point can be thought of as a weighted average of the intermediate points from previous jobs.
I'm not going to lie: the RK4 formulas are not very fun to work with by hand. The good news is that a computer has no trouble with them, and they can be coded into any spreadsheet software, including GeoGebra. In fact, it takes just about as much time to code RK4 into a spreadsheet as it does to implement one full pass through the RK4 formulas.
This lesson won't go over how precisely to program RK4 into GeoGebra's spreadsheet window, but it's extremely similar to how we saw how to program Euler's Method for first order equations in GeoGebra earlier. The only difference is that there are now quite a few more equations than there were then. If you want to look over the details of what I've done to code RK4 in GeoGebra, click the three dots in the top right of this screen, select "Details" and then follow the instructions to download the raw GeoGebra (.ggb) file.
Implementation in GeoGebra
I've implemented RK4 in GeoGebra' spreadsheet window below.
The bolded column is the column of estimates of the solution of
x_1
. If you are using this tool to estimate solutions of second order differential equations in , then this bold column represents the estimates of the solution function you seek.
To interact with the calculator, adjust f_2
to introduce a different second order differential equation. You can also adjust the initial conditions x_{10}
and x_{20}
, and the step size, h
. If you want more steps, unfortunately there's no way to directly change n
due to a limitation in GeoGebra (or at least my ability to write code in GeoGebra).
Instead, to increase the number of steps, select the entire final row of the spreadsheet, and then use the fill handle to drag it down further. Each new row represents one more step.At the outset, the calculator is set up to use RK4 to compute estimates of the solution of the system
which we've seen before, and is the systemification of the second order differential equation
The calculator is set up to calculate in steps of
h=0.1
out to t=1
. Note the incredible accuracy. The algebraic solution of the second order differential equation (which we saw in the previous lesson) is
Evaluating this algebraic solution at yields approximately 56.025 (rounded to three decimal places). In other words: RK4 is only off by about 3 hundredths after 10 steps! In the previous lesson, Euler's Method was off by almost 3 tenths after just two steps. Incredible!
On the next page is a copy of this calculator for quick and each access.