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)
1 ビュー (過去 30 日間)
NumberInside = 0;
PiEstimate = zeros(500,1);
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;
title(sprintf('Monte Carlo Estimate of Pi = %5.3f',PiEstimate(500)));
xlabel('Hundreds of Trials')
回答 (1 件)
Jyotish Robin 2018 年 2 月 23 日
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);
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!