% Script Eg2_2
% Pi Via Polygons

% Input the iteration parameters...
  clc
  delta = input('Enter the error tolerance:');
  nMax  = input('Enter the iteration bound:');
  
% The triangle case...
  n = 3;                               % Number of Polygon Edges
  Inner_n = (n/2)*sin(2*pi/n);         % Inscribed Area
  Outer_n = n*tan(pi/n);               % Circumscribed Area
  ErrorBound = (Outer_n - Inner_n)/2;  % The error bound
  
% Iterate while the discrepancy is too big and n sufficiently small...
  while (ErrorBound  > delta  && n < nMax)
     n = n+1;
     Inner_n = (n/2)*sin(2*pi/n);       
     Outer_n = n*tan(pi/n);  
     ErrorBound = (Outer_n - Inner_n)/2;
  end

% Display the final approximation...
  MyPi = (Inner_n + Outer_n)/2;
  clc
  fprintf(' delta = %10.3e\n n     = %1d\n nMax  = %1d\n',delta,n,nMax)
  fprintf(' PiEst = %20.15f\n Pi    = %20.15f\n',MyPi,pi)


    