C Program To Solve Trapezoidal Rule
Learn How To Implement Trapezoidal Rule in C Programming Language. Find out the easiest way to write a C Program for Trapezium Rule Implementation. It is a method to approximately calculate the definite integral.
Trapezium Rule Formula
This is a technique to approximate the definite integrals. It approximates the region under the graph of the function f(x) and calculates its area. This method becomes more accurate and outputs perfect results when periodic functions are integrated over their periods.
Must Read: C Program For Simpson’s Rule
If you try to compile this C Program For Trapezium Rule in Linux, you will get the following error:
/tmp/ccgL393M.o: In function `trapezoidal_rule':
test.c:(.text+0x13): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
This is because the pow() method cannot be found in the library files. To overcome this error, you will have to explicitly include the math.h header file. Compile the program using the following command:
gcc test.c -lm
C Program To Solve Trapezoidal Rule using Function
float trapezoidal_rule(float x)
int count, interval;
float lower_limit, upper_limit, result = 0, sum = 0, length;
printf("\nEnter the Intervals:\t");
printf("\nEnter Lower Limit:\t");
printf("\nEnter Upper Limit:\t");
length = (upper_limit - lower_limit) / interval;
for(count = 1; count <= interval - 1; count++)
sum = sum + trapezoidal_rule(lower_limit + count*length);
result = (trapezoidal_rule(lower_limit) + trapezoidal_rule(upper_limit) + 2*sum) * (length/2);
printf("\nTrapezoidal Rule Value:\t%f\n", result);
Must Read: C Program To Display India’s Map
If you have any compilation errors or doubts in this C Program To Implement Trapezoidal Rule, let us know about in the Comment Section below.