14. Write a program to demonstrate Runge-Kutta fourth order Method.
/* Program to execute Range-Kutta method of fourth order */
#include<stdio.h>
#include<conio.h>
#include<math.h>
float dif ( float x,float y )
{
return x+y*y;
}
void main()
{
float x ,h,x0,x1,y0,y1,k1,k2,k3,k4,k;
printf("Enter the values of x, h, x0, x0, y0: ");
scanf("%f%f%f%f", &x, &h, &x0,&y0);
x1=x0;
y1=y0;
while(x1 < x) {
k1=h*dif(x1,y1);
printf("\n\tk1=%f",k1);
k2=h*dif(x1+h/2,y1+k1/2);
printf("\n\tk2=%f",k2);
k3=h*dif(x1+h/2,y1+k2/2);
printf("\n\tk3=%f",k3);
k4=h*dif(x1+h,y1+k3);
printf("\n\tk4=%f",k4);
k=(k1+2*(k2+k3)+k4)/6;
printf("\n\tk=%f",k);
y1 += k;
x1 +=h;
printf("\n Values of y=%f, when x=%f",y1,x1);
}
getch();
}
Output
Enter the values of x, h, x0, y0: 0.2 0.1 0 1
k1 = 0.100000
k2 = 0.115250
k3 = 0.116857
k4 = 0.134737
k = 0.116492
Value of y = 1.116492, when x = 0.100000
k1 = 0.134655
k2 = 0.155143
k3 = 0.157579
k4 = 0.182326
k = 0.157071
Value of y = 1.273562, when x = 0.200000