Math 242 B, 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
]

Demonstrate that your module works as expected:

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

N[piValues]

Another way to make a list of values from a Module output:

Map[approximatePi,Range[10]]

Map has a shorthand operator

approximatePi/@Range[10]

There is also a shorthand operator for applying a function:

piValues//N

Fixing a common error

Get a list of pi values:

piValues=approximatePi/@Range[40];

Compute the accuracy

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

Position[accuracy,Indeterminate]

approximatePi[24]

Pi-piValues[[24]]

N[Pi-piValues[[24]]]

N[Pi-piValues[[24]],10]

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

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

Subscripts and Indexes

a0 = 2

(Subscript[a, 0])=17

(Subscript[a, 0])

(Subscript[a, 1])=9

k=1

(Subscript[a, k])

a=4

(Subscript[a, k])

Clear[a]

Use indexed variables in Mathematica like this:

a[0]=1/2

a[1]=4

a[2-1]