pi.mws

Calculation of Pi using polygons inscribed in a circle

Pi is the ratio of the perimeter of the polygon to twice the radius of the circle

> restart;

> with(plottools):
c := circle([1,1], 1, color=red):l1:=line([1,1],[2,1],color=blue):
l2:=line([1,1],[1,2],color=blue):l3:=line([1,2],[2,1],color=blue,linestyle=3):plots[display]({c,l1,l2,l3});

[Maple Plot]

In the above, the dotted line has length r*sqrt(2) and hence for n=4 the approximation to Pi is 4*sqrt(2)/2 ~ 2.828

This can be generalized to n*sin(Pi/n ) for a polygon of n sides

If we let x=1/n , then the approximation for Pi becomes g(x)= sin(Pi*x)/x which approaches Pi in the limit x->0

> n:=16;dataset:=evalf(seq([1+cos(2*Pi*m/n),1+sin(2*Pi*m/n)],m=0..n));

n := 16

dataset := [2., 1.], [1.923879532, 1.382683432], [1...
dataset := [2., 1.], [1.923879532, 1.382683432], [1...
dataset := [2., 1.], [1.923879532, 1.382683432], [1...

> with(plots): d:=plot([dataset],color=blue,title="n=16"):plots[display]({c,d});

[Maple Plot]

Suppose we are given the measured values for n=8,16,32,64. Can we predict the value of Pi?

This is an extrapolation problem. Let's make a simple assumption that the measured values approach Pi in powers of 1/n

Assume Pi ~ a0 + a1*x + a2*x^2 +a3*x^3 and then solve for a0,a1,a2,a3

> f:=(x,a0,a1,a2,a3)->a0+a1*x+a2*x^2+a3*x^3;

f := proc (x, a0, a1, a2, a3) options operator, arr...

> sln:=solve({f(1/8,a0,a1,a2,a3)=3.061467,f(1/16,a0,a1,a2,a3)=3.121445,f(1/32,a0,a1,a2,a3)=3.136548,f(1/64,a0,a1,a2,a3)=3.140331},{a0,a1,a2,a3});

sln := {a0 = 3.141583762, a1 = .1077333333e-2, a2 =...

> assign(sln);

> with(plots):

> p1:=plot(f(x,a0,a1,a2,a3),x=0..0.15):

> data:=[1/8,3.061467],[1/16,3.121445],[1/32,3.136548],[1/64,3.140331]:

> p2:=plot([data],style=point,symbol=cross,color=blue,symbolsize=20):

> display({p1,p2});

[Maple Plot]

This is the approximate value for Pi

> a0;

3.141583762

The error in a0 is

> evalf(a0-Pi);

-.8892e-5

However the exact expression for Pi is an even function of x

> g:=x->sin(Pi*x)/x;evalf(taylor(g(x),x,6));

g := proc (x) options operator, arrow; sin(Pi*x)/x ...

series(3.141592654-5.167712783*x^2+2.550164042*x^4+...

> restart;

Assume Pi ~ a0 + a1*x ^2+ a2*x^4 +a3*x^6 and then solve for

a0,a1,a2,a3


> f:=(x,a0,a1,a2,a3)->a0+a1*x^2+a2*x^4+a3*x^6;

f := proc (x, a0, a1, a2, a3) options operator, arr...

> sln:=solve({f(1/8,a0,a1,a2,a3)=3.061467,f(1/16,a0,a1,a2,a3)=3.121445,f

> (1/32,a0,a1,a2,a3)=3.136548,f(1/64,a0,a1,a2,a3)=3.140331},{a0,a1,a2,a3

> });

sln := {a2 = 2.761250133, a3 = -11.36234734, a1 = -...

> assign(sln);

> with(plots):

Warning, the name changecoords has been redefined

> p1:=plot(f(x,a0,a1,a2,a3),x=0..0.15):

> data:=[1/8,3.061467],[1/16,3.121445],[1/32,3.136548],[1/64,3.140331]:

> p2:=plot([data],style=point,symbol=cross,color=blue,symbolsize=20):

> display({p1,p2});

[Maple Plot]

This is a better approximate value for Pi

> a0;

3.141592655

The exact value is

> evalf(Pi);

3.141592654

>

The error in a0 is now

> evalf(a0-Pi);

.1e-8

>