Write a program to find the integral of a function using Trapezoidal rule.

#include<stdio.h>
#include<conio.h>
#include<math.h>
float trap(float,float,int);
int i;
float sum;
float fun(float);
void main()
{
 float x0,x1,res;
 int n;
 clrscr();
 printf("\n enter the lower and upper limits :");
 scanf("%f%f",&x0,&x1);
 printf("\n enter the number of intervals :");
 scanf("%d",&n);
 res=trap(x0,x1,n);
 printf("\n TRAPEZOIDAL RULE =%f \n",res);
 getch();
 }
 float trap(float x0,float x1,int n)
 {
  float h,result;
  h=(x1-x0)/n;
  sum=fun(x0)+fun(x1);
  for(i=1;i<n;i++)
  sum=sum+2*fun(i*h);
  result=(sum*h)/2;
  return(result);
  }
  float fun(float x)
  {
   return(1/(1+pow(x,2)));
  }

No comments:

Post a Comment