A number of definite integrals need to be solved in applied mathematics, physics and engineering. The manual analytical solution of definite integrals is quite cumbersome and time consuming. So, in this post I have presented source code in C program for Trapezoidal method as one of the computer-programming-based solutions of definite integrals. The techniques of numerical methods are used to solve this equation; it involves a number of calculations and efforts have been made to minimize error in the program.

The trapezium or trapezoidal rule can be used as a way of estimating the area under a curve because the area under a curve is given by integration. So, the trapezoidal rule gives a method of estimating integrals. This is useful when you come across integrals that you don’t know how to evaluate. So, the** **program for trapezoidal method in C given here is applicable to calculate finite integral or area under a curve.

The basic working principle of the trapezoidal method c program is that trapezoidal method splits the area under the curve into a number of trapeziums. Then, area of each trapezium is calculated, and all the individual areas are summed up to give the total area under the curve which is the value of definite integral.

In the above figure, area under the curve between the points x_{0} and x_{n} is to be determined. In order to determine the area, we divide the total interval (x_{n}– x_{0}) into ‘n’ small interval each of length ‘h’:

h=(x_{n}– x_{0})/n

After that, the C source code for trapezoidal method uses the following formula to calculate the value of definite integral:

Source Code for Trapezoidal Method in C:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include<stdio.h> #include<conio.h> #include<math.h> float f(float x) { return(1/(1+pow(x,2))); } void main() { int i,n; float x0,xn,h,y[20],so,se,ans,x[20]; printf("\n Enter values of x0,xn,h:\n"); scanf("%f%f%f",&x0,&xn,&h); n=(xn-x0)/h; if(n%2==1) { n=n+1; } h=(xn-x0)/n; printf("\nrefined value of n and h are:%d %f\n",n,h); printf("\n Y values \n"); for(i=0; i<=n; i++) { x[i]=x0+i*h; y[i]=f(x[i]); printf("\n%f\n",y[i]); } so=0; se=0; for(i=1; i<n; i++) { if(i%2==1) { so=so+y[i]; } else { se=se+y[i]; } } ans=h/3*(y[0]+y[n]+4*so+2*se); printf("\nfinal integration is %f",ans); getch(); } |

The aforementioned source code for trapezoidal method is short and simple to understand. It uses a user defined function to calculate the value of function i.e f(x) =1 /(1 + x^{2}). The variable data type used in the program are integer and float types. There are only three variables to be input to the program – x_{0} ( initial boundary value), x_{n }( final boundary value) and h (strip length).

**Input/Output:**

Also see,

Trapezoidal Method MATLAB Program

Trapezoidal Method Algorithm/Flowchart

Numerical Methods Tutorial Compilation

As the C program for Trapezoidal Method is executed, it asks for the value of x_{0}, x_{n }and h. After inputting them, it prints the refined value of n & h, and value of each ‘y’ at each intermediate points as shown in the output screen above. At the end, it prints the value of the define integral. To use this source code for other functions, just modify the *return(1/(1+pow(x,2)));* part in the code.

Trapezoidal rule function, since this one is wrong.

a and b bound f(x). n is the number of strips.

typedef double (*CurveFunction)(double);

double trap(double a, double b, int n, CurveFunction f)

{

double area = 0;

double sum = f(a) + f(b);

double h = (b – a) / n;

for (int x = 1; x < n; ++x) {

sum += f(x * h + a);

}

area = h * sum / 2.0;

return area;

}

this is coding for composite simpson’s 1/3 rule

Sorry but isn’t the formula for trapezoidal integration

s=h/2(y0+yn+2 (sum of rest of terms)

what’s written is Simpsons 1/3 rule right?