Archimedes’s Method for π

Math 242: Modern Computational Mathematics (spring 2023)

N[Pi,100]

Lists in Mathematica

When working with a sequence of values, it’s helpful to use a list. Here are two ways of creating lists:

Range[10]

Table[0,10]

Use the part operator, [[]], to access and assign values to list elements. Note that in Mathematica, list indexes start from 1.

sampleList=Table[0,10]

sampleList[[2]]=12

sampleList

sampleList[[4]]=-12

sampleList

sampleList[[4]]

Implementing Archimedes’s Method

Complete the code in the following cell to implement Archimedes’s method for approximating π with inscribed (2^n)-gons.

(* specify the number of iterations *)
nMax=10;

(* initialize values *)
sideLength=Sqrt[2];
piInit=2*sideLength;

(* initialize a list for approximations of π *)
piValues=Table[0,nMax];

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

(* main loop *)
Do[
a=Sqrt[1-((((sideLength/2)))^2)];
b=1-a;
sideLength=Sqrt[(b^2)+((((sideLength/2)))^2)];
piValues[[n+1]]=(2^n)*sideLength,
{n,2,nMax-1}]

(* display the results *)
Print["π values: ",N[piValues]]

Accuracy of Approximation

Assess the accuracy of your results. How does the number of correct digits of π relate to the number of iterations of Archimedes’s method?

N[Pi-piValues]

The negative log of the difference is (roughly) the number of correct digits of π:

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

We can make a table like Figure 1.1 in the text:

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

We can also make a plot similar to Figure 1.3 in the text:

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