# Runge-Kutta Methods – Algorithm, Implementation in C With Solved Examples

Numerical Methods & Algorithms / Wednesday, October 24th, 2018
(Last Updated On: November 16, 2018)

# Runge-Kutta Method

Finding the solution of differential equation the Runge Kutta method give more accurate result. The Euler method is less efficient in practical problems because if h is not sufficiently small then this method gives inaccurate result.

One advantage of Runge Kutta methods is it requires only the value of the function at some selected points on the sub-interval and it is stable, and easy to program.

The Runge-Kutta methods perform several function evaluations at each step and avoid the computation of higher order derivatives. These methods can be constructed for any order, i.e., second, third, fourth, fifth etc. The fourth-order Runge-Kutta method is more popular.

## Second-order  Runge-Kutta method

The second-order Runge-Kutta formula is

${{y}_{1}}={{y}_{0}}+\frac{1}{2}\left( {{k}_{1}}+{{k}_{2}} \right)$

Where

${{k}_{1}}=hf\left( {{x}_{0}},{{y}_{0}} \right)~and$

${{k}_{2}}=hf\left( {{x}_{0}}+h,{{y}_{0}}+hf\left( {{x}_{0}},{{y}_{0}} \right) \right)=hf\left( {{x}_{0}}+h,{{y}_{0}}+{{k}_{1}} \right)$

The local truncation error of this formula is of O(h3).

## Fouth-order  Runge-Kutta method

The fourth-order  Runge-Kutta method is

${{y}_{1}}={{y}_{0}}+\frac{1}{6}\left( {{k}_{1}}+2{{k}_{2}}+2{{k}_{3}}+{{k}_{4}} \right)$

Where

${{k}_{1}}=hf\left( {{x}_{0}},{{y}_{0}} \right)$

${{k}_{2}}=hf\left( {{x}_{0}}+\frac{h}{2},{{y}_{0}}+\frac{{{k}_{1}}}{2} \right)$

${{k}_{3}}=hf\left( {{x}_{0}}+\frac{h}{2},{{y}_{0}}+\frac{{{k}_{2}}}{2} \right)$

${{k}_{4}}=hf\left( {{x}_{0}}+h,{{y}_{0}}+{{k}_{3}} \right)$

Starting with the initial point (x0, y0), one can generate the sequence of solutions x1, x2, … using the formula

${{y}_{i+1}}={{y}_{i}}+\frac{1}{6}\left( {{k}_{1}}^{\left( i \right)}+2{{k}_{2}}^{\left( i \right)}+2{{k}_{3}}^{\left( i \right)}+{{k}_{4}}^{\left( i \right)} \right)$

Where

${{k}_{1}}^{\left( i \right)}=hf\left( {{x}_{i}},{{y}_{i}} \right)$

${{k}_{2}}^{\left( i \right)}=hf\left( {{x}_{i}}+\frac{h}{2},{{y}_{i}}+\frac{{{k}_{1}}^{\left( i \right)}}{2} \right)$

${{k}_{3}}^{\left( i \right)}=hf\left( {{x}_{i}}+\frac{h}{2},{{y}_{0}}+\frac{{{k}_{2}}^{\left( i \right)}}{2} \right)$

${{k}_{4}}^{\left( i \right)}=hf\left( {{x}_{i}}+h,{{y}_{i}}+{{k}_{3}}^{\left( i \right)} \right)$

## Algorithm of Fourth-order Runge-Kutta method

This algorithm finds the solution of the differential equation y’ = f(x, y) with y(x0) = y0 using fourth-order Runge-Kutta method, i.e., using the formula

${{y}_{i+1}}={{y}_{i}}+\frac{1}{6}\left( {{k}_{1}}+2\left( {{k}_{2}}+{{k}_{3}} \right)+{{k}_{4}} \right)$

Within the interval [x1, yn] at step h.

## Fouth-order Runge-Kutta method implementation in C

Output:

Enter the initial values of x and y: 0 2

Enter last value of x: 0.5

Enter step length h: 0.1

x-value                y-value

0.100000            1.826528

0.200000            1.695464

0.300000            1.595978

0.400000            1.521567

0.500000            1.468221

 Example 01

Compute y(0.4) from the equation

$\frac{dy}{dx}=x-y,y\left( 0 \right)=1$

Taking h = 0.1 by Runge-Kutta method, correct to 4 decimal places.

Solution:

$Here~~f(x,y)=x-y,~~{{x}_{0}}=0,{{y}_{0}}=1,h=0.1$

$\therefore {{k}_{1}}=hf\left( {{x}_{0}},{{y}_{0}} \right)=0.1f(0,1)=0.1(0-1)=-0.1$

${{k}_{2}}=hf\left( {{x}_{0}}+h,{{y}_{0}}+{{k}_{1}} \right)$

$\Rightarrow {{k}_{2}}=0.1f\left( 0+0.1,1-0.1 \right)=0.1\left( 0.1-0.9 \right)=-0.08$

$Thus~~{{y}_{1}}=y\left( 0.1 \right)={{y}_{0}}+\frac{1}{2}\left( {{k}_{1}}+{{k}_{2}} \right)$

$\therefore {{y}_{1}}=1+\frac{1}{2}\left( -0.1-0.08 \right)=0.91$

$\therefore {{x}_{1}}=0.1,{{y}_{1}}=0.91$

$\therefore {{k}_{1}}=hf\left( {{x}_{1}},{{y}_{1}} \right)=0.1\left( 0.1-0.91 \right)=-0.081$

$\therefore {{k}_{2}}=hf\left( {{x}_{1}}+h,{{y}_{1}}+{{k}_{1}} \right)=0.1\left( 0.2-0.829 \right)=-0.0629$

$Hence~~{{y}_{2}}=y\left( 0.2 \right)={{y}_{1}}+\frac{1}{2}\left( {{k}_{1}}+{{k}_{2}} \right)$

$\therefore {{y}_{2}}=0.91+\frac{1}{2}\left( -0.081-0.0629 \right)=0.83805$

$\therefore {{x}_{2}}=0.2,{{y}_{2}}=0.83805$

$Hence~~{{y}_{3}}=y\left( 0.3 \right)={{y}_{2}}+\frac{1}{2}\left( {{k}_{1}}+{{k}_{2}} \right)=0.782435$

$\therefore {{x}_{3}}=0.3,{{y}_{3}}=0.782435$

$\therefore {{k}_{1}}=hf\left( {{x}_{3}},{{y}_{3}} \right)=-0.048244$

$\therefore {{k}_{2}}=hf\left( {{x}_{3}}+h,{{y}_{3}}+{{k}_{1}} \right)=-0.033419$

$\therefore {{y}_{4}}=y\left( 0.4 \right)={{y}_{3}}+\frac{1}{2}\left( {{k}_{1}}+{{k}_{2}} \right)=0.741604$

$Hence~~{{y}_{4}}=0.7416$

Correct to 4 decimal places.

 Example 02

Use Runge-Kutta’s fourth-order method to calculate y(0.2) from the differential equation

$\frac{dy}{dx}=x+x{{y}^{2}},y(0)=1~~with~~step~~h=0.1$

Do it yourself  🙂

 Example 03

Use Runge-Kutta’s fourth-order method to obtain the value when x = 1 given that y = 1 when x = 0 and that

$\frac{dy}{dx}=\frac{y-x}{y+x},with~~step~~h=0.5$

Do it yourself  🙂

<<Previous   Next>>