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)
조회 수: 3 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Jyotish Robin
2018년 2월 23일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Monte-Carlo에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!