F How can you implement Trapezoidal Rule in numerical integration? | CodeTheta

How can you implement Trapezoidal Rule in numerical integration?

January 17, 2014


/******************************************

      INTEGRATION BY TRAPEZOIDAL RULE

      http://native-code.blogspot.com

 ******************************************/

#include<stdio.h>
#include<conio.h>
#define MAX 20
#define f(x) (x)
void main()
{
   float a,b,h,i,k;
   float x[MAX],y[MAX];
   int n;
     clrscr();
   //--------------------------
   printf("\nEnter the limits:");
   scanf("%f %f",&a,&b);
  
   printf("\nEnter the number of subintervals:");
   scanf("%d",&n);
   //--------------------------
   h=(b-a)/n;
   for(k=a;k<=b;k+=h)
   {
     if((k==a)||(k==b))
        i+=f(k);
     else
        i+=2*f(k);
   }
   i=(h/2)*(i);
   //--------------------------

   printf("\n\n\nRESULT BY TRAPEZOIDAL RULE:%f",i);
   getch();

} // end of main


/* http://native-code.blogspot.com */

Post a Comment