When I was studying math I read about the series named Leibniz Series for Pi:
- Image of the formula: https://d2vlcm61l7u1fs.cloudfront.net/media%2F0ee%2F0eeaa57b-570b-4444-93bc-7bc2f4d83931%2FphpHCRPXf.png
So I made an program for it which sums n terms of this series:
def leibniz(n):
pi = 0
for i in range(1,n+1):
if(i % 2 == 0):
pi -= 1 / (2*i - 1)
else:
pi += 1/(2*i - 1)
pi = 4 * pi
return pi
This code works but the thing is that it converges very slowly to Pi.
- So I read about different series acceleration and basically the one which is best in my case is Euler Acceleration
- The slow convergence is because my version is an alternating series, i.e. contains (-1)^n. But I am lot more confused how can I program it.
Edit:
I learned about the Shanks transformation and programed it
def accelrate(n,depth):
if depth == 1:
a = lebniez(n + 1)
b = lebniez(n)
c = lebniez(n-1)
return (a*c - b*b)/(a + c - 2*b)
a = accelrate(n + 1,depth - 1)
b = accelrate(n,depth - 1)
c = accelrate(n-1,depth - 1)
return (a*c - b*b)/(a + c - 2*b)
So what htis does is that it recursively applies Shanks transformation and keep on accelerating the series . But the problem now is that due to recursion it is very slow and the accuracy does not imporve if you increase the depth.
Is this the inefficiency of Shanks Transformation