# Trapezoidal Method MATLAB Program

0
5764

Trapezoidal method, also known as trapezium method or simply trapezoidal rule, is a popular method for numerical integration of various functions (approximation of definite integrals) that arise in science and engineering. This method is mainly applicable to estimate the area under a curve by splitting the entire area into a number of trapeziums of known area.

In this tutorial, we are going to write a program code for Trapezoidal method in MATLAB, going through its mathematical derivation and a numerical example. You can check out our earlier tutorials where we discussed a C program and algorithm/flowchart for this method.

## Derivation of Trapezoidal Method:

Consider a curve f(x) = 0 as shown below: Here, the area under the curve f(x) from x0 to xn is to be determined which is theoretically nothing but the numerical integration of curve f(x) in the interval [x0, xn]. So, in order to estimate the area, we split the whole area into ‘n’ number of trapeziums or trapezoids of equal height ‘h’.

Let y0, y1, y2….. yn be the ordinates of the curve at various abscissas.

The area of first trapezium, a1 = h * (y0 +y1)/ 2
The area of second trapezium, a2 = h * (y1 +y2)
The area of third trapezium, a3 = h * (y2 +y3)/ 2
… . . . . .  . . . . . . . . . . . . . . .. . . . .  . . . . . . ..  . ..  . .
. . . ..  . . . ..  . . . ..  . . . ..  . . . . . ..  ..  . . . .. . . .  ..  . .
The area of nth trapezium, an = h * (yn-1 +yn)/ 2

The total area under the curve is the summation of all these small areas.

So, A = a1 + a2 + a3 + …..  + an
or, A= h * (y0 +y1)/ 2 + h * (y1 +y2) + h * (y2 +y3)/ 2+ ….. + h * (yn-1 +yn)/ 2
or, A= h { (y0 + yn)/2 + ( y1 + y2+ …….. + yn-1)}

Note: This formula is the required expression which will be used in the program code for Trapezoidal rule in MATLAB.

Alternatively, it can be concluded that: ## Trapezoidal Method in MATLAB:

The above code for Trapezoidal method in MATLAB has been programmed to find the area under the curve f(x) = x2 in the interval [1, 2]. So, the user doesn’t need to give any input to the program.

In this program, the whole area has been divided into five trapeziums. The program calculates the area of each trapezium as explained in the derivation and finds the required sum.

If it is intended to find the area under other curves using this code, the user has to change the limits and the value of the function in the source code.

To run the program, copy the given code in MATLAB command window and hit enter. Here’s a screenshot sample of the output you’ll get: As the aforementioned MATLAB code has an inbuilt function, it may be difficult (for some of you) to modify the source code for other functions and limits. In such case, you can use this program code instead:

When this MATLAB program is executed, it asks for the value of the interval in which the area of curve is to be estimated. After that, the user needs to input allowed error in calculation. Finally, the area under the curve is displayed as output.

How to input the function?

To run the code, follow these steps:

• Copy the source code given above in MATLAB editor and save as file_mam file.
• Create another MATLAB file in the same folder in which .m is saved.
• In this newly created MATLAB file, define the function following the syntax given below.

The sample output of this program is given below: ## Trapezoidal Method Numerical Example:

Now, let’s analyze numerically the first program for Trapezoidal method in MATLAB. We have to find the area under the curve f(x) = x2 in the interval [1, 2].

Given function, f(x) = x2
x0 =  1 and xn = 2

Take number of steps, n = 5 such that the height of the interval, h = ( 2-1)/5 = 0.2. Detailed calculation is presented in the table below: Now, using the formula for trapezoidal method:

Area = 0.2/2 * [ (1+4) + 2*( 1.44 + 1.96 + 2.56 + 3.24)] = 2.3438

Thus, the answer is same as the one obtained using the program for Trapezoidal method in MATLAB.

If you have any questions regarding trapezoidal method or its MATLAB code, bring them up to me from the comments box below. Also, you can find more Numerical Methods tutorials here.