Simpson 3/8 Rule Method in C

Simpson's rule is a Newton-Cotes formula for approximating the integral of a function f using quadratic polynomials (i.e., parabolic arcs instead of the straight line segments used in the trapezoidal rule. Simpson's rule can be derived by integrating a third-order Lagrange interpolating polynomial fit to the function at three equally spaced points.

Program of Simpson 3/8 Rule in C


   #include<stdio.h>
   #include<conio.h>
   #include<math.h>
   #define f(x) 1/(1+x);

   void main()
   {
    int n,i;
    float a,b,s,s1=0,s2=0,u,h,y;
    clrscr();
    
    printf("Enter the uper limit: ");
    scanf("%f",&a);
    printf("Enter the lower limit: ");
    scanf("%f",&b);
    printf("Enter the interval: ");
    scanf("%d",&n);
    h=(a-b)/n;
    s=f(a)+f(b);
    for(i=1;i<=n-1;i++)
    { if(i%3==0)
        {
            s1=s1+2*f(a+(i*h));
        }
        else
        {
            s2=s2+2*f(a+(i*h));
        }
        u=((3*h)/8)*(s+s1+s2);
    }
    printf("Answer is: %f",u);
    
    getch();
}
  

Output

simpson 3/8 method

Post Your Comment