Quick guide to Mathematica

To get help, place the cursor on the function and press F1
To run all commands in one cell, place the cursor there and press Shift + Enter or Enter on the numerical keyboard.
Output of many commands is suppressed by ; at the end of the command. If you want to see the output, delete ;
Argument of functions must be in square brackets [...], braces {...} are for arrays, ranges etc., use double brackets [[...]] to access elements of arrays
expr /. {x -> a} means substitute a for x in expr, expr // function means apply function to expr, i.e. it is equivalent to function[expr].
D[f[x],x] = derivative of f[x] with respect to x.
To define a function of x one can use f[x_] := 1+(x^2), notice the underscore and the colon, or # and & like in f := 1+((#)^2) &
N[expr] evaluates expr with machine precision, if you use the decimal point in the expression it will be also evaluated with machine precision
Special characters can be inserted by pressing Esc ... Esc, e.g. Esc p Esc gives π
Clear[...] is used to unset any variables which could have been assigned previously.
Some other useful commands: Simplify, Expand, Factor; Integrate, Series, Sum

Clear["Global`*"]

Approximation of π by simple series

From calculus you probably know this series

((π^2)/6)=(∑_(k=1)^∞)(1/(k^2))

n=1000000;
sum=0.0;
Do[sum=sum+1.0/(k*k),{k,1,n}]
π-Sqrt[6.0*sum]

or

((π^4)/90)=(∑_(k=1)^∞)(1/(k^4))

n=100;
sum=0.0;
Do[sum=sum+1.0/k^4,{k,1,n}]
π-(90.0*sum)^(1/4)

As you can see in the latter case, similar accuracy can be obtain with much less terms. Why?

Archimedes’ approximation of π

Basic iterations

There are two possibilities how to write the recurrence relation for the circumference of polygons in the circle:
the first one is not suited for numerical evaluation for it includes subtraction of two close numbers

(Subscript[y, k+1])=((2^(k+1))/(Sqrt[2]))(Sqrt[1-(Sqrt[1-(((((Subscript[y, k])/(2^k))))^2)])])

the second one does not suffer this problem

(Subscript[x, k+1])=(x^k)(Sqrt[(2/(1+(Sqrt[1-(((((Subscript[x, k])/(2^k))))^2)])))])

Let us calculate the sequences numerically and compare them with the exact sequence

n=30;
y=ConstantArray[0.0,n]; x=ConstantArray[0.0,n];
y[[1]]=2.0; x[[1]]=2.0;
tk=2.0;
Do[
y[[i]]=2.0tk Sqrt[(1.0-Sqrt[1.0-(((y[[i-1]]/tk))^2)])/2.0];
x[[i]]=x[[i-1]] Sqrt[2.0/(1.0+Sqrt[1.0-(((x[[i-1]]/tk))^2)])];
tk=2.0tk,
{i,2,n}
]
exact=Array[(2^(#))Sin[π/(2^(#))]&,n];
ListLogPlot[{Abs[exact-y],Abs[exact-x]},PlotStyle->{Blue,Green},PlotLegends->{"(Subscript[y, k])","(Subscript[x, k])"},AxesLabel->{"n","Error"}]

The relative error of the consequent approximations of π

ListLogPlot[{Abs[π-y]/π,Abs[π-x]/π},PlotStyle->{Blue,Green},PlotLegends->{"(Subscript[y, k])","(Subscript[x, k])"},AxesLabel->{"n","Error"}]

Acceleration of convergence - Aitken’s (Δ^2) process (run Basic.Functions.for.TMF057.m first)

A basic method for acceleration of linear convergence of a series (Subscript[x, k]) by introducing a new series (Subscript[Ax, k]) which converges faster but again linearly

(Subscript[Ax, k])=(Subscript[x, k])-((((Δ (Subscript[x, k])))^2)/((Δ^2)(Subscript[x, k]))), where Δ (Subscript[x, k])=(Subscript[x, k+1])-(Subscript[x, k]) and  (Δ^2) (Subscript[x, k])=(Subscript[x, k+2])-2(Subscript[x, k+1])+(Subscript[x, k])

We can accelerate several times. Here we apply Aitken’s (Δ^2)-process 3 times:

Ax1=MyAitkenDeltaSquareProcess[x];
Ax2=MyAitkenDeltaSquareProcess[Ax1];
Ax3=MyAitkenDeltaSquareProcess[Ax2];
ListLogPlot[{Abs[π-x],Abs[π-Ax1],Abs[π-Ax2],Abs[π-Ax3]},PlotStyle->{Blue,Green,Red,Brown},PlotLegends->{"(Subscript[x, k])","First Aitken (Δ^2) accelaration","Second Aitken (Δ^2) accelaration","Third Aitken (Δ^2) accelaration"},AxesLabel->{"n","Error"}]

Chudnovsky approximation of π

This is an actual series which is used by Mathematica and other programs to calculate π efficiently (Chudnovsky algorithm)

MyPi[nn_]=1/(12Sum[(((-1))^k)(6k)!(163*3344418k+13591409)/((3k)!(((k!))^3)(640320^(3k+3/2))),{k,0,nn}]);
Print[N[π,50],", exact π"]
nterms=3;
Do[
Print[N[MyPi[i-1],50],", # terms = ",i,", rel. error ",1.0N[Abs[π-MyPi[i-1]]/π,50]],
{i,1,nterms}
]