/* Simpson's rule */
#include <stdio.h>
#include <math.h>
double f(double x)
{return 4.0/(1.0+x*x);}
int main()
{
int i, n ;
double a=0.0, b=1.0 , h, s1=0.0, s2=0.0, s3=0.0, x;
printf("Enter number of partitions = ");
scanf("%d", &n) ;
h = (b-a)/(2.0*n) ;
s1 = (f(a)+ f(b));
for (i=1; i<2*n; i=i+2) s2 = s2 + f(a + i*h);
for (i=2; i<2*n; i=i+2) s3 = s3 + f(a + i*h);
printf("%20.12lf\n", (h/3.0)*(s1+ 4.0*s2 + 2.0*s3)) ;
return 0;
}
|