Translate

Labels

Sunday 16 June 2013

PROGRAM TO FIND INTEGRAL VALUE USING SIMPSON'S RULE

#define f(x) 1.0/(1+x*x)
main()
{
int n;
float simps(float,float,int)a,b;
printf("Enter the values of a,b and n\n");
scanf("%f%f%d",&a,&b,&n);
printf("Integral value of f(x) by simpson's 1/3 rule:%f\n",simps(a,b,n));
}

float simps(float a,float b,int n)
{
float x,y[21],h,sum;
int i;
h=(b-a)/n;
x=a;
for(i=0;i<=n;i++)
{
y[i]=f(x);
x=x+h;
}
sum=y[0]+y[n];
for(i=1;i<=n-1;i+=2)
sum+=4*y[i]+2*y[i+1];
sum-=2*y[n];
sum=sum*h/3.0;
return sum;
}

OUTPUT:
Enter the value of a,b and n
0     1     10
Integral value f(x) by simpson's 1/3 rule:0.785398

No comments: