Translate

Labels

Saturday 15 June 2013

PROGRAM TO FIND THE INTEGRAL VALUE USING TRAPEZOIDAL RULE

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

float trapz(float a,float b,int n)
{
float x,y[51],h;
float 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;i++)
sum+=2*y[i];
sum=sum*h/2;
return sum;
}


OUTPUT:
Enter the values of a,b and n
0     1     10
Integral value of f(x) bu trapezoidal rule:0.785398 

No comments: