Trapezoidal Formula

Trapezoidal 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,ics,x,f1,f2,f4;
   printf("Initial value of x: ");
   scanf("%f",&a);
   printf("Final value of x: ");
   scanf("%f",&b);
   printf("Number of segment: ");
   scanf("%d",&n);
   h=(b-a)/n;
   x=a;
   f1=F(x);
   for(i=2;i<=n;i++)
   {
      x=x+h;
   	f2=F(x);
      sum1=sum1+f2;
   }
   x=b;
   f4=F(x);
   ics=((2*sum1+f1+f4)*h)/2.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 *