7.2 Composite Trapezoidal and Simpson’s Rule An intuitive method of finding the area under the curve y = f (x) over [a,b]is. Use the composite trapezoidal rule with 11 sample points to compute an approximation to the integral of f (x) taken over [1,6]. Let’s understand the trapezoidal method in numerical analysis and implement trapezoidal rule in C programming language. What is Trapezoidal Rule? This numerical analysis method is used to approximating the definite integral. The trapezoidal numerical method works on the principle of straight line approximation. The Trapezoidal rule 1, 2, 3.,n segments. The program will also display the true error, the absolute The program will also display the true error, the absolute relative percentage true error, the approximate error, the absolute relative aprroximate percentage.

Definite integral is replica of area under a curve within the certain limit. In order to calculate such area, there have been developed a number of analytical methods but they are time-consuming, laborious and chance of occurrence of error is also high. That is why, techniques of numerical methods are very much popular in calculation numerical integration which can easily be programmed and trapezoidal method is one of them.

In this tutorial, we’re going to discuss a simple algorithm and flowchart for trapezoidal method along with a brief introduction to the method.

Trapezoidal method is based on the principle that the area under the curve which is to be calculated is divided into number of small segments. The bounding curve in the segment is considered to be a straight line as a result the small enclosed area becomes a trapezium.

The area of each small trapezium is calculated and summed up i.e. integrated. This idea is the working mechanism in trapezoidal method algorithm and flowchart, even it source code.

Let us consider a function f(x) representing a curve as shown in above figure. You are to find out the area under the curve from point ‘a’ to ‘b’. In order to do so, divide the distance between ab into a number vertical strips of width ‘h’ so that each strip can be considered as trapezium.

The following formula is used to calculate the area under the curve:


Trapezoidal Method Algorithm:

  • Start
  • Define and Declare function
  • Input initial boundary value, final boundary value and length of interval
  • Calculate number of strips, n = (final boundary value –final boundary value)/length of interval
  • Perform following operation in loop

x[i]=x0+i*h

y[i]=f(x[i])

print y[i]

  • Initialize se=0, s0=0
  • Do the following using loop

If i %2 = 0

So=s0+y[i]

Otherwise

Se=se+y[i]

  • ans= h/3*(y[0]+y[n]+4*so+2*se)
  • print the ans
  • stop

Trapezoidal Method Flowchart:

Also see,
Trapezoidal Method C Program
Simpson 1/3 Rule C Program
Numerical Methods Tutorial Compilation

Among a number of methods for numerical integration, trapezoidal method is the simplest and very popular method which works on the principle of straight line approximation. I hope the algorithm and flowchart presented here will guide you to write source code for the method in any high level language.

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, theprogram for trapezoidal method in C given here is applicable to calculate finite integral or area under a curve.

Matlab Trapezoidal Rule

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.

Simple c program for addition of two numbers

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

h=(xn– x0)/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:

Source code in C Program for Trapezoidal Method

Simple C Program For Atm Transaction

2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
#include<conio.h>
floatf(floatx)
return(1/(1+pow(x,2)));
voidmain()
inti,n;
printf('n Enter values of x0,xn,h:n');
n=(xn-x0)/h;
{
}
printf('nrefined value of n and h are:%d %fn',n,h);
for(i=0;i<=n;i++)
x[i]=x0+i*h;
printf('n%fn',y[i]);
so=0;
for(i=1;i<n;i++)
if(i%21)
so=so+y[i];
else
se=se+y[i];
}
printf('nfinal integration is %f',ans);
}

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 + x2). The variable data type used in the program are integer and float types. There are only three variables to be input to the program – x0 ( initial boundary value), xn ( final boundary value) and h (strip length).

Input/Output:

Simple C Program For Trapezoidal Ruler

Also see,
Trapezoidal Method MATLAB Program
Trapezoidal Method Algorithm/Flowchart
Numerical Methods Tutorial Compilation

Matlab

Simple C Program For Prime Number

As the C program for Trapezoidal Method is executed, it asks for the value of x0, xn 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.