clear all

a = 0;
b = 10;
dxf = 0.01;
xf = a:dxf:b;
yf = sin(xf);
plot(xf,yf)

dxc = 0.5;
xc = a:dxc:b;
yc = sin(xc);
hold on
stairs(xc,yc,'r')

n = length(xc);

% left-rectangle rule
area1 = 0;
for i=1:n-1  % number of rectangles
    area1 = area1 + yc(i)*dxc;
end
area1

% right-rectangle rule
area2 = 0;
for i=1:n-1  % number of rectangles
    area2 = area2 + yc(i+1)*dxc;
end
area2

% trapezoid rule
area3 = 0;
for i=1:n-1
    area3 = area3 + (dxc/2)*(yc(i)+yc(i+1));
end
area3


% we can also use built in matlab functions
area1 = sum(yc(1:end-1))*dxc;
area2 = sum(yc(2:end))*dxc;
area3 = trapz(xc,yc);
area3 = trapz(yc)*dxc;

% we can also figure out better estimate using fine resolution data
area1f = sum(yf(1:end-1))*dxf;
area2f = sum(yf(2:end))*dxf;
area3f = trapz(xf,yf);
area3f = trapz(yf)*dxf;










