Monday, April 8, 2013

Numerical Integraion Using Trapezoidal Rule.

The process of evaluating a definite integral from a set of tabulated values of integrand f(t) is called numerical integration. The following MATLAB code gives a generalized illustration of numerical integration using Trapezoidal rule. The algorithm for the same is given in the comments at the beginning of the program. Try it for your own function.

% Integration by Trapezoidal method
% This example shows how you can do numerical integration by Trapizoidal
% method
% The formula for this method is given as ...
% $$ \int\limits_0^{\frac{2\pi}{\omega}}f(t)dt = \frac{h}{2}\left[f(a) +
% 2\sum_{i = 0}^{i = K - 1}f(x) + f(b)\right]$
%
% where a = lower limit of integration, b = upper limit of integration
%
% $h = \frac{a - b}{K}$, where K = no. intervals under consideration
% Here the function is $ f(t) = \frac{1}{1+x^2} $ Here we are integrating
% from 0 to 6, taking 6
% intervals in between.
 
clc;
clear all;
a = 0;
b = 6;
K = 6;
h = (b - a) / K;
t = 0 : .0001 :6;
f = inline('1./(1+t.^2)', 't')
plot(t, f(t),'r','linewidth',2.5)
hold;
t1 = a : h : b;
stem(t1, f(t1),'m','filled','linewidth',2.5)
grid;
out = 0;
for i = 1 : K - 1
    out = out + f(a+i*h);
end
result = (h / 2) * (f (a) + 2 * (out) + f(b))

Thank You