#include /* Header for input/output subroutines. */ #include /* Header for math subroutines. */ #include /* Header for floating point subroutines. */ #define pi 3.141592653589793238462643383279 /* Accurate value for pi. */ /* Simpson's rule for approximating integrals. a: left endpoint b: right endpoint fc: pointer to function to integrate n: number of subintervals */ double fc (double x); main() { double a,b,h,sum,x,y; /* In 'C' all variables must be assigned */ double p1, p2, p3; int i, n; printf("\007"); /* Sound bell in computer. */ a = (double) 0.0; printf("\nLeft end point = %.16lf",a); b = (double) pi/2.0; printf("\nRight end point = %.16lf",b); i = -1; while (i < 0){ printf("\nEnter number of subintervals (must be even) "); scanf("%d",&n); i = n/2; i = 2*i - n; /* Don't allow odd values of n. */ if (n<=0) i = -1; /* Don't allow zero or negative. */ } printf("\nNumber of subintervals %d",n); h = (double) (b-a)/n; for (i=1, sum=0.0; i<=n; i = i+ 2){ p1 = fc((double) a+(i-1)*h); p2 = fc((double) a+i*h); p3 = fc((double) a+(i+1)*h); sum += p1 + 4.0 * p2 + p3; /* printf("\n x, f(x) %.16lf %.16lf",a+(i-1)*h,p1); */ /* printf("\n x, f(x) %.16lf %.16lf",a+i*h,p2); */ /* printf("\n x, f(x) %.16lf %.16lf",a+(i+1)*h,p3); */ } printf("\nValue of sum = %.16lf", (double) sum); y = (double) h*sum/3.0; printf("\nValue of integral = %.16lf", (double) y); /* x=(double) 2.0; printf("\nTheoretical Value = %.16lf", (double) log((double) x)); printf("\n"); */ return(0); } double fc (double x) { double y; y = (double) sqrt(1.0-sin(exp(1)/2.)*sin(exp(1)/2)*sin(x)*sin(x)); return (y); } /* End of file */