Simson’s one-third formula

Simson’s 1/3rd formula (composite)

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define F(x) 1/(1+x*x)
int main (void)
{
	int n,i;
   float a,b,h,sum1=0.0,sum2=0.0,ics,x,f1,f2,f3,f4;
   printf("Initial value of x: ");
   scanf("%f",&a);
   printf("Final value of x: ");
   scanf("%f",&b);
   printf("Number of segment(even no): ");
   scanf("%d",&n);
   h=(b-a)/n;
   x=a;
   f1=F(x);
   for(i=2;i<=n;i=i+2)
   {
      x=x+h;
   	f2=F(x);
      sum1=sum1+f2;
      x=x+h;
   }
   x=a;
   for(i=3;i<n;i=i+2)
   {
      x=x+2*h;
   	f3=F(x);
      sum2=sum2+f3;
   }
   x=b;
   f4=F(x);
   ics=((4*sum1+2*sum2+f1+f4)*h)/3.0;
   printf("\nIntegral from %f to %f , \nwhen h = %f is %f",a,b,h,ics);
   getch();
   return 0;
}

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *