Write a program to demonstrate Runge-Kutta fourth order Method in C language.

Tested on: Turbo C++ Version 4.0Time Complexity: O (n)Space Complexity: O (1)Open Source: MIT
1
2
        /* Program to execute Range-Kutta method of fourth order */
3
	#include<stdio.h>
4
	#include<conio.h>
5
	#include<math.h>
6
	float dif ( float x,float y )
7
	{
8
	    return   x+y*y;
9
	}
10
	void main()
11
	{
12
	    float x  ,h,x0,x1,y0,y1,k1,k2,k3,k4,k;
13
	    printf("Enter the values of x, h, x0, x0, y0: ");
14
	    scanf("%f%f%f%f", &x, &h, &x0,&y0);
15
	    x1=x0;
16
	    y1=y0;
17
	    while(x1 < x) {
18
		k1=h*dif(x1,y1);
19
		printf("\n\tk1=%f",k1);
20
		k2=h*dif(x1+h/2,y1+k1/2);
21
		printf("\n\tk2=%f",k2);
22
		k3=h*dif(x1+h/2,y1+k2/2);
23
		printf("\n\tk3=%f",k3);
24
		k4=h*dif(x1+h,y1+k3);
25
		printf("\n\tk4=%f",k4);
26
		k=(k1+2*(k2+k3)+k4)/6;
27
		printf("\n\tk=%f",k);
28
		y1 += k;
29
		x1 +=h;
30
		printf("\n Values of y=%f, when x=%f",y1,x1);
31
	    }
32
	    getch();
33
	}
34
        
35
        

Output

1
2
            
3
        Enter the values of x, h, x0, y0: 0.2 0.1 0 1
4
	
5
	    k1 = 0.100000
6
	    k2 = 0.115250
7
	    k3 = 0.116857
8
	    k4 = 0.134737
9
	    k = 0.116492
10
	Value of y = 1.116492, when x = 0.100000
11
	    k1 = 0.134655
12
	    k2 = 0.155143
13
	    k3 = 0.157579
14
	    k4 = 0.182326
15
	    k = 0.157071
16
	Value of y = 1.273562, when x = 0.200000
17
            
18
            

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

How to run Runge Kutta 4th Order Method program in C-Language?

To run Runge Kutta 4th Order Method program in C-Language, follow the below mentioned steps:

  1. Copy the Code: Press the copy button in the code block to copy the complete code.
  2. Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
  3. Paste the code: Paste the copied code into Turbo C++’s editor.
  4. Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
  5. Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
  6. Enter the Input: Provide the required inputs or your desired inputs, if applicable.
  7. Receive the Result: Get the result of the program on the screen based on the provided inputs.

More About Runge-Kutta 4th Order (RK4) Method

Discovered by: German Mathematicians Carl Runge and Martin Kutta

Discovered Year: 1895-1901

Global Error: O(h4)

Local Truncation Error: O(h5)

Method Type: Single-Step Explicit Numerical Method

Runge-Kutta 4th Order Method is known as the Gold Standard method to solve differential equations (ODEs). When you need a direct answer for any equation where the slope of the equation changes rapidly, then the first choice is the Runge-Kutta 4th Order Method.

How does the Runge-Kutta 4th Order Method work?

It checks the slopes at 4 different points in each small step (h).

Then computes a weighted average of these slopes and moves forward with the next step (h).

The all 4 slopes for 4 different-different points are denoted by k1,k2, k3, and k4. With the help of the formula, the final result is calculated

Image

Where the k1, k2, k3 and k4 are mentioned below

Image

Image

Image

Image

Why is the Runge-Kutta 4th Order Method used?

To find high precision, the Runge-Kutta 4th Order Method is used because it finds the slopes for 4 different points in each step, which causes an error that is almost equal to zero, even if you consider a larger step (h) value.

Where is the Runge-Kutta 4th Order Method used?

  1. Aerospace or Rocket Science: When the rocket is launched in space, the gravity and air resistance continuously change to track the trajectory of the rocket or aerospace this Runge-Kutta 4th Order Method is used.
  2. Game Physics and Simulation: The Runge-Kutta 4th Order Method is used in Game physics and simulation for Rigid body dynamics, gravity simulation, and realistic projectile motion in the backend.
  3. Astronomy (Planetary orbits): To calculate the orbits of the planets that are rotating around the Sun, which has different gravity values every second.

Limitations of Runge-Kutta 4th Order Method

  • Heavy calculation: Due to calculating the 4 slopes for 4 points in every single step, the calculations are very heavy, and this makes a big challenge for manual calculations.
  • Old Points are vanishing: The main drawback is that in the Runge-Kutta 4th Order Method, the old points that are processed vanish during the process.

Alternate Methods

  • RK-Fehlberg (RKF45) and Dormand-Prince (DP54): If the curve changes rapidly then these methods divide the steps into small size for more accuracy.
  • Multi-Step Methods (Adams-Bashforth & Adams-Moulton): Due to not remembering the old step points, the multi-step methods are used because these methods remember the old steps; hence, as a result, they are faster than the Runge-Kutta 4th Order Method.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.