

TRAPEZOIDAL RULE
//Solve by Trapezoidal method 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;
int n,i;
printf("Enter the lower limit: ");
scanf("%lf",&a);
printf("\nEnter the upper limit: ");
scanf("%lf",&b);
printf("\nEnter the number of sub intervals: ");
scanf("%d",&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++)
{
x[i]=x[0]+i*h;
y[i]=f(x[i]);
s1=s1+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 to %2d |\n\n",n,n-1);
for(i=0;i<=n;i++)
{
printf("| %2d |\t| %5.5lf |",i,x[i]);
if(i==0||i==n)
printf("\t| %5.5lf\t |",y[i]);
else
printf("\t|\t\t |");
if(i==0||i==n)
{
printf("\t|\t\t |\n");
continue;
}
printf("\t| %5.5lf\t |\n",y[i]);
}
printf("\n| sum |\t| \t |\t| %5.5f\t |\t| %5.5lf\t |\n",s0,s1);
r=(h/2.0)*(s0+(2*s1));
printf("\n\nThe result is = %5.5lf\n",r);
return 1;
}
