% CLASS12NOV  show built-in numerical quadrature, and trapezoid rule as
%             a dot product

% use built-in on an integral we know how to do by FTC
% DO THIS FOR MORE INFO:  help quad
format long g
quad(@(x) x.^3,0,2)  % using anonymous form

% name f(x) so it is not anonymous
f = @(x) x.^3;
quad(f,0,2)

% use quad on an integral we do not know how to do by FTC
f = @(x) exp(-x.^2);
quad(f,0,1)
quadl(f,0,1)  % a second built-in routine

% 7 point composite trapezoid rule on same integral
a = [1 2 2 2 2 2 1]; % coefficient pattern
h = (1-0)/6;
x = 0:h:1;
y = f(x);
(h/2) * a * y'  % trapezoid = (h/2) Sigma_{i=0}^N a_i f(x_i)

% more points: composite trapezoid
N = 100;  h = (1-0)/N;  x = 0:h:1;
a = [1 2*ones(1,N-1) 1];
y = f(x);
(h/2)*a*y'

% even more points; one hundred million points!; takes about 11 seconds on my laptop
tic
N = 100000000;  h = (1-0)/N;  x = 0:h:1;
a = [1 2*ones(1,N-1) 1];
y = f(x);
(h/2)*a*y'
toc
% note that rounding error apparently does not build up much!

