% ex8p15.m
% Combining Archimedes method for approximating
% Pi and Richardson extrapolation.
% See Exercise 8.15 on p.377 Heath.
% Half of the perimeter of an inscribed polygon 
% with n sides: pn = n*sin(pi/n)
% Half of the perimeter of a circumscribed polygon 
% with n sides: qn = n*tan(pi/n)
% RESULT:
% Pip = 3.14107
% Piq = 3.13250
% NOTE: for n = 120, pn = n*sin(pi/n) = 3.14123
%                    qn = n*tan(pi/n) = 3.14231
% K. Ming Leung, 04/07/03

clear all; format long e; format compact;
% parameters for Richardson extrapolation:
q = 2; p = 2;

p6 = 3.0000; p12 = 3.1058;
p6RelError = 1 - p6/pi,
p12RelError = 1 - p12/pi,

a0p = p6+(p6-p12)/(q^(-p)-1),
a0pRelError = 1 - a0p/pi,

q6 = 3.4641; q12 = 3.2154;
q6RelError = 1 - q6/pi,
q12RelError = 1 - q12/pi,

a0q = q6+(q6-q12)/(q^(-p)-1),
a0qRelError = 1 - a0q/pi,

% RESULTS:
%
% p6RelError =
%     4.507034144862798e-002
% p12RelError =
%     1.139315549038289e-002
% a0p =
%     3.141066666666667e+000
% a0pRelError =
%     1.674268376344879e-004
% q6RelError =
%    -1.026572767292693e-001
% q12RelError =
%    -2.349360803536049e-002
% a0q =
%     3.132500000000000e+000
% a0qRelError =
%     2.894281529275733e-003
%
% We reduce the errors by 1 or 2 orders of magnitude by 
% using Richardson extrapolation to accelerate the convergence.