%program to estimate the value of pi using Archimedes principle
clear all;
clc;

N = input('Maximum number of sides of the polygons: ');

j = 6; %minimum polygon taken by Archimedes

for i =j:N
    k = i-j+1; %so k value starts with 1
    
    n(k) = i;
    theta(k) = pi/n(k);
    P_i(k)=n(k)*sin(theta(k)); %perimeter of the inner polygon
    P_o(k)=n(k)*tan(theta(k)); %perimeter of the outer polygon
    diff_error_p(k) = (pi-P_i(k))/(P_o(k)-pi);
end

plot(n,P_i,'b-',n,P_o,'r--');
xlabel('Sides of Polygons');
ylabel('Value of pi');
legend('Inner Polygon','Outer Polygon');

disp('The best estimate for the value of pi is that it lies in between');
disp(P_i(k));
disp('and');
disp(P_o(k));

% plot(k,diff_error_p);
% xlabel('Sides of Polygons');
% ylabel('pi-P_i/P_o-pi');