Weddle’s Rule

Weddle’s Rule

#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,sum3=0.0,sum4=0.0,sum5=0.0,sum6=0.0;
   float ans,x,f1,f2,f3,f4,f5,f6,f7,f8;
   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);            
   x=x+h;
   for(i=2;i<=n;i=i+6)
   {
   	f2=F(x);
      sum1=sum1+f2;
      x=x+6*h;
   }
   x=a;
   x=x+2*h;
   for(i=3;i<n;i=i+6)
   {
   	f3=F(x);
      sum2=sum2+f3;
      x=x+6*h;
   }
   x=a;
   x=x+3*h;
   for(i=4;i<n;i=i+6)
   {
   	f4=F(x);
      sum3=sum3+f4;
      x=x+6*h;
   }
   x=a;
   x=x+4*h;
   for(i=5;i<n;i=i+6)
   {
   	f5=F(x);
      sum4=sum4+f5;
      x=x+6*h;
   }
   x=a;
   x=x+5*h;
   for(i=6;i<=n;i=i+6)
   {
   	f6=F(x);
      sum5=sum5+f6;
      x=x+6*h;
   }
   x=a;
   x=x+6*h;
   for(i=7;i<n;i=i+6)
   {
     	f7=F(x);
      sum6=sum6+f7;
      x=x+6*h;
   }
   x=b;
   f8=F(x);
   ans=((f1+5*sum1+sum2+6*sum3+sum4+5*sum5+2*sum6+f8)*3*h)/10.0;
   printf("\n\nIntegral from %f to %f , \nwhen h = %f \nis = %f",a,b,h,ans);
   getch();
   return 0;
}

Related posts:

Leave a Reply

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