You are on page 1of 7

Comparison among EM, IEM &

RK2
For Approximation of e
(Solution of y’=y for x=1)

CPP4EM
(for h=0.25 and 4 steps)
EM e = 2.44141
Actual e = 2.71828
( %Error = -10.2 % )

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
double x0,y0,h,N,xn,x,y,f;
cout<<"Plz enter the value of x0 = ";
cin>>x0;
cout<<"Plz enter the value of y0 = ";
cin>>y0;
cout<<"Plz enter the value of xn = xn = ";
cin>>xn;
cout<<"Plz enter the step size (Hint: h=(xn-x0)/(total steps N) = h = ";
cin>>h;
N=(xn-x0)/h;
cout<<"Hence number of steps = N = "<<N<<endl;
x=x0;
y=y0;
for(int n=0; n<=N; n=n+1)
{
f=y;
cout<<"\t Solution at x = "<<x<<" is y = "<<y<<endl;
y=y+h*f;
x=x+h;
}
getch();
}
CPP4IEM
(for h=0.25 and 4 steps)
EM e = 2.69486
Actual e = 2.71828
( %Error = -0.86 % )

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
double x0,y0,h,N,xn,x,y,yold,yem,f,fold;
cout<<"Plz enter the value of x0 = ";
cin>>x0;
x=x0;

cout<<"Plz enter the value of y0 = ";


cin>>y0;
y=y0;

cout<<"Plz enter the value of xn = xn = ";


cin>>xn;

cout<<"Plz enter the step size (Hint: h=(xn-x0)/(total steps N) = h = ";


cin>>h;
N=(xn-x0)/h;
cout<<"Hence number of steps = N = "<<N<<endl;

for(int n=0; n<N; n=n+1)


{
yold=y;
f=y;
fold=f;
y=y+h*f;
yem=y;
x=x+h;
f=y;
y=yold+h*(fold+f)/2;
cout<<"At x = "<<x<<" solution is y = "<<y<<endl;
}
getch();
}
CPP4RK2
(for h=0.25 and 4 steps)
EM e = 2.69486
Actual e = 2.71828
( %Error = -0.86 % )

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
double x0,y0,h,N,xn,x,xhalf,yhalf, fhalf,y,f;
cout<<"Plz enter the value of x0 = ";
cin>>x0;
cout<<"Plz enter the value of y0 = ";
cin>>y0;
cout<<"Plz enter the value of xn = xn = ";
cin>>xn;
cout<<"Plz enter the step size (Hint: h=(xn-x0)/(total steps N) = h =
";
cin>>h;
N=(xn-x0)/h;
cout<<"Hence number of steps = N = "<<N<<endl;
x=x0;
y=y0;
for(int n=0; n<N; n=n+1)
{
x=x0+n*h;
f=y;

xhalf=x+h/2;
yhalf=y+(h/2)*f;
fhalf=yhalf;
x=x+h;
y=y+h*fhalf;
cout<<"At x = "<<x<<", y = "<<y<<endl;
}
getch();
}

You might also like