Runge-kutta’s method of order 4
f ( xi , yi ) : k1 / h
f ( xi 1/ 2 , yi 1/ 2 ) : k 2 / h , k3 / h
f ( xi 1 , yi 1 ) : k 4 / h
TNPL
JoongJin-Cho
Algorithm
This routine solve the initial value problem
at equidistant points
t1 t0 h, t2 t0 2h,, t N t0 Nh
Here the function f(x,t) is continuous the
interval t0 , t N
TNPL
JoongJin-Cho
Algorithm
x(ti 1 ) x(ti )
ti 1
f ( x, t )dt
ti
x(ti ) h ( xi , ti , hi ) x(ti ) ( xi , ti , hi )
a1k1 a2 k 2 an k n
Taylor Series (To compare K 4 Talyor series )
2
3
h
h
xi 1 xi hyi yi yi
2
3!
and Error bound 0(h n 1 ) 0(h 5 )
TNPL
JoongJin-Cho
Algorithm
Runge-Kutta’s method of order 4 to solve a first-order
differential equation
INPUT: Initial values t0 , x0 , step siz e h, number of steps N
CALCULATE: Approximate solution x at tN
For i 0,1,2, ,N
K 1 f(xi ,ti )
h
h
K 1 ,ti )
2
2
h
h
K 3 f(xi K 2 ,ti )
2
2
K 4 f(xi hK 3 ,ti h)
K 2 f(xi
h
(K 1 2K 2 2K 3 K 4 ); (compute xi 1 )
6
ti h;(compute ti 1 )
xi 1 x1
ti 1
OUTPUT: xN , t N
END
TNPL
JoongJin-Cho
Java Programming
//4th order Runge-Kutta method, need derivative f
public static void main(String args[]){
int i;
double t,ti,h,x,xi, N=5;
double f; //Differential equation dx/dt=f(x,t)=x+t
double t0,x0,f1,f2,f3,f4;
x=xi; t=ti;
f=x+t;
for(i=0;i<N;i++){
t0=t;x0=x;
f=x+t;
f1=f;
// K1
x=x0+h*f1/2; t=t0+h/2;
f=x+t;
f2=f;
// K2
x=x0+h*f2/2; t=t0+h/2;
f=x+t;
f3=f;
// K3
x=x0+h*f3; t=t0+h;
f=x+t;
f4=f;
x=x0+h*(f1+2*f2+2*f3+f4)/6;
t=t0+h;
TNPL
// K4
// Xi+1
// ti+1
JoongJin-Cho
import java.io.*;
public class RungeKutta2{
static BufferedReader bf = new BufferedReader
(new InputStreamReader(System.in));
//4th order Runge-Kutta method, need derivative f
public static void main(String args[])throws IOException
{
int i;
double t,ti,h,x,xi,N=5;
double f;
double t0,x0,f1,f2,f3,f4;
String str;
System.out.print("Input initial value ti = >");
str=bf.readLine();
ti=Double.parseDouble(str);
System.out.print("Input initial value xi = >");
str=bf.readLine();
xi=Double.parseDouble(str);
System.out.print("step size h = >");
str=bf.readLine();
h=Double.parseDouble(str);
TNPL
JoongJin-Cho
x=xi; t=ti;
f=x+t;
for(i=0;i<N;i++){
t0=t;x0=x;
f=x+t;
f1=f;
x=x0+h*f1/2; t=t0+h/2;
f=x+t;
f2=f;
x=x0+h*f2/2; t=t0+h/2;
f=x+t;
f3=f;
x=x0+h*f3; t=t0+h;
f=x+t;
f4=f;
}
x=x0+h*(f1+2*f2+2*f3+f4)/6;
t=t0+h;
System.out.println
("t="+(float)t+"\t"+"x="+x+"\t"+"f="+f);
}
}
TNPL
JoongJin-Cho
© Copyright 2026 Paperzz