F Simpson's 1/3 Rule In C | CodeTheta

Simpson's 1/3 Rule In C

May 08, 2014

This is Simpson's 1/3 rule Program which is written in c programming. In this code you can see that we use math function to integrate the mathematical equation and all variable is in float value. At first moment  system will ask you to enter the limits then it will will ask you to enter no of strips after that the result will display.
The main mechanism in this small code:
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));
 

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


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

     Simpson's 1/3 Rule

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

#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;
   printf("http://native-code.blogspot.com");
   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();

}
   

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

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

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us

Post a Comment