How can we calculate the total area covered by all the circles together using Monte Carlo method?

댓글 수: 5

I gues the following code would do:
% Circle is given with (xc, yc, rc)
% (xc, yc) coordinates of center, rc is radius
% Monte Carlo algorithm works by creating a square having x and y points in range [-rc ; rc]
% around center point (xc, yc)
xc = 5; yc = 4; rc = 2;
t = linspace(0, 360);
x = xc + rc*cosd(t);
y = yc + rc*sind(t);
plot(x,y)
axis equal
in_circle = 0;
total = 0;
nr_points = 1e6;
for i = 1:nr_points
x = 2*rc*rand - rc + xc;
y = 2*rc*rand - rc +yc;
if (x-xc)^2+(y-yc)^2 <= rc^2
in_circle = in_circle + 1;
end
total = total +1;
area = (2*rc)^2 * in_circle/total;
end
Monte Carlo method estimates area by calculating ratio of total number of points inside a circle of radius rc and total number of generated points.
Jan
Jan 2023년 1월 17일
@Elysi Cochin: What are trhe given inputs?
Torsten
Torsten 2023년 1월 17일
편집: Torsten 2023년 1월 17일
  1. Enclose the area in which all circles are comprised by a rectangle with side lengths a and b.
  2. Generate N random points uniformly distributed in the rectangle.
  3. For each random point, loop over all circles and check the in_circle condition.
  4. If the condition is true for at least one circle, set in_circle = in_circle + 1.
  5. An approximation for the area of the circles is then area = in_circle/N * (a*b)
Elysi Cochin
Elysi Cochin 2023년 1월 17일
Using the code in the comment by @Askic V I could compute the area of all the circles by inputting the x,y coordinate and radius using a loop. But my doubt is, is that code sufficient to compute the area of two intersecting circles?
Image Analyst
Image Analyst 2023년 1월 17일
Did you see my Answer below?
Why would you want to compute the area of overlap? If you did, how do you think you'd handle it? Remove the break, right? What else?

댓글을 달려면 로그인하십시오.

 채택된 답변

Image Analyst
Image Analyst 2023년 1월 17일

0 개 추천

You need to generate an (x,y) point, like in an outer loop. Then in the inner loop you need to loop over all the known circles. If the point is inside a circle, increment the count, and skip checking the rest of the circles by calling "break" so that you don't count a point twice if it's in two circles. Here's a start, assuming xc and yc is a list of the circle center coordinates, and radii is a list of the corresponding radii.
numPoints = 100000; % Whatever
count = 0;
for k = 1 : numPoints
x = whatever, get random number in the range.
y = whatever
for c = 1 : numCircles
if sqrt(x - xc(c)).^ 2 + (y - yc(c))^2) < radii(c)
% Then it's inside this circle
count = count + 1
break; % Skip checking the rest of the circles.
end
end
end
percentArea = count / numPoints

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Triangulations에 대해 자세히 알아보기

질문:

2023년 1월 17일

편집:

2023년 1월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by