Estimation of Value of Pi From Area of Circle in Two Dimension Using Monte Carlo
import random
import math
import numpy as np
import matplotlib.pyplot as plt
N = [100,1000,10000,100000,100000]
E = []
true_value = 3.141592
for j in range(len(N)):
c= 0
s = 0
I = 0
for i in range(N[j]):
x = random.random()
y = random.random()
d = x*x + y*y
if d <= 1:
c +=1
s +=1
I += 1
pi = 4 * (c/s)
print(f'Calculated value of pi: {pi}')
error = abs(pi - true_value)
print(f'Estimated value of error: {error}')
E.append(error)
Calculated value of pi: 3.04
Estimated value of error: 0.10159200000000013
Calculated value of pi: 3.164
Estimated value of error: 0.022407999999999983
Calculated value of pi: 3.1504
Estimated value of error: 0.008807999999999705
Calculated value of pi: 3.14332
Estimated value of error: 0.0017279999999999518
Calculated value of pi: 3.1496
Estimated value of error: 0.008007999999999793
Plot of Number VS Error
plt.plot(N,E)
plt.title("Plot of Error")
plt.xlabel("Value of N")
plt.ylabel("Value of Error")
Text(0, 0.5, 'Value of Error')
Estimation of Value of Pi From Volume of Sphere in Three Dimension
Here, we have value of hypersphere in n dimension
import random
N = [100,1000,10000,100000]
E = []
true_value = 3.14
for i in range(len(N)):
I = 0
for j in range(N[i]):
x = random.random()
y = random.random()
z = random.random()
r = x**2 + y**2+ z**2
if r<1:
I = I + 1
ratio = I/N[i]
Pi = ratio * 6
print(f'Calculated value of pi: {Pi}')
error = abs(Pi - true_value)
print(f'Estimated value of error: {error}')
E.append(error)
Calculated value of pi: 3.24
Estimated value of error: 0.10000000000000009
Calculated value of pi: 3.186
Estimated value of error: 0.04599999999999982
Calculated value of pi: 3.189
Estimated value of error: 0.04899999999999993
Calculated value of pi: 3.1386000000000003
Estimated value of error: 0.0013999999999998458
Plot of Number VS Error
plt.plot(N,E)
plt.title("Plot of Error")
plt.xlabel("Value of N")
plt.ylabel("Value of Error")
Text(0, 0.5, 'Value of Error')
Estimation of Value of Pi From Area of Circle in Four Dimension
Estimation of Value of Pi from the integration function
Estimation of value of pi using Integration Method
Algorithm:
-
Import random
-
Import math
-
Import numpy
-
a = 0
-
b = 1
-
integral = 0
-
i = 0
-
while i < 1000
-
x = random.random()
-
integral += 1/1 + x**2
-
i = i + 1
-
ans = integral* (b-a)/float(N)
-
Print(ans)
N = [100,1000,10000,100000,1000000]
print("True Value of Pi")
true_value = 3.141592653589793238
print(true_value)
Error = []
for i in range(len(N)):
a = 0
b = 1
integral = 0
for j in range(N[i]):
x = random.random()
integral += 4 * 1/(1 + x**2)
ans = integral * (b-a)/float(N[i])
error = abs(ans - true_value)
Error.append(error)
#print(ans)
print(f'Calclated Value of Pi : {ans}')
print(f'Error Value of pi: {Error}')
True Value of Pi
3.141592653589793
Calclated Value of Pi : 3.2079305434667744
Calclated Value of Pi : 3.1171874694431705
Calclated Value of Pi : 3.141631082310233
Calclated Value of Pi : 3.138488698353392
Calclated Value of Pi : 3.14167462845529
Error Value of pi: [0.0663378898769813, 0.024405184146622627, 3.8428720440020214e-05, 0.003103955236400946, 8.197486549699207e-05]
Plot of Number Vs Error
plt.plot(N,E)
plt.title("Plot of Error")
plt.xlabel("Value of N")
plt.ylabel("Value of Error")
[]