Trapezoidal Rule – Numerical Integration with MATLAB

Trapezoidal rule is a numerical tool for the solving of definite integral. This rule based on computing the area of trapezium. Trapezoidal rule is applicable for all number of interval whether n is even or odd. The large number of interval give the best result compare than small number of interval. At here, we write the code of Trapezoidal Rule in MATLAB step by step. MATLAB is easy way to solve complicated problems that are not solve by hand or impossible to solve at page. MATLAB is develop for mathematics, therefore MATLAB is the abbreviation of MATrix LABoratory.

In this program, we evaluate the integral

\int_{a}^{b}\frac{1}{1+x^2}dx

The formula of composite trapezoidal rule is

\int_{a}^{b}f(x)dx=\frac{h}{2}(f(a)+2\sum_{i=1}^{n-1}f(x_i)+f(b))

% Numerical Analysis Trapezoidal Rule using MATLAB
clear all;
close all;
clc;

f=inline('1/(1+x^2)');

a=input('Enter lower limit of integral=');
b=input('Enter upper limit of integral=');
n=input('Enter number of intervals=');

h=(b-a)/n;

sum=0.0;

for i=1:n-1
    x=a+i*h;
    sum=sum+f(x);
end

trap=h*(f(a)+2*sum+f(b))/2.0;
fprintf('Evaluated Integral =%f',trap);