The Ultimate Guide to the Runge-Kutta Method for Beginners

The Ultimate Guide to the Runge-Kutta Method for Beginners

The Runge-Kutta method for beginners approximates solutions to differential equations when finding an exact solution is difficult or impossible. Instead of relying on a single slope at each step, the Runge-Kutta method uses several carefully chosen slopes to produce a more accurate approximation of the solution.

In this guide, we will develop an understanding of the Runge-Kutta method from the ground up. We will begin by examining how the method works and why using multiple slopes yields a more accurate approximation. We will then explore when the method succeeds, when it can fail, and how to calculate error. Finally, we will work through several examples step by step so that you can see exactly how the Runge-Kutta method is used to approximate solutions to differential equations.

By the end of this guide, you should not only know how to apply the Runge-Kutta method, but also understand why it works, how accurate it can be, and when it is an appropriate method to use.

What Is the Runge-Kutta Method?

The Runge-Kutta method is a numerical method for approximating solutions to ordinary differential equations. The basic idea is similar to Euler’s method. We begin with a known point on the solution curve and use information about the derivative to estimate where the solution will be at the next point. The major difference is that the Runge-Kutta method evaluates the derivative at multiple points within each step. These evaluations are then combined to produce a more accurate approximation. For a review of Euler’s method, please refer to the article How to Solve Differential Equations Using Euler’s Method: A Complete Tutorial.

The Initial Value Problem

Suppose we want to solve an initial value problem of the form

$$\frac{dy}{dx} = f(x,y),$$

with the initial condition

$$y(x_0) = y_0.$$

The differential equation tells us how \( y \) changes as \( x \) changes, while the initial condition gives us a known point on the solution curve.

Our goal is to use this information to approximate the value of \( y \) at later values of \( x \). This will require knowledge of tangent lines as well as derivatives. For a review of these topics, please refer to the articles Secant and Tangent Lines in Calculus: A Complete Beginner’s Guide and How to Differentiate a Function Step by Step: A Beginner’s Guide.

The Basic Idea

To understand the Runge-Kutta method, think about what a slope tells us.

For a differential equation

$$\frac{dy}{dx} = f(x,y),$$

the value of \( f(x,y) \) gives the slope of the solution at a particular point.

If we know the slope at \( ( x_n, y_n) \), we have information about the direction in which the solution is moving. Euler’s method uses this single slope to estimate the next value:

$$y_{n + 1} = y_n + h f(x_n, y_n),$$

where \( h \) is the step size.

The problem is that the slope of the solution may change as we move across the interval from \( x_n \) to \( x_{n+1} \). Using only the slope at the beginning of the interval may therefore give us a poor approximation.

The Runge-Kutta method addresses this problem by looking at several slopes.

Using the Runge-Kutta method, we calculate four different slope estimates. The first uses the beginning of the interval. The second and third examine the midpoint of the interval, and the fourth examines the end of the interval.

We then combine these values using a weighted average.

The Runge-Kutta Method

For the initial value problem

$$\frac{dy}{dx} = f(x,y),$$

with

$$y(x_0) = y_0,$$

the Runge-Kutta method calculates four quantities at each step.

The first slope is

$$k_1 = f(x_n,y_n).$$

This is the slope at the beginning of the interval.

The second slope is

$$k_2 = f(x_n+\frac{h}{2}, y_n + \frac{hk_1}{2}).$$

This estimates the slope at the midpoint of the interval using \( k_1 \).

The third slope is

$$k_3 = f(x_n + \frac{h}{2}, y_n + \frac{hk_2}{2}).$$

This provides another estimate of the slope at the midpoint, this time using \( k_2 \).

Finally, the fourth slope is

$$k_4 = f(x_n + h, y_n + hk_3).$$

This estimates the slope at the end of the interval.

Once these four slopes have been calculated, we use them to determine the next approximation:

$$y_{n + 1} = y_n + \frac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4).$$

Notice that \( k_2 \) and \( k_3 \) receive twice the weight of \( k_1 \) and \( k_4 \). The method is therefore not simply taking an ordinary average of the four slopes. Instead, it uses a carefully chosen weighted average that produces a much more accurate approximation.

This is similar to the weighted averages used for the Trapezoid Rule and Simpson’s Rule in numerical integration. If you’d like to explore these methods, please refer to the articles Trapezoid Rule Examples with Solutions for Beginners and Simpson’s Rule Explained step by step: Everything You Need to Know.

From One Step to the Next

Once we calculate \( y_{n + 1} \), that approximation becomes the starting point for the next step.

For example, suppose we begin with

$$(x_0, y_0).$$

After one step, we obtain

$$(x_1, y_1).$$

We can then use \( (x_1, y_1) \) to calculate

$$(x_2, y_2),$$

and continue this process for as many steps as necessary.

The accuracy of these approximations depends on several factors, including the behavior of the differential equation and the step size \( h \). Later in this guide, we will examine why the Runge-Kutta method can be highly accurate, how to measure its error, and situations where it may not perform as well.

Why Does the Runge-Kutta Method Work?

The Runge-Kutta method works because it looks at more than just the slope at one point. Instead, it gathers information about the solution at several points within each step and combines that information in a carefully chosen way. To understand why this works, it helps to first look at the limitation of using only one slope.

The Problem with Using One Slope

Recall that Euler’s method uses the slope at the beginning of each interval to estimate the next value of the solution.

For the differential equation

$$\frac{dy}{dx} = f(x,y),$$

Euler’s method uses

$$y_{n + 1} = y_n + hf(x_n, y_n).$$

Geometrically, this amounts to replacing a portion of the solution curve with a straight line.

The problem is that the slope of the solution may change as we move from \( x_n \) to \( x_{n + 1} \). The slope at the beginning of the interval may therefore not represent the slope throughout the entire interval.

This is one reason why Euler’s method can accumulate significant error over many steps.

The Runge-Kutta method takes a different approach. Instead of assuming that one slope adequately describes the entire interval, it evaluates the differential equation at several carefully selected points.

It then combines these values using a weighted average.

Using Multiple Slopes

The Runge-Kutta method uses four slope estimates:

$$k_1, k_2, k_3, k_4.$$

These slopes describe the solution throughout the interval.

The first slope is calculated at the beginning:

$$k_1 = f(x_n, y_n).$$

The second slope is calculated halfway through the interval:

$$k_2 = f(x_n + \frac{h}{2}, y_n + \frac{hk_1}{2}).$$

The third slope also examines the midpoint, but it uses the improved estimate provided by \( k_2 \):

$$k_3 = f(x_n + \frac{h}{2}, y_n + \frac{hk_2}{2}).$$

Finally, the fourth slope examines the end of the interval:

$$k_4 = f(x_n + h, y_n + hk_3).$$

These four evaluations give considerably more information about the solution than Euler’s method obtains from a single slope.

We can think of the four slopes as taking four different measurements of how the solution is changing.

The important point is that the Runge-Kutta method does not simply calculate four slopes and treat them equally. It combines them using a weighted average:

$$\frac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4).$$

This weighted average estimates the slope across the interval.

The next value of the solution is then calculated using

$$y_{n + 1} = y_n + \frac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4).$$

By considering the behavior of the solution at the beginning, middle, and end of the interval, the Runge-Kutta method accounts for changes in the slope that Euler’s method misses.

Why Are the Slopes Weighted?

At first, the weights may seem arbitrary. Why do \( k_2 \) and \( k_3 \) receive twice the weight of \( k_1 \) and \( k_4 \)?

The answer comes from how the method is constructed to approximate the behavior of the solution.

The four slopes do not equally represent the entire interval. The midpoint evaluations provide particularly important information about what happens between the two endpoints. As a result, the two midpoint slopes receive greater weight.

The weighted average is

$$\frac{k_1 + 2k_2 + 2k_3 + k_4}{6}.$$

The weights add up to six:

$$1 + 2 + 2 + 1 = 6.$$

Therefore, dividing by six produces a weighted average of the four slope estimates.

The result is a much better estimate of the average slope over the interval.

These weights are not chosen simply because they look reasonable. They are derived so the resulting numerical method agrees with the true solution to a high degree of accuracy.

This is similar in spirit to numerical integration techniques that use weighted function values to obtain more accurate approximations. Rather than relying on a single value, we use information from several locations and assign each value an appropriate weight.

Why Is the Runge-Kutta Method So Accurate?

The Runge-Kutta method is a fourth-order numerical method. This means that its global truncation error is generally proportional to

$$O(h^4),$$

where \( h \) is the step size.

This is a significant improvement over Euler’s method, whose global truncation error is generally

$$O(h).$$

The difference in order helps explain why the Runge-Kutta method can produce highly accurate results without requiring an extremely small step size.

Geometric Interpretation

It can also be helpful to think about the Runge-Kutta method geometrically.

Suppose the solution curve between \( x_n \) and \( x_{n + 1} \) is curved rather than straight. A single tangent line at \( x_n \) cannot completely describe that curve.

The Runge-Kutta method gathers information from several locations along the interval. It examines the slope at the beginning, considers what happens near the middle, and then examines the slope at the end.

The resulting weighted slope better represents the overall behavior of the solution during the step.

Of course, using multiple slope evaluations does not guarantee that the Runge-Kutta method will always produce an accurate answer. The differential equation, the step size, and the behavior of the solution all affect the method’s performance.

In the next sections of this guide, we will examine when the Runge-Kutta method performs particularly well, when it can struggle, and how we can measure the error in its approximations.

When Does The Runge-Kutta Method Succeed?

The Runge-Kutta method can produce highly accurate approximations to solutions of differential equations, but its success depends on the problem being solved. The method works particularly well when the solution is sufficiently smooth, and the step size is chosen appropriately.

Smooth Functions

Consider an initial value problem of the form

$$\frac{dy}{dx} = f(x,y),$$

with

$$y(x_0) = y_0.$$

When \( f(x,y) \) changes smoothly, the slope of the solution generally changes predictably. The four slope estimates used by the Runge-Kutta method can then capture the behavior of the solution effectively.

For a refresher on smooth functions, please refer to the articles Newton’s Method Explained for Beginners and The Ultimate Resource for Understanding the Secant Method.

Choosing an Appropriate Step Size

Recall that the Runge-Kutta method has global truncation error of order

$$O(h^4).$$

This means that reducing \( h \) generally improves the accuracy of the approximation.

However, smaller is not always better computationally. A smaller step size requires more steps to reach the same final value of \( x \). Each step requires four evaluations of the differential equation, so reducing the step size increases the amount of computation.

The goal is therefore not simply to choose the smallest possible step size. Instead, we want a step size that provides the desired accuracy without unnecessary computation.

When Does The Runge-Kutta Method Fail?

The Runge-Kutta method is highly accurate for many ordinary differential equations, but it does not work equally well for every problem. A numerical method can produce a result even when it poorly approximates the true solution. For this reason, it is important to understand the situations in which the Runge-Kutta method can struggle.

The most common problems involve choosing a step size that is too large, discontinuities, and stiff differential equations.

When the Step Size Is Too Large

Recall that the Runge-Kutta method estimates the solution over an interval of length \( h \) by evaluating the differential equation at several points within the interval. The method works best when these evaluations provide a reasonable estimate of how the solution behaves between \( x_n \) and \( x_{n + 1} \).

If \( h \) is very large, the solution may change substantially between these points. The four slope estimates may then fail to capture important features of the solution.

Discontinuities

The Runge-Kutta method can also encounter difficulties when the function defining the differential equation is discontinuous.

If \( f(x,y) \) is discontinuous within a step, the slope may change abruptly.
For example, consider a function with a discontinuity at \( x = a \).

If a step crosses \( x = a \), the four slope evaluations may occur on different sides of the discontinuity.

In this situation, the weighted average of the slopes may not provide a meaningful approximation to the behavior of the solution across the entire interval.

For a review of continuous functions, please refer to the articles A Complete Introduction to the Bisection Method with Worked Examples and Solutions and How to Solve Equations Using Fixed Point Iteration: A Step-by-Step Guide.

Stiff Differential Equations

Stiffness occurs when a solution grows or decays rapidly.

The difficulty is that the Runge-Kutta method may require an extremely small step size to remain stable when solving a stiff differential equation.

Error Computation

Every numerical method introduces some amount of error because it produces an approximation rather than an exact solution. The Runge-Kutta method is designed to make this error very small, but understanding how to measure and interpret it is an important part of using the method correctly.

When we use the Runge-Kutta method to approximate a solution, we should not simply ask whether the answer is correct or incorrect. Instead, we should ask how close the approximation is to the true solution and how that accuracy changes when we change the step size.

Absolute Error

Suppose the exact solution to an initial value problem is \( y(x) \), but the Runge-Kutta method produces the approximation \( y_n \) at \( x = x_n \).

The absolute error at that point is the difference between the exact value and the numerical approximation:

$$\epsilon_n = |y(x_n) – y_n|.$$

A smaller absolute error means that the numerical approximation is closer to the exact solution.

Relative and Percent Error

Absolute error is useful, but it does not always tell us how large the error is relative to the size of the exact solution.

For this reason, we often calculate the relative error:

$$\epsilon_n = |\frac{y(x_n) – y_n}{y(x_n)}|.$$

Multiplying by \( 100\% \) gives the percent error:

$$\epsilon_n = |\frac{y(x_n) – y_n}{y(x_n)}| \times 100\%.$$

Local Truncation Error

The local truncation error measures the error introduced by the numerical method in a single step, assuming the calculation starts from the exact value at the beginning of that step.

For the Runge-Kutta method, the local truncation error is generally of order

$$O(h^5).$$

This means the error introduced in one step decreases rapidly as the step size \( h \) decreases.

However, it is important to distinguish local truncation error from the total error after many steps. The errors introduced at individual steps can accumulate as the numerical solution progresses.

Global Truncation Error

The global truncation error measures the accumulated error in the numerical solution after taking multiple steps.

For the Runge-Kutta method, the global truncation error is generally of order

$$O(h^4).$$

This is what we mean when we say that the Runge-Kutta method is a fourth-order method.

Step-by-Step Procedure

After understanding how the Runge-Kutta method works, why it is accurate, when it succeeds, and how to measure its error, the next step is learning how to actually use it. The Runge-Kutta method follows a consistent sequence of calculations. Once you understand this sequence, you can apply it to a wide variety of initial value problems.

Step 1: Write the Differential Equation in Standard Form

The first step is to write the differential equation in the standard form

$$\frac{dy}{dx} = f(x,y)$$

along with the initial condition

$$y(x_0) = y_0.$$

You should also identify the interval over which you want to approximate the solution and choose a step size \( h \).

Step 2: Identify the Function \( f(x,y) \)

Next, identify the function on the right-hand side of the differential equation.

This function is used in every slope calculation.

Step 3: Calculate \( k_1 \)

The first slope is calculated using the starting point of the interval:

$$k_1 = f(x_n, y_n).$$

Step 4: Calculate \( k_2 \)

The second slope estimates the slope at the midpoint of the interval using \( k_1 \):

$$k_2 = f(x_n + \frac{h}{2}, y_n + \frac{hk_1}{2}).$$

Step 5: Calculate \( k_3 \)

The third slope also evaluates the midpoint, but it uses \( k_2 \) instead of \( k_1 \):

$$k_3 = f(x_n + \frac{h}{2}, y_n + \frac{hk_2}{2}).$$

Step 6: Calculate \( k_4 \)

The fourth slope evaluates the function at the end of the interval:

$$k_4 = f(x_n + h, y_n + hk_3).$$

Step 7: Combine the Four Slopes

The four slopes are combined using the weighted average:

$$y_{n + 1} = y_n + \frac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4).$$

Step 8: Move to the Next Step

If we want to continue the calculation, the newly calculated value becomes the starting value for the next step.

The process continues until we reach the desired value of \( x \).

With this procedure in place, we can now work through complete examples and see how the Runge-Kutta method performs in practice.

Worked Out Examples

Now that we have seen how the Runge-Kutta method works and how to apply the algorithm step by step, it is time to put everything together.

The first example uses techniques covered in the article A Step-by-Step Tutorial on Linear Differential Equations with Examples and Solutions.

Example 1: Use the Runge-Kutta Method to approximate the solution of \( \frac{dy}{dx} = x + y \), subject to the initial condition \( y(0) = 1 \) at \( x = 0.5 \). Use a step size of \( h = 0.1 \) and find the absolute error at \( x = 0.5 \).

Solution: Consider the function

$$f(x, y) = x + y.$$

Calculating the first slope, we obtain

$$k_1 = f(0, 1).$$

This is

$$k_1 = 0 + 1.$$

Adding we find

$$k_1 = 1.$$

Calculating the second slope, we obtain

$$k_2 = f(0 + \frac{0.1}{2}, 1 + \frac{(0.1)(1)}{2}).$$

Multiplying gives

$$k_2 = f(0 + \frac{0.1}{2}, 1 + \frac{0.1}{2}).$$

Dividing we get

$$k_2 = f(0 + 0.05, 1 + 0.05).$$

Adding we find

$$k_2 = f(0.05, 1.05).$$

This is

$$k_2 = 0.05 + 1.05.$$

Adding we find

$$k_2 = 1.1.$$

Calculating the third slope, we obtain

$$k_3 = f(0 + \frac{0.1}{2}, 1 + \frac{(0.1)(1.1)}{2}).$$

Multiplying gives

$$k_3 = f(0 + \frac{0.1}{2}, 1 + \frac{0.11}{2}).$$

Dividing we get

$$k_3 = f(0 + 0.05, 1 + 0.055).$$

Adding we find

$$k_3 = f(0.05, 1.055).$$

This is

$$k_3 = 0.05 + 1.055.$$

Adding we find

$$k_3 = 1.105.$$

Calculating the fourth slope, we obtain

$$k_4 = f(0 + 0.1, 1 + (0.1)(1.105).$$

Multiplying gives

$$k_4 = f(0 + 0.1, 1 + 0.1105).$$

Adding we find

$$k_4 = f(0.1, 1.1105).$$

This is

$$k_4 = 0.1 + 1.1105.$$

Adding we find

$$k_4 = 1.2105.$$

The next \( x \)-value is

$$x_1 = 0 + 0.1.$$

Adding gives

$$x_1 = 0.1.$$

The next \( y \)-value is

$$y_1 = 1 + \frac{0.1}{6}(1 + 2(1.1) + 2(1.105) + 1.2105).$$

Multiplying we get

$$y_1 = 1 + \frac{0.1}{6}(1 + 2.2 + 2.21 + 1.2105).$$

Adding gives

$$y_1 = 1 + \frac{0.1}{6}(6.6205).$$

Dividing this is

$$y_1 = 1 + 0.016666(6.6205).$$

Multiplying we obtain

$$y_1 = 1 + 0.110341.$$

Adding we find

$$y_1 = 1.110341.$$

Continuing, we calculate the first slope to obtain

$$k_1 = f(0.1, 1.110341).$$

This is

$$k_1 = 0.1 + 1.110341.$$

Adding we find

$$k_1 = 1.210341.$$

Calculating the second slope, we obtain

$$k_2 = f(0.1 + \frac{0.1}{2}, 1.110341 + \frac{(0.1)(1.210341)}{2}).$$

Multiplying gives

$$k_2 = f(0.1 + \frac{0.1}{2}, 1.110341 + \frac{0.121034}{2}).$$

Dividing we get

$$k_2 = f(0.1 + 0.05, 1.110341 + 0.060517.$$

Adding we find

$$k_2 = f(0.15, 1.170858).$$

This is

$$k_2 = 0.15 + 1.170858.$$

Adding we find

$$k_2 = 1.320858.$$

Calculating the third slope, we obtain

$$k_3 = f(0.1 + \frac{0.1}{2}, 1.1103417 + \frac{(0.1)(1.320858)}{2}).$$

Multiplying gives

$$k_3 = f(0.1 + \frac{0.1}{2}, 1.1103417 + \frac{0.132085}{2}).$$

Dividing we get

$$k_3 = f(0.1 + 0.05, 1.1103417 + 0.066042.$$

Adding we find

$$k_3 = f(0.15, 1.176384).$$

This is

$$k_3 = 0.15 + 1.176384.$$

Adding we find

$$k_3 = 1.326384.$$

Calculating the fourth slope, we obtain

$$k_4 = f(0.1 + 0.1, 1.110341 + (0.1)(1.326384).$$

Multiplying gives

$$k_4 = f(0.1 + 0.1, 1.110341 + 0.132638.$$

Adding we find

$$k_4 = f(0.2, 1.1105).$$

This is

$$k_4 = 0.2 + 1.242980.$$

Adding we find

$$k_4 = 1.442980.$$

The next \( x \)-value is

$$x_2 = 0.1 + 0.1.$$

Adding gives

$$x_2 = 0.2.$$

The next \( y \)-value is

$$y_2 = 1.110341 + \frac{0.1}{6}(1.210341 + 2(1.320858) + 2(1.326384) + 1.442980).$$

Multiplying we get

$$y_2 = 1.110341 + \frac{0.1}{6}(1.210341 + 2.641717 + 2.652769 + 1.442980).$$

Adding gives

$$y_2 = 1.110341 + \frac{0.1}{6}(7.947808).$$

Dividing this is

$$y_2 = 1.110341 + 0.016666(7.947808).$$

Multiplying we obtain

$$y_2 = 1.110341 + 0.132463.$$

Adding we find

$$y_2 = 1.242805.$$

Continuing, we calculate the first slope to obtain

$$k_1 = f(0.2, 1.242805).$$

This is

$$k_1 = 0.2 + 1.242805.$$

Adding we find

$$k_1 = 1.442805.$$

Calculating the second slope, we obtain

$$k_2 = f(0.2 + \frac{0.1}{2}, 1.242805 + \frac{(0.1)(1.442805)}{2}).$$

Multiplying gives

$$k_2 = f(0.2 + \frac{0.1}{2}, 1.242805 + \frac{0.144280}{2}).$$

Dividing we get

$$k_2 = f(0.2 + 0.05, 1.242805 + 0.072140).$$

Adding we find

$$k_2 = f(0.25, 1.314945).$$

This is

$$k_2 = 0.25 + 1.314945.$$

Adding we find

$$k_2 = 1.564945.$$

Calculating the third slope, we obtain

$$k_3 = f(0.2 + \frac{0.1}{2}, 1.242805 + \frac{(0.1)(1.564945)}{2}).$$

Multiplying gives

$$k_3 = f(0.2 + \frac{0.1}{2}, 1.242805 + \frac{0.156494}{2}).$$

Dividing we get

$$k_3 = f(0.2 + 0.05, 1.242805 + 0.078247).$$

Adding we find

$$k_3 = f(0.25, 1.321052).$$

This is

$$k_3 = 0.25 + 1.321052.$$

Adding we find

$$k_3 = 1.571052.$$

Calculating the fourth slope, we obtain

$$k_4 = f(0.2 + 0.1, 1.242805 + (0.1)(1.571052).$$

Multiplying gives

$$k_4 = f(0.2 + 0.1, 1.110341 + 0.157105.$$

Adding we find

$$k_4 = f(0.3, 1.399910).$$

This is

$$k_4 = 0.3 + 1.399910.$$

Adding we find

$$k_4 = 1.699910.$$

The next \( x \)-value is

$$x_3 = 0.2 + 0.1.$$

Adding gives

$$x_3 = 0.3.$$

The next \( y \)-value is

$$y_3 = 1.242805 + \frac{0.1}{6}(1.442805 + 2(1.564945) + 2(1.571052) + 1.699910).$$

Multiplying we get

$$y_3 = 1.242805 + \frac{0.1}{6}(1.442805 + 3.129890 + 3.142104 + 1.699910).$$

Adding gives

$$y_3 = 1.242805 + \frac{0.1}{6}(9.414711).$$

Dividing this is

$$y_3 = 1.242805 + 0.016666(9.414711).$$

Multiplying we obtain

$$y_3 = 1.242805 + 0.156911.$$

Adding we find

$$y_3 = 1.399717.$$

Continuing, we calculate the first slope to obtain

$$k_1 = f(0.3, 1.399717).$$

This is

$$k_1 = 0.3 + 1.399717.$$

Adding we find

$$k_1 = 1.699717.$$

Calculating the second slope, we obtain

$$k_2 = f(0.3 + \frac{0.1}{2}, 1.399717 + \frac{(0.1)(1.699717)}{2}).$$

Multiplying gives

$$k_2 = f(0.3 + \frac{0.1}{2}, 1.399717 + \frac{0.169971}{2}).$$

Dividing we get

$$k_2 = f(0.3 + 0.05, 1.399717 + 0.084985).$$

Adding we find

$$k_2 = f(0.35, 1.484702).$$

This is

$$k_2 = 0.35 + 1.484702.$$

Adding we find

$$k_2 = 1.834702.$$

Calculating the third slope, we obtain

$$k_3 = f(0.3 + \frac{0.1}{2}, 1.399717 + \frac{(0.1)(1.834702)}{2}).$$

Multiplying gives

$$k_3 = f(0.3 + \frac{0.1}{2}, 1.399717 + \frac{0.183470}{2}).$$

Dividing we get

$$k_3 = f(0.3 + 0.05, 1.399717 + 0.091735).$$

Adding we find

$$k_3 = f(0.35, 1.491452).$$

This is

$$k_3 = 0.35 + 1.491452.$$

Adding we find

$$k_3 = 1.841452.$$

Calculating the fourth slope, we obtain

$$k_4 = f(0.3 + 0.1, 1.399717 + (0.1)(1.841452).$$

Multiplying gives

$$k_4 = f(0.3 + 0.1, 1.399717 + 0.184145).$$

Adding we find

$$k_4 = f(0.4, 1.583862).$$

This is

$$k_4 = 0.4 + 1.583862.$$

Adding we find

$$k_4 = 1.983862.$$

The next \( x \)-value is

$$x_4 = 0.3 + 0.1.$$

Adding gives

$$x_4 = 0.4.$$

The next \( y \)-value is

$$y_4 = 1.399717 + \frac{0.1}{6}(1.699717 + 2(1.834702) + 2(1.841452) + 1.983862).$$

Multiplying we get

$$y_4 = 1.399717 + \frac{0.1}{6}(1.699717 + 3.669405 + 3.682904 + 1.983862).$$

Adding gives

$$y_4 = 1.399717 + \frac{0.1}{6}(11.035889).$$

Dividing this is

$$y_4 = 1.399717 + 0.016666(11.035889).$$

Multiplying we obtain

$$y_4 = 1.399717 + 0.183931.$$

Adding we find

$$y_4 = 1.583648.$$

Continuing, we calculate the first slope to obtain

$$k_1 = f(0.4, 1.583648).$$

This is

$$k_1 = 0.4 + 1.583648.$$

Adding we find

$$k_1 = 1.983648.$$

Calculating the second slope, we obtain

$$k_2 = f(0.4 + \frac{0.1}{2}, 1.583648 + \frac{(0.1)(1.983648)}{2}).$$

Multiplying gives

$$k_2 = f(0.4 + \frac{0.1}{2}, 1.583648 + \frac{0.198364}{2}).$$

Dividing we get

$$k_2 = f(0.4 + 0.05, 1.583648 + 0.099182).$$

Adding we find

$$k_2 = f(0.45, 1.682830).$$

This is

$$k_2 = 0.45 + 1.682830.$$

Adding we find

$$k_2 = 2.132830.$$

Calculating the third slope, we obtain

$$k_3 = f(0.4 + \frac{0.1}{2}, 1.583648 + \frac{(0.1)(2.132830)}{2}).$$

Multiplying gives

$$k_3 = f(0.4 + \frac{0.1}{2}, 1.583648 + \frac{0.213283}{2}).$$

Dividing we get

$$k_3 = f(0.4 + 0.05, 1.583648 + 0.106641).$$

Adding we find

$$k_3 = f(0.45, 1.690290).$$

This is

$$k_3 = 0.45 + 1.690290.$$

Adding we find

$$k_3 = 2.140290.$$

Calculating the fourth slope, we obtain

$$k_4 = f(0.4 + 0.1, 1.583648 + (0.1)(2.140290).$$

Multiplying gives

$$k_4 = f(0.4 + 0.1, 1.583648 + 0.214029).$$

Adding we find

$$k_4 = f(0.5, 1.797677).$$

This is

$$k_4 = 0.5 + 1.797677.$$

Adding we find

$$k_4 = 2.297677.$$

The next \( x \)-value is

$$x_5 = 0.4 + 0.1.$$

Adding gives

$$x_5 = 0.5.$$

The next \( y \)-value is

$$y_5 = 1.583648 + \frac{0.1}{6}(1.983648 + 2(2.132830) + 2(2.140290) + 2.297677).$$

Multiplying we get

$$y_5 = 1.583648 + \frac{0.1}{6}(1.983648 + 4.265661 + 4.280580 + 2.297677).$$

Adding gives

$$y_5 = 1.583648 + \frac{0.1}{6}(12.827567).$$

Dividing this is

$$y_5 = 1.583648 + 0.016666(12.827567).$$

Multiplying we obtain

$$y_5 = 1.583648 + 0.213792.$$

Adding we find

$$y_5 = 1.797441.$$

Subtracting y from both sides of the differential equation gives

$$\frac{dy}{dx} – y = x.$$

This is a linear differential equation in standard form. The integrating factor is

$$\mu(x) = e^{-\int dx}.$$

Integrating we get

$$\mu(x) = e^{-x}.$$

Multiplying each term of the equation by the integrating factor, we obtain

$$e^{-x}\frac{dy}{dx} – e^{-x}y = xe^{-x}.$$

Rewriting the left-hand side as a product rule, this becomes

$$\frac{d}{dx}(e^{-x}y) = xe^{-x}.$$

Integrating both sides, we find

$$\int \frac{d}{dx}(e^{-x}y) dx = \int xe^{-x} dx.$$

To evaluate the integral on the right-hand side, choose \( u = x \) and \( dv = e^{-x}dx \), then \( du = dx \) and \( v = -e^{-x} \). Applying the integration by parts formula then gives

$$\int \frac{d}{dx}(e^{-x}y) dx = -xe^{-x} + \int e^{-x}dx.$$

Therefore,

$$e^{-x}y = -xe^{-x} – e^{-x} + C.$$

Multiplying both sides of the equation by \( e^x \), we obtain

$$e^xe^{-x}y = -xe^xe^{-x} – e^xe^{-x} + Ce^x.$$

Using exponential properties, we find

$$e^{x – x}y = -xe^{x – x} – e^{x – x} + Ce^x.$$

Simplifying this is equivalent to

$$e^0y = -xe^0 – e^0 + Ce^x.$$

Hence,

$$1y = -1x – 1 + Ce^x.$$

Our solution is then

$$y = -x – 1 + Ce^x.$$

Applying the initial condition, we get

$$y(0) = -0 – 1 + Ce^0.$$

Multiplying then gives

$$y(0) = 0 – 1 + Ce^0.$$

This is equivalent to

$$y(0) = 0 – 1 + 1C.$$

Hence,

$$y(0) = 0 – 1 + C.$$

Simplifying, we find

$$y(0) = -1 + C.$$

By the initial condition, this is equivalent to

$$1 = -1 + C.$$

Which implies

$$2 = C.$$

Substituting into the solution then gives

$$y = -x – 1 + 2e^x.$$

Evaluating the function at \( x = 0.5 \) we obtain

$$y(0.5) = -0.5 – 1 + 2e^{0.5}.$$

Exponentiating this is

$$y(0.5) = -0.5 – 1 + 2(1.648721).$$

Multiplying we get

$$y(0.5) = -0.5 – 1 + 3.297442.$$

Adding and subtracting gives

$$y(0.5) = 1.797442.$$

The absolute error is then

$$\epsilon = |1.797442 – 1.797441|.$$

Subtracting gives

$$\epsilon = |0.000001|.$$

Taking the absolute value, we arrive at a final answer of

$$\epsilon = 0.000001.$$

The next example illustrates one of the primary applications of numerical analysis, namely solving differential equations that cannot be solved analytically.

Example 2: Use the Runge-Kutta method to approximate the solution of \( \frac{dy}{dx} = \sin{x} + e^y \), subject to the initial condition \( y(0) = 0 \) at \( x = 0.6 \). Use a step size of \( h = 0.2 \).

Solution: Consider the function

$$f(x, y) = \sin{x} + e^y.$$

Calculating the first slope, we obtain

$$k_1 = f(0, 0).$$

This is

$$k_1 = \sin{0} + e^0.$$

Evaluating the sine and exponential gives

$$k_1 = 0 + 1.$$

Adding we find

$$k_1 = 1.$$

Calculating the second slope, we obtain

$$k_2 = f(0 + \frac{0.2}{2}, 0 + \frac{(0.2)(1)}{2}).$$

Multiplying gives

$$k_2 = f(0 + \frac{0.2}{2}, 0 + \frac{0.2}{2}).$$

Dividing we get

$$k_2 = f(0 + 0.1, 0 + 0.1).$$

Adding we find

$$k_2 = f(0.1, 0.1).$$

This is

$$k_2 = \sin{0.1} + e^{0.1}.$$

Evaluating the sine and exponential gives

$$k_2 = 0.099833 + 1.105170.$$

Adding we find

$$k_2 = 1.205004.$$

Calculating the third slope, we obtain

$$k_3 = f(0 + \frac{0.2}{2}, 0 + \frac{(0.2)(1.205004)}{2}).$$

Multiplying gives

$$k_3 = f(0 + \frac{0.2}{2}, 0 + \frac{0.241000}{2}).$$

Dividing we get

$$k_3 = f(0 + 0.1, 0 + 0.120500).$$

Adding we find

$$k_3 = f(0.1, 0.120500).$$

This is

$$k_3 = \sin{0.1} + e^{0.120500}.$$

Evaluating the sine and exponential gives

$$k_3 = 0.099833 + 1.128061.$$

Adding we find

$$k_3 = 1.227894.$$

Calculating the fourth slope, we obtain

$$k_4 = f(0 + 0.2, 1 + (0.2)(1.227894).$$

Multiplying gives

$$k_4 = f(0 + 0.2, 0 + 0.245578).$$

Adding we find

$$k_4 = f(0.2, 0.245578).$$

This is

$$k_4 = \sin{0.2} + e^{0.245578}.$$

Evaluating the sine and exponential gives

$$k_4 = 0.198669 + 1.278361.$$

Adding we find

$$k_4 = 1.477030.$$

The next \( x \)-value is

$$x_1 = 0 + 0.2.$$

Adding gives

$$x_1 = 0.2.$$

The next \( y \)-value is

$$y_1 = 0 + \frac{0.2}{6}(1 + 2(1.205004) + 2(1.227894) + 1.477030).$$

Multiplying we get

$$y_1 = 0 + \frac{0.2}{6}(1 + 2.410008 + 2.455789 + 1.477030).$$

Adding gives

$$y_1 = 0 + \frac{0.2}{6}(7.342828).$$

Dividing this is

$$y_1 = 0 + 0.033333(7.342828).$$

Multiplying we obtain

$$y_1 = 0 + 0.244760.$$

Adding we find

$$y_1 = 0.244760.$$

Continuing, we calculate the first slope to obtain

$$k_1 = f(0.2, 0.244760).$$

This is

$$k_1 = \sin{0.2} + e^{0.244760}.$$

Evaluating the sine and exponential gives

$$k_1 = 0.198669 + 1.277315.$$

Adding we find

$$k_1 = 1.475985.$$

Calculating the second slope, we obtain

$$k_2 = f(0.2 + \frac{0.2}{2}, 0.244760 + \frac{(0.2)(1.475985)}{2}).$$

Multiplying gives

$$k_2 = f(0.2 + \frac{0.2}{2}, 0.244760 + \frac{0.295197}{2}).$$

Dividing we get

$$k_2 = f(0.2 + 0.1, 0.244760 + 0.147598).$$

Adding we find

$$k_2 = f(0.3, 0.392359).$$

This is

$$k_2 = \sin{0.3} + e^{0.392359}.$$

Evaluating the sine and exponential gives

$$k_2 = 0.295520 + 1.480469.$$

Adding we find

$$k_2 = 1.775990.$$

Calculating the third slope, we obtain

$$k_3 = f(0.2 + \frac{0.2}{2}, 0.244760 + \frac{(0.2)(1.775990)}{2}).$$

Multiplying gives

$$k_3 = f(0.2 + \frac{0.2}{2}, 0.244760 + \frac{0.355198}{2}).$$

Dividing we get

$$k_3 = f(0.2 + 0.1, 0.244760 + 0.177599).$$

Adding we find

$$k_3 = f(0.3, 0.422360).$$

This is

$$k_3 = \sin{0.3} + e^{0.422360}.$$

Evaluating the sine and exponential gives

$$k_3 = 0.295520 + 1.525557.$$

Adding we find

$$k_3 = 1.821077.$$

Calculating the fourth slope, we obtain

$$k_4 = f(0.2 + 0.2, 0.244760 + (0.2)(1.821077).$$

Multiplying gives

$$k_4 = f(0.2 + 0.2, 0.244760 + 0.3642156).$$

Adding we find

$$k_4 = f(0.4, 0.608976).$$

This is

$$k_4 = \sin{0.4} + e^{0.608976}.$$

Evaluating the sine and exponential gives

$$k_4 = 0.389418 + 1.838548.$$

Adding we find

$$k_4 = 2.227967.$$

The next \( x \)-value is

$$x_2 = 0.2 + 0.2.$$

Adding gives

$$x_2 = 0.4.$$

The next \( y \)-value is

$$y_2 = 0.244760 + \frac{0.2}{6}(1.475985 + 2(1.775990) + 2(1.821077) + 2.227967).$$

Multiplying we get

$$y_2 = 0.244760 + \frac{0.2}{6}(1.475985 + 3.551980 + 3.642155 + 2.227967).$$

Adding gives

$$y_2 = 0.244760 + \frac{0.2}{6}(10.898087).$$

Dividing this is

$$y_2 = 0.244760 + 0.033333(10.898087).$$

Multiplying we obtain

$$y_2 = 0.244760 + 0.363269.$$

Adding we find

$$y_2 = 0.608030.$$

Continuing, we calculate the first slope to obtain

$$k_1 = f(0.4, 0.608030).$$

This is

$$k_1 = \sin{0.4} + e^{0.608030}.$$

Evaluating the sine and exponential gives

$$k_1 = 0.389418 + 1.836810.$$

Adding we find

$$k_1 = 2.226228.$$

Calculating the second slope, we obtain

$$k_2 = f(0.4 + \frac{0.2}{2}, 0.608030 + \frac{(0.2)(2.226228)}{2}).$$

Multiplying gives

$$k_2 = f(0.4 + \frac{0.2}{2}, 0.608030 + \frac{0.445245}{2}).$$

Dividing we get

$$k_2 = f(0.4 + 0.1, 0.608030 + 0.222622).$$

Adding we find

$$k_2 = f(0.5, 0.830653).$$

This is

$$k_2 = \sin{0.5} + e^{0.830653}.$$

Evaluating the sine and exponential gives

$$k_2 = 0.479425 + 2.294817.$$

Adding we find

$$k_2 = 2.774243.$$

Calculating the third slope, we obtain

$$k_3 = f(0.4 + \frac{0.2}{2}, 0.608030 + \frac{(0.2)(2.774243)}{2}).$$

Multiplying gives

$$k_3 = f(0.4 + \frac{0.2}{2}, 0.608030 + \frac{0.554848}{2}).$$

Dividing we get

$$k_3 = f(0.4 + 0.1, 0.608030 + 0.277424).$$

Adding we find

$$k_3 = f(0.5, 0.885454).$$

This is

$$k_3 = \sin{0.5} + e^{0.885454}.$$

Evaluating the sine and exponential gives

$$k_3 = 0.479425 + 2.424086.$$

Adding we find

$$k_3 = 2.903512.$$

Calculating the fourth slope, we obtain

$$k_4 = f(0.4 + 0.2, 0.608030 + (0.2)(2.903512).$$

Multiplying gives

$$k_4 = f(0.4 + 0.2, 0.608030 + 0.580702.$$

Adding we find

$$k_4 = f(0.6, 1.188733).$$

This is

$$k_4 = \sin{0.6} + e^{1.188733}.$$

Evaluating the sine and exponential gives

$$k_4 = 0.564642 + 3.282919.$$

Adding we find

$$k_4 = 3.847561.$$

The next \( x \)-value is

$$x_3 = 0.4 + 0.2.$$

Adding gives

$$x_3 = 0.6.$$

The next \( y \)-value is

$$y_3 = 0.608030 + \frac{0.2}{6}(2.226228 + 2(2.774243) + 2(2.903512) + 3.847561).$$

Multiplying we get

$$y_3 = 0.608030 + \frac{0.2}{6}(2.226228 + 5.548486 + 5.807024 + 3.847561).$$

Adding gives

$$y_3 = 0.608030 + \frac{0.2}{6}(17.4293014).$$

Dividing this is

$$y_3 = 0.608030 + 0.033333(17.4293014).$$

Multiplying we obtain

$$y_3 = 0.608030 + 0.580976.$$

Adding we arrive at a final answer of

$$y_3 = 1.189007.$$

Conclusion

The Runge-Kutta method improves the approximation of a differential equation by evaluating the slope at several points within each step. These slope estimates are then combined using a weighted average to approximate the solution.

Throughout this guide on the Runge-Kutta method for beginners, we have seen that the method is particularly effective for many smooth initial value problems. We have also seen that it does not work perfectly in every situation. A step size that is too large, discontinuities, and stiff differential equations can all create difficulties. Understanding both the strengths and limitations of the Runge-Kutta method is therefore just as important as knowing how to perform the calculations.

Once you are comfortable with the basic algorithm, you can begin applying it to increasingly complicated differential equations. The goal is not simply to memorize formulas, but to understand how they work together to approximate the solution of a differential equation.

With that understanding, the Runge-Kutta method becomes much more than a collection of calculations. It becomes a tool for solving differential equations numerically and exploring problems that may be difficult or impossible to solve analytically.

Further Reading

How to Solve Differential Equations Using Euler’s Method: A Complete Tutorial – Learn how Euler’s method approximates solutions to ordinary differential equations by using the slope at each step to estimate the next solution value. This provides a foundation for understanding how Runge–Kutta methods use multiple slope estimates to improve accuracy.

Frequently Asked Questions

For a sufficiently smooth differential equation under appropriate conditions, the global truncation error is proportional to \( h^4 \), where \( h \) is the step size.

The appropriate step size depends on the differential equation and the accuracy you need. Smaller step sizes generally improve accuracy, but they also require more calculations.

One approach is to calculate the solution using two different step sizes and compare the results. When the method is accurate to fourth order, the difference between these approximations can help estimate the remaining error.

Take The Next Step

Put numerical analysis into practice with the Numerical Analysis Course. Work through the concepts step by step with lessons, Python labs, assignments, and a capstone project.

Support The Math Perimeter

If you enjoyed this article, please consider supporting The Math Perimeter on Buy Me a Coffee, so I can keep making more content like it.