Nov 18, 2008

Simpson's 1/3 Rule

Q.Integration of 1/(1+x^2) within the limit (1,6) assuming h=1



#include<stdio.h>

float f(float a)
{
float b;
b=(1/(1+(a*a)));
return b;
}

int main()
{
int i,m=0,n=6;
float y[7],e=0.0,o=0.0,w,h=1.0;
for(i=m;i<=n;i++)
{
y[i]=f(i);
printf("x=%d,y[%d]=%f\n",i,i,y[i]);
}

for(i=1;i<=n-1;i=i+2)
{
o=o+y[i];
}

for(i=2;i<=n-1;i=i+2)
{
e=e+y[i];
}
printf("E=%f, O=%f",e,o);
w=((y[0]+y[n])+4*o+2*e)*(h/3);
printf("\nAns= %f\n",w);
return 0;
}

0 comments: