Archimedes’s Method for π

Math 242: Modern Computational Mathematics (spring 2024)

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]

sample=Table[2,7]

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

sample[[2]]

sample[[3]]=83

sample

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];

(* 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]]=(((2^n)*sideLength)/2),
{n,3,nMax}
]


(* display the results *)
N[piValues]

N[Pi,200]

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[piValues[[2;;]]]

N[Pi-piValues[[2;;]]]

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

N[-Log[10,Pi-piValues[[2;;]]]]

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

TableForm[Transpose[{Range[2,nMax],N[Pi-piValues[[2;;]]],N[-Log[10,Pi-piValues[[2;;]]]]}]]

Here is a better version with headings:

TableForm[Transpose[{Range[2,nMax],N[piValues[[2;;]]],N[-Log[10,Pi-piValues[[2;;]]]]}],TableHeadings->{{},{"n","approx","accuracy"}}]

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

ListPlot[N[-Log[10,Pi-piValues[[2;;]]]],AxesLabel->{"n","accuracy"}]