We can use Monte Carlo simulation to estimate geometric areas. For example, suppose we want to approximate the area of a circle of radius \(r\text{.}\) Because the circle is symmetric, it suffices to estimate the area of a quarter circle in the unit square and multiply by 4.
A random point \((x,y)\) uniformly distributed in \([0,1]^2\) either lies inside the quarter circle (\(x^2 + y^2 \le r^2\)) or it doesnβt. The fraction of points that fall inside should approximate the ratio of the areas:
\begin{equation*}
\frac{\text{points inside circle}}{\text{total points}}
\approx
\frac{\text{area of quarter circle}}{\text{area of unit square}}.
\end{equation*}
N = 1000;
r = 0.5;
hits = 0;
% Monte Carlo loop
for k = 1:N
x1 = rand(); y1 = rand();
if x1^2 + y1^2 < r^2
hits = hits + 1;
end
end
hit_frac_hat = hits/N; % fraction of points inside
approxArea = 4 * hit_frac_hat; % approximate full circle area
exactArea = pi * r^2; % true area for comparison
% 95% confidence interval
CL = 0.95;
z = z_from_CL(CL);
marginOfError = z * sqrt(hit_frac_hat*(1-hit_frac_hat)/N);
T = table(N, hits, hit_frac_hat, marginOfError, approxArea, exactArea);
disp(T)
Running this produces the following output:
N hits hit_frac_hat marginOfError approxArea exactArea
____ ____ ____________ _____________ __________ _________
1000 192 0.192 Β± 0.024413 0.768 0.7854
