F How can you implement Simpson's 1/3 Rule by numerical integration? | CodeTheta

How can you implement Simpson's 1/3 Rule by numerical integration?

January 17, 2014


/******************************************

     INTEGRATION BY SIMPSON'S 1/3 RULE

      http://native-code.blogspot.com

 ******************************************/

#include<stdio.h>

#include<conio.h>

#include<math.h>

float f(float);

void main()

{
   float a,b,h,i;
   float s1,s2,e;
   int j,n;
   clrscr();

   //--------------------------
   printf("\nEnter the limits:");
   scanf("%f %f",&a,&b);
   do
   { printf("\nEnter number of strips");
     printf(" by which to divide the interval(even):");
     scanf("%d",&n);
   }while(n%2!=0);
   //--------------------------

   h=(b-a)/(float)n;
   e=f(a)+f(b);
   s1=s2=0.0F;
   //--------------------------

   for(j=1;j<n;j++)
   {

     if((j%2)==0)
        s1+=f(a+j*h);
     else
        s2+=f(a+j*h);

   }

   //--------------------------

   i=(h/3)*(e+(s2*4)+(s1*2));
   printf("\n\n\nRESULT BY SIMPSON'S 1/3 RULE:%f",i);
   getch();

} // end of main  

float f(float x)

{
   return(exp(-x*x));
}

/* http://native-code.blogspot.com */ 

Post a Comment