Simpson Method in C | Simpson Rules in C

Simpson Method in C


Simpson 1/3 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 1/3 Rule in C

   #include<stdio.h>
   #include<conio.h>
   #include<math.h>
   
   float func(float x)
   {
   float a;
   a=2*pow(x,2)+3*x;
   return (a);
   }
   void main()
   {
    int n,i;
    float a,b,h,sum,integral;
    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;
    sum=func(a)+func(b)+4*func(b-h);
    for(i=1;i<=n-3;i=i+2)
    {