Runge-Kutta method is a popular iteration method of approximating solution of ordinary differential equations. Developed around 1900 by German mathematicians C.Runge and M. W. Kutta, this method is applicable to both families of explicit and implicit functions.

Also known as RK method, the Runge-Kutta method is based on solution procedure of initial value problem in which the initial conditions are known. Based on the order of differential equation, there are different Runge-Kutta methods which are commonly referred to as: RK2, RK3, and RK4 methods.

In earlier tutorial, we’ve already discussed a C program for RK4 method. This tutorial focuses on writing a general program code for **Runge-Kutta method in MATLAB** along with its mathematical derivation and a numerical example.

## Derivation of Runge-Kutta Method:

Let’s consider an initial value problem given as:

y’ = f( t, y), y(t_{0}) = y_{0}

where,

- y is an unknown function of time t, which may be scalar or vector quantity
- y’ is the rate of change of y with respect to t
- t
_{0}is the initial value of t - y
_{0}is the value of y at time t_{0}

Let ‘h’ be the step size such that h > 0. Now, the generation term of the series can be defined as:

y_{n+1} = y_{n} + h/6 * ( k_{1} + 2k_{2} + 2k_{3} + k_{4})

t_{n+1} = t_{n} + h

for n = 1, 2 , 3, 4, …. such that:

- k
_{1}= f( t_{n}, y_{n}) - k
_{2}= f( tn + h/2 , yn + h/2 k_{1}) - k
_{3}= f( tn + h/2, y_{n}+ h/2k_{2}) - k
_{4}= f( t_{n}+ h, y_{n}+ hk_{3}) - yn+1 is the Runge-Kutta method approximation of y(tn+1)
- k
_{1}is the increment which depends on the gradient of starting interval as in Euler’s method - k
_{2}is the increment which relies on the slope at the midpoint of the interval, k_{2 }= y+ h/2 * k_{1} - k
_{3}is also an increment based on the gradient at midpoint, k3=y+h/2*k2 - k
_{4 }is again an increment whose value depends on end of the end of interval k_{4}= y + hk_{3}

## Runge-Kutta Method in MATLAB:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | function a = runge_kutta(df) % asking initial conditions x0 = input('Enter initial value of x : '); y0 = input ('Enter initial value of y : '); x1 = input( 'Enter value of x at which y is to be calculated : '); tol = input( 'Enter desired level of accuracy in the final result : '); %choose the order of Runge-Kutta method r = menu ( ' Which order of Runge Kutta u want to use', ... ' 2nd order ' , ' 3rd order ' , ' 4th order '); switch r case 1 % calculating the value of h n =ceil( (x1-x0)/sqrt(tol)); h = ( x1 - x0)/n; for i = 1 : n X(1,1) = x0; Y (1,1) = y0; k1 = h*feval( df , X(1,i), Y(1,i)); k2 = h*feval( df , X(1,i) + h , Y(1,i) + k1); k = 1/2* ( k1+ k2); X( 1, i+1) = X(1,i) + h; Y( 1 ,i+1) = Y(1,i) + k; end case 2 % calculating the value of h n =ceil( (x1-x0)/nthroot(tol,3)); h = ( x1 - x0)/n; for i = 1 : n X(1,1) = x0; Y (1,1) = y0; k1 = h*feval( df , X(1,i), Y(1,i)); k2 = h*feval( df , X(1,i) + h/2, Y(1,i) + k1); k3 = h*feval( df , X(1,i) + h, Y(1,i) + k2); k = 1/6* ( k1+ 4*k2 + k3); X( 1, i+1) = X(1,i) + h; Y( 1 ,i+1) = Y(1,i) + k; end case 3 % calculating the value of h n =ceil( (x1-x0)/nthroot(tol,3)); h = ( x1 - x0)/n; for i = 1 : n X(1,1) = x0; Y (1,1) = y0; k1 = h*feval( df , X(1,i), Y(1,i)); k2 = h*feval( df , X(1,i) + h/2, Y(1,i) + k1); k3 = h*feval( df , X(1,i) + h/2, Y(1,i) + k2); k4 = h*feval( df , X(1,i) + h, Y(1,i) + k3); k = 1/6* ( k1+ 2*k2 + 2*k3 + k4); X( 1, i+1) = X(1,i) + h; Y( 1 ,i+1) = Y(1,i) + k; end end %displaying results fprintf( 'for x = %g \n y = %g \n' , x1,Y(1,n+1)) %displaying graph x = 1:n+1; y = Y(1,n+1)*ones(1,n+1) - Y(1,:); plot(x,y,'r') xlabel = (' no of interval '); ylabel = ( ' Error '); |

The given code for Runge-Kutta method in Matlab is applicable to find out the approximate solution of ordinary differential equation of any order. In the source code, the argument ‘df’ is defined to represent equation, making right hand side zero.

The differential equation to be solved is given as input to the program through a MATLAB file. For example, if y ‘ = sin(x) + 2 is to be solved by using this MATLAB source code, following piece of codes should be saved as **ex.m** file and opened while executing the above program:

1 2 3 4 | % y is the function of x alone function y=y(x) y=sin (x) +2 ; |

As the Runge-Kutta Methods are based on initial value problem, it is necessary to define the initial condition in any problem. When the program is executed, it asks for initial condition i.e. initial value of x, initial value of y, and the degree of accuracy or error tolerance.

After inputting all the values, the program asks to choose the order of Runge-Kutta method. Based on the order, the calculations are proceeded as explained above in the mathematical derivation. Here’s a sample output of the program:

The last part of the code is for displaying graph as shown below:

## Runge-Kutta Numerical Example:

Let’s analyze and solve an initial value problem using Runge-Kutta method. We have to approximate y(1.5) and y(1.0) using RK4 method in this problem:

y'(t) = 1 – t*y(t) ; y(0.5) = 2.5

Here, given initial condition: *t*_{0} = 0.5, *y*_{0} = 2.5

For y’ = 1 – ty, adopt a step size, h = 1. Then, finding the value of k_{1}, k_{2}, k_{3} and k_{4} using the formulas discussed in the derivation.

K_{1} = f(0.5, 2.5) = -0.25

K_{2} = f(1, 2.375) = -1.375

K_{3} = f(1, 1.8125) = -0.8125

K_{4} = f(0.5, 1.6875) = -0.153125

(K_{1} + 2K_{2} + 2K_{3} + K_{4})/6 = -1.026041667

The first approximation of y, *y*_{1} = *y*_{0} + *h*(-1.026041667) = 1.473958333

Next, to approximate y_{2}, we have: *t*_{0} = 0.5, *y*_{0} = 2.5, and *h* = 0.5

K_{1} = f(0.5, 2.5) = -0.25

K_{2} = f(1, 2.4375) = -0.828125

K_{3} = f(1, 2.29296875) = -0.719726562

K_{4} = f(0.5, 2.140136719) = -1.140136719

(K_{1} + 2K_{2} + 2K_{3} + K_{4})/6 = -0.7476399739

The second approximation of y, y_{2} = *y*_{0} + *h*(-0.7476399739) = 2.126180013 …. and so on for others.

The whole calculation procedure of this numerical example (and of any program code of Runge-Kutta method in MATLAB) is shown in the table below:

In Runge-Kutta method, the accuracy of the result depends on the value of step size, h. Smaller the value of h, higher will be the accuracy of the result obtained. The aforementioned table shows the various value of h in descending order and respective result of the function.

So, whether you’re solving a program manually or by programming (using MATLAB, C, or any other language syntax), the smaller the value of h, smaller will be the error in calculation of result. You can find more Numerical methods tutorial using MATLAB here.

Sir can i hav a car rential system code

how can i write a matlab code for lobatto methods?