What is the 4th-order Runge-Kutta method?

The 4th-order Runge-Kutta method, also referred to as RK4, is widely used to solve ordinary differential equations (ODEs) numerically. It gives an approximate solution of an ODE by estimating the value of the next point based on the derivative at a number of intermediary points. RK4 uses four approximations to the slope.

Slope estimation with RK4

Suppose, there is a first-order differential equation such that its solution needs approximation given by:

              with y(t0)=y0

The following slope approximations are used to estimate the slope at some time t₀ (assuming there is an approximation to y(t₀) (that is y*(t₀)).

Explanation

  • The slope at the start of the time step is known as k1; it is also known as k1 in the first and second-order RK methods.

  • A rough estimate of the slope at the midway is represented by k2 if we use slope k1 to step halfway through the time step.

  • An alternative estimation of the slope is k3 at the midpoint if we use slope k2 to step halfway through the time step.

  • The slope, k3, is then used to step through the entire time step (to t0+h), and k4 is an estimate of the slope at the endpoint.

Mathematically, the RK4 method can be expressed as follows

where:

  • t0 represents the current time

  • y represents the current value of the solution

  • h represents the step size or time interval

  • f(t0, y) is the derivative function that defines the ODE

Finally,  a weighted sum of these four slopes is used to obtain the final estimate of y*(t₀+h) is given by:

Here is the weighted average slope estimation

Visualizing RK4 method

Let's consider an example of the differential equation

             with y(0)=3

since t0=0t_0 = 0, at t=t0+ht = t_0 + h or t=ht = h after one time step.

The first slope k1

Using the equation given above, we can use y(0)y(0) to determine the slope, y(0).y'(0). This slope is known as k1.

We estimate the value of the function at the halfway point of the interval, or y(½h), using this slope. The formula for this estimation is y1=y(0)+k1(h2)=2.4y_1 = y(0) + k_1(\frac{h}{2}) = 2.4y1=y(0)+k1*½h=2.4.

Estimating y1 using k1
Estimating y1 using k1

The second slope k2 

Using the equation of y1 given above, we can use it to estimate the slope at the midpoint of the interval, t=h2t = \frac{h}{2}. This slope is called k2k_2.

We estimate the slope at t=h2t = \frac{h}{2} and use it to find another estimate of y(h2)y(\frac{h}{2}). The formula for this estimation is y2=y(0)+k2(h2)=2.52y_2 = y(0) + k_2\cdot(\frac{h}{2}) = 2.52

Estimating y2 using k2
Estimating y2 using k2

The third slope k3 

Using the equation of y2 given above, we can use it to estimate the slope at the midpoint of the interval, t=½h. This slope is called k3.k_3.

We estimate the value of the function at the end of the interval, or y(h), using this slope at t=h2t = \frac{h}{2}. The formula for this estimation is:

Estimating y3 using k3
Estimating y3 using k3

The fourth slope k4

Using the equation of y3y_3 given above, we can use it to estimate the slope at the end of the interval, t=ht = h. This slope is called k4k_4.

Creating the final estimate for y(h),y(h), or final slope, we weigh the sum of all the slope estimates. We weigh the midpoint slope estimates twice as heavily as endpoint estimations. The weighted estimate becomes:

using m, to generate the final estimate

Estimating y(h) using m
Estimating y(h) using m

The final estimate’s value for this example is y(h)=2.0112.y^*(h) = 2.0112. It is really close to the exact solution, which is:

Note: Exact solution will not always be given as it is in this case.

Using this estimate as a starting point, we can repeat this procedure to eventually come up with a solution.

Conclusion

All in all, the RK4 method is an iterative process, and compared to lower-order RK approaches, it is relatively accurate and straightforward, which makes it popular. However, to accurately record quick changes in the solution, smaller step sizes could be necessary.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved