Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Runge-Kutta 4th order rule for differential equation
Runge Kutta method is used for solving ordinary differential equations (ODE). It uses dy/dx function for x and y, and also need the initial value of y, i.e. y(0). It finds the approximate value of y for given x. For solving ODE, we have to follow these formulas:

Here h is the height of the interval.
Note: From these formulas, we can use first two k1 and k2 find the Runge-Kutta 2nd Order solution for ODE.
Input and Output
Input: The x0 and f(x0): 0 and 0 the value of x = 0.4 the value of h = 0.1 Output: Answer of differential equation: 0.0213594
Algorithm
rungeKutta(x0, y0, x, h)
Input − Initial x and y value, the targeted x value, and the height of interval h.
Output − The value of y for value x.
Begin
iteration := (x – x0)/h
y = y0
for i := 1 to iteration, do
k1 := h*f(x0, y)
k2 := h*f((x0 + h/2), (y + k1/2))
k3 := h*f((x0 + h/2), (y + k2/2))
k4 := h*f((x0 + h), (y + k3))
y := y + (1/6)*(k1 + 2k2 + 2k3 + k4)
x0 := x0 + h
done
return y
End
Example
#includeusing namespace std; double diffOfy(double x, double y) { return ((x*x)+(y*y)); //function x^2 + y^2 } double rk4thOrder(double x0, double y0, double x, double h) { int iteration = int((x - x0)/h); //calculate number of iterations double k1, k2, k3, k4; double y = y0; //initially y is f(x0) for(int i = 1; i> x0 >> y0; cout > x; cout > h; cout Output
Enter x0 and f(x0): 0 0 Enter x: 0.4 Enter h: 0.1 Answer of differential equation: 0.0213594
Advertisements
