Friday, April 10, 2015

Simpson's 3/8 Rule in C program

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) 1/x
 void main()
{
float a,b,h,s,s1,I,x[30],f[30];
int i,n;
clrscr();
printf("\n Enter lower & upper limit");
scanf("%f %f",&a,&b);
printf("Enter the no. of stripes");
scanf("%d",&n);
h=(b-a)/n;
printf("step size =%f",h);
for(i=0;i<=n;i++)
{
x[i]=a;
f[i]=f(a);
a=a+h;
}
s=0.0;
for(i=3;i<=n-1;i=i+3)
{
s=s+f[i];
}
s1=0.0;
for(i=1;i<=n-1;i++)
{
if(i%3!=0)
s1=s1+f[i];
}
I=(3*h*(f[0]+f[n]+2.0*s+3.0*s1))/8;
printf("\n Integration value =%f",I);
getch();
}

No comments:

Post a Comment