# Ordinary Differential Equation Using Fourth Order Runge Kutta (RK) Method Using C

This program is implementation of Runge Kutta Fourth Order method for solving ordinary differential equation using C programming language with output.

Output of this is program is solution for dy/dx = (y2 - x2)/(y2+x2) with initial condition y = 1 for x = 0 i.e. y(0) = 1 and we are trying to evaluate this differential equation at y = 0.4 in two steps i.e. n = 2. ( Here y = 0.4 i.e. y(0.4) = ? is our calculation point)

## C Program for RK-4 Method

``````
#include<stdio.h>
#include<conio.h>

#define f(x,y) (y*y-x*x)/(y*y+x*x)

int main()
{
float x0, y0, xn, h, yn, k1, k2, k3, k4, k;
int i, n;
clrscr();
printf("Enter Initial Condition\n");
printf("x0 = ");
scanf("%f", &x0);
printf("y0 = ");
scanf("%f", &y0);
printf("Enter calculation point xn = ");
scanf("%f", &xn);
printf("Enter number of steps: ");
scanf("%d", &n);

/* Calculating step size (h) */
h = (xn-x0)/n;

/* Runge Kutta Method */
printf("\nx0\ty0\tyn\n");
for(i=0; i < n; i++)
{
k1 = h * (f(x0, y0));
k2 = h * (f((x0+h/2), (y0+k1/2)));
k3 = h * (f((x0+h/2), (y0+k2/2)));
k4 = h * (f((x0+h), (y0+k3)));
k = (k1+2*k2+2*k3+k4)/6;
yn = y0 + k;
printf("%0.4f\t%0.4f\t%0.4f\n",x0,y0,yn);
x0 = x0+h;
y0 = yn;
}

/* Displaying result */
printf("\nValue of y at x = %0.2f is %0.3f",xn, yn);

getch();
return 0;
}

``````

## RK Method C Program Output

```Enter Initial Condition
x0 = 0
y0 = 1
Enter calculation point xn = 0.4
Enter number of steps: 2

x0      y0      yn
0.0000  1.0000  1.1960
0.2000  1.1960  1.3753

Value of y at x = 0.40 is 1.375
```