
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
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
#include <iostream> using 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<=iteration; i++) { k1 = h*diffOfy(x0, y); k2 = h*diffOfy((x0+h/2), (y+k1/2)); k3 = h*diffOfy((x0+h/2), (y+k2/2)); k4 = h*diffOfy((x0+h), (y+k3)); y += double((1.0/6.0)*(k1+2*k2+2*k3+k4)); //update y using del y x0 += h; //update x0 by h } return y; //f(x) value } int main() { double x0, y0, x, h; cout << "Enter x0 and f(x0): "; cin >> x0 >> y0; cout << "Enter x: "; cin >> x; cout << "Enter h: "; cin >> h; cout << "Answer of differential equation: " << rk4thOrder(x0, y0, x, h); }
Output
Enter x0 and f(x0): 0 0 Enter x: 0.4 Enter h: 0.1 Answer of differential equation: 0.0213594
- Related Articles
- Euler Method for solving differential equation in C++
- 2A+B+C ----->+E;It is the first-order reaction with respect to A, A, second order with respect to B and zero-order with respect to C. Give the differential rate equation for the reaction.
- How to order by a custom rule like order like 4,2,1,3 in MySQL?
- Explain Differential pricing method
- Trapezoidal Rule for definite integral
- Usage of @import Rule for CSS
- What is Differential Cryptanalysis in Information Security?
- What is the golden rule for complimentary feeding?
- Check the divisibility rule of 11 for 1.10000001.
- Signals and Systems – Solving Differential Equations with Laplace Transform
- Shorthand property for setting all the column-rule-* properties
- Simpson's 1/3 Rule for definite integral
- What is rule for perimeter of rectangle and square?
- Current Divider Rule and Voltage Divider Rule
- Explain inverse square rule and its derivation for class 9.

Advertisements