fprintf('\n Math 240-002 * Spring 2005 * Final Exam * Problem 3\n\n');
fprintf('   N   |   Integral   |  Error   | log(Error)  \n');
fprintf('______________________________________________\n');

f = inline('sqrt(x)');
error(1:9) = 0;

for m = 1 : 9
    N = 3^m;
    I = rule3_8(f, 0, 1, N);
    error(m) = abs(I - 2/3);
    fprintf(' %5d | %12.10g | %.2e | %.2e \n', N, I, error(m), log(error(m)) );
end

slope = (log(error(9)) - log(error(8))) / (log(3^9) - log(3^8))
yLine = log(error(9)) + slope * (log(3.^(1:9)) - log(3^9));

plot(log(3.^(1:9)), log(error), 'ob', log(3.^(1:9)), yLine, '-r');
axis tight; xlabel('ln(N)'); ylabel('ln(error)');
title('3/8 Integration rule - error convergence');
legend('Error', 'Linear fit', 'location', 'best');