I have a set of points S(0) that is closer to (0,0) than the edges of of a square with vertices (1,1),(1,-​1),(-1,-1)​,(-1,1). I have the code and now i just need to modify it to estimate the area of S(0)

2 vues (au cours des 30 derniers jours)
close all
rng('Shuffle');
NumberInside = 0;
PiEstimate = zeros(500,1);
for k=1:500
x = -1+2*rand(100,1);
y = -1+2*rand(100,1);
NumberInside = NumberInside + sum(x.^2 + y.^2 <= 1);
PiEstimate(k) = (NumberInside/(k*100))*4;
end
plot(PiEstimate)
title(sprintf('Monte Carlo Estimate of Pi = %5.3f',PiEstimate(500)));
xlabel('Hundreds of Trials')
  3 commentaires
Tony Phan
Tony Phan le 16 Fév 2018
Sorry. So i basically have a square board and inside of that i have a set of points S(0), which is similar to a circle but not quite a circle(think of a dartboard almost), I need help modifying the code to estimate the area of S(0)
Jos (10584)
Jos (10584) le 16 Fév 2018
What is your definition of the area of a set of points?

Connectez-vous pour commenter.

Réponses (1)

Jyotish Robin
Jyotish Robin le 23 Fév 2018
Hi Tony!
From your query, it looks like the set of points S(0) is defined by the (x,y) coordinates that are randomly generated in your code.
But, does it look similar to a circle? No is my hunch. If you wanted to generate a set of points that look similar to a circle , you could use something similar to the below:
t = 2*pi*rand(n,1); % n=number of points
r = R*sqrt(rand(n,1)); % R =radius of circle
x = x0 + r.*cos(t); %(x0,y0) : center of circle
y = y0 + r.*sin(t);
Now, once you have the cluster of points, you can generate a boundary and then estimate the area within. To do this, make use of the boundary function. ( https://www.mathworks.com/help/matlab/ref/boundary.html )
[k,v] = boundary(_) returns a scalar v, which is the area which boundary k encloses.
[k, v] = boundary(x,y);
plot(x(k),y(k));
Based on your existing code's structure, it seems that you are trying to estimate pi from this area calculation.
Here, the returned 'v' will be an estimate of pi/4. (If R=0.5)
Hence 4 times 'v' will be an estimate of pi.
Hope it helps!
Thanks,
Jyotish

Catégories

En savoir plus sur Polynomials dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by