Nov 18, 2008

Simpson's 3/8 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],t=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++)
{
if(i%3==0) continue;
else o=o+y[i];
}

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

0 comments: