# Numerical method Solution of ordinary differential equation using RK4 method in C

Numerical method : Solution of ordinary differential equation using RK4 method in C

Algorithm:

1. Start
2. Declare and Initialize necessary variable likes K1, K2, K3, K4 etc.
3. Declare and define the function that returns the functional value.
4. Get the initial value of x, initial value of y, no. of iteration and interval from user.
5. for i = 0 to i < n go to step 6.
6. Do the following calculation: and go to step 7
7. The new values of x and y are: x = x + interval, y = y + K;
8. print the value of x and y;
9. stop

Flowchart:

Source Code:

```#include
/******************************************************
Program: solution of ordinary differential equation
Language : C
Author: Bibek Subedi
Tribhuvan University, Nepal
********************************************************/
float func(float x, float y){
return (y*y-x*x)/(y*y+x*x);
}

int main(){
float K, K1, K2, K3, K4;
float x0 , y0, x, y;
int j, n; float i, H;
printf("Enter initial value of x: ");
scanf("%f", &x0);
printf("Enter initial value of y: ");
scanf("%f", &y0);
printf("Enter no iteration: ");
scanf("%d", &n);
printf("Enter the interval: ");
scanf("%f", &H);
x = x0;
y = y0;
for(i = x+H, j = 0; j < n; i += H, j++){
K1 = H * func(x , y);
K2 = H * func(x+H/2, y+K1/2);
K3 = H * func(x+H/2, y+K2/2);
K4 = H * func(x+H, y+K3);

K = (K1 + 2*K2 + 2*K3 + K4)/6;

x = i;
y = y + K;

printf("At  x = %.2f, y = %.4f ", x, y);
printf("\n");
}
return 0;
}

Output

Related PostsJuly 29, 2012 Numerical Methods Solution of nonlinear equation using Newton Raphson method in CJuly 30, 2012 Numerical Methods Solution of nonlinear equation by using Secant method in CJuly 28, 2012 Numerical Methods Solution of nonlinear equations by using Bisection method in CAugust 3, 2012 Numerical Methods Solution of simultaneous algebraic equations using Gauss Jordan method in CAugust 2, 2012 Numerical Methods Solution of simultaneous algebraic equations using Gauss Elimination method in CZemanta

```