Write a c program to find integral of x^2 using trapezoidal/rule
Answers
Trapezoidal Rule – C Program. Trapezoidal Rule is a Numerical technique to find the definite integral of a function. ... Then the area of trapeziums is calculated to find the integral which is basically the area under the curve. THe more is the number of trapeziums used, the better is the approximation.A
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(1/(1+pow(x,2)));
}
void main()
{
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
printf("\n Enter values of x0,xn,h:\n");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf("\nrefined value of n and h are:%d %f\n",n,h);
printf("\n Y values \n");
for(i=0; i<=n; i++)
{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf("\n%f\n",y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
so=so+y[i];
}
else
{
se=se+y[i];
}
}
ans=h/3*(y[0]+y[n]+4*so+2*se);
printf("\nfinal integration is %f",ans);
getch();
}