Write a program to demonstrate Trapezoidal rule in C language.

Tested on: Turbo C++ Version 4.0Time Complexity: O (n)Space Complexity: O (1)Open Source: MIT
1
2
            
3
        /* Program to execute trapezoidal rule*/
4
	#include<stdio.h>
5
	#include<conio.h>
6
	#include<math.h>
7
	float calc ( float );
8
	void main()
9
	{
10
	    float x0,xn,h,y;
11
	    int i,sn;
12
	    printf("Enter values of  x0,xn and number of sub-intervals: ");
13
	    scanf("%f%f%d", &x0, &xn, &sn);
14
	    h=(xn-x0)/sn;
15
	    y=calc(x0) + calc(xn);
16
	    for(i=1; i<sn; i++)
17
	        y +=2*calc(x0+i*h);
18
	    y=(h/2)*y;
19
	    printf("Value of integral is %f",y);
20
	    getch();
21
	}
22
	float calc(float x)
23
	{
24
	    return 1/(1+x*x);
25
	}
26
	
27
28

Output

1
2
3
        Enter values of  x0,xn and number of sub-intervals: 0 6 6
4
	Value of integral is 1.410799
5
            
6
    

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

How to run Trapezoidal Rule program in C-Language?

To run Trapezoidal Rule program in C-Language, follow the below mentioned steps:

  1. Copy the Code: Press the copy button in the code block to copy the complete code.
  2. Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
  3. Paste the code: Paste the copied code into Turbo C++’s editor.
  4. Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
  5. Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
  6. Enter the Input: Provide the required inputs or your desired inputs, if applicable.
  7. Receive the Result: Get the result of the program on the screen based on the provided inputs.

More about the Trapezoidal Rule

Discovered by: Babylonian astronomers

Discovered Year: Circa 200 BCE to 50 BCE

Modified/Formalized by: James Gregory and Roger Cotes (as part of the Newton-Cotes formulas) in 1668 (Gregory) and 1714 (Cotes)

The area under the curve is calculated with the help of the Trapezoidal Rule method where direct integration is not possible.

How does the Trapezoidal Rule Method Work?

  1. The curve area is divided into small trapezoidal parts (that means the area under the curve is divided into small areas)
  2. Linear approximation (straight-line segments) is drawn to each small trapezoidal part to connect all these areas.
  3. Calculate the complete area with the help of the below-mentioned formula

    Image
    Where h is the interval gap of integration and y is the height
    Image

Why is the Trapezoidal Rule used?

  • When you have an equation whose integration formula is very complex.
  • If you have just experimental data (number table) and want to find out the total quantity, then the trapezoidal formula is more suitable.

Real-Life Applications of Trapezoidal Rule

  1. Earthwork calculations in Civil Engineering & Surveys: To find out the cover area for digging or filling with the sand then Trapezoidal Rule is used because it gives the estimate area to cover even the area is in irregular shape.
  2. Physics & sensor Data (Time Graph or speed): To find out the distance of rocket or other vehicles at the basis of speed-data this method is used.
  3. Finance: Sometimes the cash flow or economic curve occurs irregularly; then, to find out the area under the curve, the Trapezoidal Rule is used.

Limitations of Trapezoidal Rule

  • Lack of Accuracy: The Trapezoidal Rule method connects the boundaries with a straight line; hence, if the curve is very highly bent, it causes error or deviation
  • Need a very big table to get a precise result: If you want to get a precise result, then you have to divide the curve area into very small parts and calculate all the small parts; that is why you need a very big table.

Alternate Methods

  • Simpson’s 1/3 and Simpson’s 3/8 Rule: These methods use parabolic arcs (curves) instead of a straight line to find the area under the curve.
  • Gaussian Quadrature: Used to find out the extreme high precision at the basis of less points.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.