

SIMPSON'S ONE-THIRD RULE
//Solve by Simpson's 1/3 rd rule of integration [ e^(atan(x)) ]
#include<stdio.h>
#include<math.h>
double f(double x)
{
return (exp(atan(x)));
}
int main()
{
double a,b,x[50],y[50],h,r,s0=0.0,s1=0.0,s2=0.0;
int n,i;
printf("Enter the lower limit: ");
scanf("%lf",&a);
printf("\nEnter the upper limit: ");
scanf("%lf",&b);
while(1)
{
printf("\nEnter the number of sub intervals: ");
scanf("%d",&n);
if(n%2==0)
break;
else
printf("\nThe number of sub-intervals should be even.\n");
}
h=(b-a)/n;
x[0]=a;
x[n]=b;
y[0]=f(x[0]);
y[n]=f(x[n]);
s0=y[0]+y[n];
for(i=1;i<n;i+=2)
{
x[i]=x[0]+i*h;
y[i]=f(x[i]);
s1=s1+y[i];
}
for(i=2;i<n;i+=2)
{
x[i]=x[0]+i*h;
y[i]=f(x[i]);
s2=s2+y[i];
}
printf("\nComputation table:-\n");
printf("\n| i |\t| Xi=Xo+ih |\t| Yi=f(x),i=0,%d |\t|Yi=f(x),i=1,3,..|\t|Yi=f(x),i=2,4,..|\n\n",n);
for(i=0;i<=n;i++)
{
printf("| %2d |\t| %3.2lf |",i,x[i]);
if(i==0||i==n)
printf("\t| %3.5lf\t |",y[i]);
else
printf("\t|\t\t |");
if(i==0||i==n)
{
printf("\t|\t\t |\t|\t\t |\n");
continue;
}
if(i%2==0)
printf("\t|\t\t |\t| %5.5lf\t |\n",y[i]);
else
printf("\t| %5.5lf\t |\t|\t\t |\n",y[i]);
}
printf("\n| sum |\t| \t |\t| %5.5f\t |\t| %5.5lf\t |\t| %5.5lf\t |\n",s0,s1,s2);
r=(h/3.0)*(s0+(4*s1)+(2*s2));
printf("\n\nThe result is = %5.5lf\n",r);
return 1;
}
