Pi Complete
Pi Complete
Estimation of �
2025-01-25
Pi Estimation Report
Introduction
Monte Carlo simulation is a probabilistic method that uses random sampling to estimate
numerical results. In this, we estimate the mathematical constant � by simulating random
points in a square and calculating the proportion of points that fall inside a unit circle inscribed
within the square.
Methodology
• A point lies inside the circle if its distance from the origin is less than or equal to 1.
• This condition is checked for all generated points.
1
inside_circle = (x^2 + y^2 <= 1) # Logical vector indicating points inside the circle
• The total number of points inside the circle is calculated by summing the logical values.
4. Estimate �
pi_estimate = 4 * count / n
print(pi_estimate) # Print the estimated value of �
[1] 3.1384
5. Iterative Refinement
pi_values = function(n) {
x = runif(n, min = -1, max = 1)
y = runif(n, min = -1, max = 1)
count = sum(x^2 + y^2 <= 1)
return(4 * count / n)
}
n_iterations = 1000
pi_arr = sapply(1:n_iterations, pi_values)
pi_arr_sample = sample(pi_arr, 100)
pi_arr_sample
2
[41] 2.995633 3.077748 3.119048 3.286031 3.186885 3.116641 3.111111 3.198257
[49] 3.167048 3.065896 2.933333 3.322581 3.107981 2.968553 3.152888 3.130435
[57] 2.666667 3.600000 3.140351 3.090909 3.206544 3.019763 3.140741 3.222482
[65] 3.456790 3.134986 3.163359 3.341176 3.163043 3.042945 3.293777 3.112500
[73] 3.109948 3.099851 3.183217 3.232000 3.087719 3.101370 3.108943 3.164557
[81] 3.483871 3.106439 3.158576 3.177143 3.097030 3.136000 3.118227 3.253142
[89] 3.105691 3.150115 3.182796 3.156812 2.961326 2.787879 3.246377 3.187141
[97] 3.125000 3.143552 3.303797 3.099415
6. Visualization
# Display results
print(mean(pi_arr)) # Average of all estimates
[1] 3.135534
3
Estimation of p
3.5
3.4
Estimated Value of p
3.3
3.2
3.1
3.0
0 250 500 750 1000
Iterations
Results
1. Convergence
• As the number of iterations increases, the estimated values of � converge to the true value
(� = 3.14159).
2. Stability
• The average of the estimates over 1000 iterations provides a robust approximation of �.
3. Visualization
• The plot demonstrates the convergence of the estimates towards the true value of �, with
a dashed line marking the reference value.
4
Conclusion
Monte Carlo simulation effectively estimates � by leveraging random sampling. While initial
estimates may vary, iterative refinements demonstrate convergence to the true value of �. This
approach showcases the power of probabilistic methods in numerical approximation.