You are on page 1of 5

Runge-Kutta Method

#include<stdio.h>
#include <math.h>
#include<conio.h>
#define F(x,y) (y)+(x)
void main()
{
double y0,x0,y1,n,h,f,f1,f2,f3,k1,k2,k3,k4;
int j;
printf("\nEnter the value of x0: ");
scanf("%lf",&x0);
printf("\nEnter the value of y0: ");
scanf("%lf",&y0);
printf("\nEnter the value of h: ");
scanf("%lf",&h);
printf("\nEnter the value of last point: ");
scanf("%lf",&n);
for(; x0<n; x0=x0+h)
{

f=F(x0,y0);
k1 = h * f;
f1 = F(x0+(h/2),y0+(k1/2));
k2 = h * f1;
f2 = F(x0+(h/2),y0+(k2/2));
k3 = h*f2;
f3 = F(x0+h,y0+k3);
k4 = h*f3;
y1 = y0 + ( k1 + 2*k2 + 2*k3 + k4)/6;
printf("\n\n k1 = %.4lf ",k1);
printf("\n\n k2 = %.4lf ",k2);
printf("\n\n k3 = %.41f ",k3);
printf("\n\n k4 = %.41f ",k4);
printf("\n\n y(%.4lf) = %.3lf ",x0+h,y1);
y0=y1;
}
getch();
}

Output:
Enter the value of x0: 0
Enter the value of y0: 1
Enter the value of h: 0.2
Enter the value of last point: 0.2
k1 = 0.2000
k2 = 0.2400
k3 = 0.24399999999999999000000000000000000000000
k4 = 0.28880000000000000000000000000000000000000
y(0.2000) = 1.243

Euler Method
#include<stdio.h>
#include <math.h>
#include<conio.h>
#define F(x,y) (x)+(y)
void main()
{
double y1,y2,x1,a,n,h;
int j;
printf("\nEnter the value of range: ");
scanf("%lf %lf",&a,&n);
printf("\nEnter the value of y1: ");
scanf("%lf",&y1);
printf("\n\nEnter the h: ");
scanf("%lf",&h);
printf("\n\n y1 = %.3lf ",y1);
for(x1=a,j=2; x1<=n-h; x1=x1+h,j++)
{
y2= y1 + h * (x1+y1);
printf("\n\n x = %.3lf => y%d = %.3lf ",x1,j,y2);
y1=y2;
}
}

Output:

Enter the value of range: 0 1


Enter the value of y1: 1
Enter the h: 0.1
y1 = 1.000
x = 0.000 => y2 = 1.100
x = 0.100 => y3 = 1.220
x = 0.200 => y4 = 1.362
x = 0.300 => y5 = 1.528
x = 0.400 => y6 = 1.721
x = 0.500 => y7 = 1.943
x = 0.600 => y8 = 2.197
x = 0.700 => y9 = 2.487
x = 0.800 => y10 = 2.816
x = 0.900 => y11 = 3.187

You might also like