Trapezoidal rule

%       The derivation of the trapezoidal rule.
%       If you want to run other graphs after having used this
%       file, you may have to enter the command CLF.

x  = 0: pi/90: pi/2;    % abscissa of integrand
y  = 1+ sin(x);         % integrand
xs = 0: pi/8: pi/2;     % abscissa of substitute integrand
ys = 1 + sin(xs);       % substitute integrand
a = [ 0 1.6 ]; b = [ 0 0 ];
x0 = [ 0 0 ]; y0 = [ 0 1 ];
x1 = [ pi/8 pi/8 ];     % second ordinate
y1 = [ 0 (1 + sin(pi/8)) ];   % second evaluation point
x2 = [ pi/4 pi/4 ];     % third ordinate
y2 = [ 0 (1 + sin(pi/4)) ];   % third evaluation point
x3 = [ 3*pi/8 3*pi/8 ]; % fourth ordinate
y3 = [ 0 (1 + sin(3*pi/8)) ]; % fourth evaluation point
x4 = [ pi/2 pi/2 ];     % fifth ordinate
y4 = [ 0 (1 + sin(pi/2)) ];   % fifth evaluation point
hold on
plot(x,y,’-‘,xs,ys,’–‘,x0, y0,’:g’,x1,y1,’:g’,x2,y2,’:g’,x3,y3,’:g’,x4,y4,’:g’)
plot(a, b, ‘-‘)
axis(‘off’)
xlabel(‘x’)
text(xs(1)-0.01, -0.05, ‘x0’)
text(xs(1)+0.01, ys(1)-0.1, ‘f(x0)’)
text(xs(2)+0.01, -0.05, ‘x1’)
text(xs(2)+0.01, ys(2)-0.1, ‘f(x1)’)
text(xs(3)+0.01, -0.05, ‘x2’)
text(xs(3)+0.01, ys(3)-0.1, ‘f(x2)’)
text(xs(4)+0.01, -0.05, ‘x3’)
text(xs(4)+0.01, ys(4)-0.1, ‘f(x3)’)
text(xs(5)-0.05, -0.05, ‘x4’)
text(xs(5)-0.10, ys(5)-0.1, ‘f(x4)’)
text((xs(3) + xs(4))/2, (ys(3) + ys(4))/2 + 0.05, ‘f(x)’)
text(0.2, 0.4, ‘The continous line represents f(x)’)
text(0.2, 0.2, ‘The dotted line represents substitution by trapezoids’)
hold off