• Remix
  • Share
  • New Entry

on 9 Nov 2023
  • 6
  • 22
  • 0
  • 0
  • 370
drawframe(30);
Write your drawframe function below
function drawframe(f)
n = f * 100; % number of random points
x = -1+2*rand(n,1); % generate n random x-coordinates
y = -1+2*rand(n,1); % generate n random y-coordinates
d = sqrt(x.^2 + y.^2); % calculate distance from origin
count = sum(d <= 1); % count number of points inside unit circle
pi_approx = 4 * count / n; % approximate pi
inside = d <= 1; % logical array of points inside circle
outside = d > 1; % logical array of points outside circle
theta = linspace(0,2*pi,100); % angles for plotting circle
xc = cos(theta); % x-coordinates of circle
yc = sin(theta); % y-coordinates of circle
plot(xc,yc,'k'); % plot circle
hold on;
plot(x(inside),y(inside),'r.',MarkerSize=2); % plot points inside circle
plot(x(outside),y(outside),'b.',MarkerSize=2); % plot points outside circle
axis equal;
title(sprintf("points = %d\npi = %f",n,pi_approx))
end
Animation
Remix Tree