|
|
import numpy as np |
|
|
import matplotlib.pyplot as plt |
|
|
|
|
|
|
|
|
num_samples = 1000000 |
|
|
radius = 1.0 |
|
|
|
|
|
|
|
|
x = np.random.uniform(-radius, radius, num_samples) |
|
|
y = np.random.uniform(-radius, radius, num_samples) |
|
|
|
|
|
|
|
|
inside_circle = np.sum(x**2 + y**2 <= radius**2) |
|
|
|
|
|
|
|
|
pi_estimate = (inside_circle / num_samples) * 4 |
|
|
print(f"Estimated value of pi with {num_samples} samples: {pi_estimate}") |
|
|
|
|
|
|
|
|
def plot_simulation(x, y): |
|
|
plt.figure(figsize=(8, 8)) |
|
|
plt.scatter(x[x**2 + y**2 <= radius**2], y[x**2 + y**2 <= radius**2], color='blue', s=1) |
|
|
plt.scatter(x[x**2 + y**2 > radius**2], y[x**2 + y**2 > radius**2], color='red', s=1) |
|
|
plt.xlim(-1.5, 1.5) |
|
|
plt.ylim(-1.5, 1.5) |
|
|
plt.title('Monte Carlo Simulation of Pi') |
|
|
plt.gca().set_aspect('equal', adjustable='box') |
|
|
plt.axhline(0, color='black', lw=0.5) |
|
|
plt.axvline(0, color='black', lw=0.5) |
|
|
plt.grid() |
|
|
plt.show() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|