irrarea.m
function [a, xbar, ybar] = irrarea(D)
% Computes area of irregular closed contour given by coordinates
% stored in array D. The first column of D contains the x-coordinate
% and the second column contains the y-coordinates.
%
% Note: the first point will automatically be duplicated as the last
%
% USAGE: area = irrarea(D)
x = D(:,1);
y = D(:,2);
nm = length(x);
n = nm+1;
x(n) = x(1);
y(n) = y(1);
sum = 0;
sumx = 0;
sumy = 0;
for i=1:nm
dA = (x(i)*y(i+1) - x(i+1)*y(i))/2;
sum = sum + dA;
sumy = sumy + (y(i)+y(i+1)) * dA / 3;
sumx = sumx + (x(i)+x(i+1)) * dA / 3;
end
a = sum;
xbar = sumx / a;
ybar = sumy / a;
plot(x,y,xbar,ybar,'r+')