0% found this document useful (0 votes)
8 views3 pages

Monte Carlo Project Advanced

Uploaded by

artakransiqi1278
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Monte Carlo Project Advanced

Uploaded by

artakransiqi1278
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Monte Carlo Methods for Approximating π

Abstract
This project delves into the Monte Carlo method as a probabilistic approach to
approximating the value of π. By leveraging random sampling, the method simulates the
ratio between points inside a circle and those within the square that circumscribes it. This
ratio, when multiplied by four, serves as an approximation for π. The project involves
developing an algorithm, implementing it in Python, and visualizing the simulation. The
findings aim to highlight the balance between computational efficiency and accuracy,
demonstrating how Monte Carlo methods can be applied to solve complex mathematical
and scientific problems.

Introduction
Monte Carlo methods are renowned for their use in probabilistic simulations and solving
problems across various fields, ranging from physics to finance. This project focuses on
applying the Monte Carlo approach to estimate the value of π, one of the most fundamental
constants in mathematics. The method is based on a simple geometric relationship between
a circle and its circumscribing square. By randomly generating points within the square and
counting how many fall inside the circle, we approximate π using probability and basic
geometry.

Theoretical Background
The Monte Carlo method operates on the principle that the ratio of areas between a circle
and its circumscribing square is proportional to the ratio of points falling within those
regions when randomly sampled. Consider a circle of radius r inscribed in a square with
side length 2r:
- Area of the Circle = πr²
- Area of the Square = (2r)² = 4r²

The ratio of these areas is:


πr² / 4r² = π / 4

Using this ratio, π can be approximated as:


π ≈ 4 × (Number of Points in Circle / Total Number of Points)

This method relies on the assumption that random sampling is uniformly distributed,
ensuring an unbiased estimation of π.

Algorithm
The Monte Carlo method for approximating π involves the following steps:
1. Define a square with side length 2r and an inscribed circle with radius r, centered at the
origin.
2. Randomly generate points (x, y) within the square, where x and y are uniformly
distributed in the range [-r, r].
3. Determine whether each point lies inside the circle using the equation x² + y² ≤ r².
4. Count the number of points inside the circle and calculate the ratio of these points to the
total points generated.
5. Multiply the ratio by 4 to approximate the value of π.

Implementation in Python
The following Python code demonstrates the Monte Carlo simulation for approximating π:

import random

def monte_carlo_pi(num_points):
inside_circle = 0
for _ in range(num_points):
x, y = random.uniform(-1, 1), random.uniform(-1, 1)
if x**2 + y**2 <= 1:
inside_circle += 1
return 4 * (inside_circle / num_points)

# Example usage
num_points = 100000
pi_estimate = monte_carlo_pi(num_points)
print(f"Estimated π: {pi_estimate}")

Visualization
Visualization plays a crucial role in understanding the Monte Carlo simulation. By plotting
the randomly generated points, we can visually validate the method. Points that fall inside
the circle are marked distinctly from those outside. This graphical representation also helps
in analyzing the distribution of random points and verifying uniform sampling.

Below is an example of how points can be visualized in Python using matplotlib:

import matplotlib.pyplot as plt


import random

def plot_monte_carlo(num_points):
inside_x, inside_y = [], []
outside_x, outside_y = [], []

for _ in range(num_points):
x, y = random.uniform(-1, 1), random.uniform(-1, 1)
if x**2 + y**2 <= 1:
inside_x.append(x)
inside_y.append(y)
else:
outside_x.append(x)
outside_y.append(y)

plt.figure(figsize=(6, 6))
plt.scatter(inside_x, inside_y, color='blue', s=1, label='Inside Circle')
plt.scatter(outside_x, outside_y, color='red', s=1, label='Outside Circle')
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.title('Monte Carlo Simulation for π')
plt.show()

plot_monte_carlo(5000)

Discussion
The Monte Carlo method provides an efficient and intuitive approach for estimating π. As
the number of points increases, the approximation of π converges to its true value.
However, the accuracy of the method depends on the randomness and uniformity of point
generation. The computational cost increases linearly with the number of points, making it
suitable for parallelization. This method also serves as an excellent introduction to
probabilistic and numerical simulations.

Conclusion
The Monte Carlo method is a versatile and accessible technique for approximating π. Its
probabilistic nature demonstrates the power of random sampling in solving mathematical
problems. Through this project, we have explored the theoretical foundation, algorithm
development, and practical implementation of the Monte Carlo method. The visualizations
and results further highlight the simplicity and effectiveness of this approach. Monte Carlo
methods remain a cornerstone in computational mathematics, with applications extending
far beyond the estimation of π.

References
1. Metropolis, N., & Ulam, S. (1949). The Monte Carlo Method. Journal of the American
Statistical Association.
2. Press, W. H., Teukolsky, S. A., Vetterling, W. T., & Flannery, B. P. (2007). Numerical
Recipes: The Art of Scientific Computing.
3. Python Documentation: https://docs.python.org
4. GeeksforGeeks: Estimating Value of π Using Monte Carlo Method.

You might also like