Math 242 A, 18 February 2022

Here is a module that returns an approximation of π. The specific formula inside the module isn’t important right now.

approximatePi[n_]:=Module[{sum=0},
Do[sum+=(-1)^(i+1)(1/2^(2i-1)+1/3^(2i-1))/(2i-1),
{i,1,n}
];
4*sum
]

When you write a module, you should demonstrate that it works.  A good way to do this is to show the output of the module for some sample input.

piValues=Table[approximatePi[n],{n,1,10}]

N[piValues]

Here are some other ways of making lists of values:

Map[approximatePi,Range[10]]

Map also has a shorthand operator:

piValues=approximatePi/@Range[10]

Shorthand for function composition:

piValues//N

Let' s get a larger list of values:

piValues=approximatePi/@Range[50];

Compute the accuracy

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

Position[accuracy,Indeterminate]

Pi-approximatePi[24]

N[Pi-approximatePi[24],40]

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

accuracy=Floor[-Log[10,Abs[Pi-piValues]]]

Subscripts/Indexes

(Subscript[a, 1])=5

a1

(Subscript[a, 1])

i=5

(Subscript[a, i])

(Subscript[a, i])=4

(Subscript[a, i])

a[1]=4

i=1

a[i]

a[2]=17

a[1+1]

a[0]=3

$MaxExtraPrecision=1000