Archimedes's Method

Initial calculations

Approximation using a square:

s2=Sqrt[2]

pi2=2*s2

N[pi2]

Approximation using an octagon:

a=Sqrt[1-(s2/2)^2]

b=1-a

s3=Sqrt[b^2+(s2/2)^2]

pi3=8*s3/2

Simplify[pi3]

N[pi3]

Lists in Mathematica

If I want to record a sequence of values, like a sequence of approximations, I should store them in a list.

Here is a simple way to make a list:

sampleList=Table[0,5]

Access values in a list with double square brackets:

sampleList[[2]]=4

sampleList

sampleList[[3]]=-5.4

sampleList

sampleList[[3]]

Compute Pi using a loop and a list

(* specify number of approximation *)
nMax=10

(* initial side length *)
sideLength=Sqrt[2]

(* create a list to store approximations of pi *)
piValues=Table[0,nMax]

(* include the inital approximation in the list*)
piValues[[2]]=2*sideLength

(* the loop *)
Do[
a=Sqrt[1-(sideLength/2)^2];
b=1-a;
(* compute the next side length *)
sideLength=Simplify[Sqrt[b^2+(sideLength/2)^2]];
piValues[[n+1]]=2^n * sideLength
,{n,2,nMax-1}
]

(* print the output*)
piValues

N[piValues]

Accuracy

These are the errors in our approximations:

N[Pi-piValues]

Use log base 10 to get the number of correct decimal digits:

accuracy=N[-Log[10,Pi-piValues]]

Plot the accuracy of the approximations:

ListPlot[accuracy,AxesLabel->{"iteration","accuracy"}]

{Range[nMax],N[piValues],N[accuracy]}

TableForm[Transpose[{Range[nMax],N[piValues],N[accuracy]}]]

You can add table headings like this:

TableForm[Transpose[{Range[nMax],N[piValues],N[accuracy]}],TableHeadings->{{},{"n","approximation","accuracy"}}]