C Program For Trapezoidal Method

In mathematics, the trapezoidal method, also known as the trapezoid rule or trapezium rule is a technique for approximating the definite integral in numerical analysis. The trapezoidal method is an integration rule used to calculate the area under a curve by dividing the curve into small trapezoids. The summation of all the areas of the small trapezoids will give the area under the curve.

The trapezoidal method is applied to solve the definite integral of form ba f(x) dx, by approximating the region under the graph of the function f(x) as a trapezoid and calculating its area. Under the trapezoidal rule, we evaluate the area under a curve by dividing the total area into little trapezoids rather than rectangles.

Trapezoidal Method Formula

Trapezoidal Method Formula

Trapezoidal Method Solved Example

Trapezoidal Method Solved Example

C Program

//Trapezoidal Method
//This code is written by Souvik Ghosh
#include<stdio.h>
#include<math.h>

/* Define the function to be integrated here: */
double f(double x){
  return (x/(x+1));
}

/*Program begins*/
void main(){
  int n,i;
  double a,b,h,x,sum=0,integral,e=0;
  /*Ask the user for necessary input */
  printf("Enter the no. of sub-intervals(n): ");
  scanf("%d",&n);
  printf("\nEnter the initial limit(a): ");
  scanf("%lf",&a);
  printf("\nEnter the final limit(b): ");
  scanf("%lf",&b);
  /*Begin Trapezoidal Method: */
  h=fabs(b-a)/n;
  for(i=1;i<=n-1;i++){
    x=a+i*h;
    sum=sum+f(x);
  }
  integral=(h/2)*(f(a)+f(b)+2*sum);

  /*Print the answer */
  printf("\nThe answer is: %0.4f\n",integral);


}

Output Terminal

Trapezoidal Method Program Output

Related Posts

CSS Cheatsheet

Transform your web design game with my CSS cheatsheet! Master the art of styling, dive into essential properties and selectors, and create visually stunning websites effortlessly. Whether…

HTML Cheatsheet

Unleash your web development potential with my HTML cheatsheet! Elevate your coding skills, master essential tags and attributes, and build stunning websites with ease. Whether you’re a…

Git&GitHub Cheatsheet

Elevate your Git and GitHub game with my ultimate cheatsheet! Unlock the power of version control, streamline collaboration, and boost productivity. Navigate the complexities of Git commands…

Command Line Cheatsheet

Master the Command Line effortlessly with my comprehensive cheatsheet! Streamline your workflow, boost productivity, and become a command line guru. Unlock essential commands, shortcuts, and tips to…

OpenAI’s APIs Notes

Unlock the potential of OpenAI’s APIs with my comprehensive notes! Dive into the world of cutting-edge artificial intelligence, explore the capabilities of GPT-4 and beyond, and learn…

SQL Notes

Discover the essence of SQL with my comprehensive notes! Dive into the world of Structured Query Language, unraveling the complexities, and mastering database management. Elevate your SQL…

Leave a Reply

Your email address will not be published. Required fields are marked *