-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApproximation of pi via the Ramanujan series.py
65 lines (65 loc) · 2.5 KB
/
Approximation of pi via the Ramanujan series.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 29 12:08:35 2021
Calculate an approximation of the number pi by using the Ramanujan series.
More information in this website: https://planetmath.org/ramanujansformulaforpi
@author: Lucas Romero Fernández
"""
import time
import math
import matplotlib.pyplot as plt
import numpy as np
#main_program
start_time_program=time.process_time()#To calculate the program execution time
#Definition of variables, constants and lists
k=0#Summation index
n=1#Summation upper bound, must be an integer
S_list=[]
Ram_pi_list=[]
error_Ram_pi_list=[]
#Main process of the obtainment of pi with the Ramanujan series
while k<=n:
S=(math.factorial(4*k))*(1103+26390*k)/(((math.factorial(k))**(4))*(396)**(4*k))
S_list.append(S)
Ram_pi=(((2*np.sqrt(2))/9801)*sum(S_list))**(-1)
Ram_pi_list.append(Ram_pi)
k+=1
#Main process of the obtainment of pi with NumPy
numpy_pi=np.pi
for i in range(0,len(Ram_pi_list)):#Error between methods
error_Ram_pi_list.append(abs(numpy_pi-Ram_pi_list[i]))
iterations_list=np.arange(0,n+1,1)#For the graphs
#Graphs
#Approximations of the number pi vs n
plt.figure(figsize=(9,5))
plt.plot(iterations_list,Ram_pi_list,c='red')
plt.axhline(numpy_pi,color="black",linestyle="dashed",alpha=0.7,label="NumPy pi")
ax=plt.gca()
ax.yaxis.get_major_formatter().set_scientific(False)
ax.yaxis.get_major_formatter().set_useOffset(False)
plt.title("Calculus of the number pi with the Ramanujan series")
plt.ylabel("Approximations of the number pi")
plt.xlabel("Number of iterations")
plt.legend()
plt.grid()
plt.tight_layout()
plt.show()
#Error of the method vs n
plt.figure(figsize=(9,5))
plt.plot(iterations_list,error_Ram_pi_list,c='red')
plt.title("Error of the Ramanujan series")
plt.ylabel("Error of the expression")
plt.xlabel("Number of iterations")
plt.grid()
plt.tight_layout()
plt.show()
#Results
print("Value of pi with the Ramanujan series (n = {})".format(n),"=",Ram_pi_list[-1])#Result of the obtainment of pi with the Ramanujan series
print("")
print("Value of pi with NumPy =",numpy_pi)#Result of the obtainment of pi with NumPy
print("")
print("Value of the final error =",error_Ram_pi_list[-1])#Result of the final error between methods
print("")
print("Value of the final absolute relative error =",abs(((error_Ram_pi_list[-1])/(numpy_pi)))*100,"%")#Result of the final absolute relative error between methods
print("")
print("Program execution time:",time.process_time()-start_time_program,"seconds.")