% EASYINTEGRALS  Do some integrals for Assignment # 7.

% problem 1a
format long g
fa = @(x) cos(x).^2;
trap1a = (0.5/2) * (fa(0) + fa(0.5))                  % Trapezoid
simp1a = (0.25/3) * (fa(0) + 4 * fa(0.25) + fa(0.5))  % Simpson's
mid1a = (0.5) * fa(0.25)                             % Midpoint
exact1a = 0.460367746

% problem 1b; same order of rules
fb = @(x) x .* log(1+x);
trap1b = (0.5/2) * (fb(-0.5) + fb(0))
simp1b = (0.25/3) * (fb(-0.5) + 4 * fb(-0.25) + fb(0))
mid1b = (0.5) * fb(-0.25)

% problem 2 actual errors
trapactualerr2 = abs(trap1a - exact1a)
simpactualerr2 = abs(simp1a - exact1a)
midactualerr2  = abs(mid1a - exact1a)

% problem 5; composite Trapezoid and Simpson's, implemented using loops
n = 10;  h = 0.5 / n;
sum5T = fa(0);
sum5S = sum5T;
for j = 1:n-1
  x = 0 + j * h;
  sum5T = sum5T + 2 * fa(x);
  if abs(mod(j,2)) < 0.5,  sum5S = sum5S + 2 * fa(x);
  else,                    sum5S = sum5S + 4 * fa(x);   end
end
sum5T = sum5T + fa(0.5);
sum5S = sum5S + fa(0.5);
comptrap5 = (h/2) * sum5T
compsimp5 = (h/3) * sum5S
trapactualerr5 = abs(comptrap5 - exact1a)
simpactualerr5 = abs(compsimp5 - exact1a)
