Choose n points randomly from a circle, how to calculate the probability that all the points are in one semicircle?

조회 수: 3 (최근 30일)
I want to create a graphic monte carlo simulation that solves this problem, The mathematical answer is : n/2^(n-1)
maybe someone have the algorithm code for that question ?
  댓글 수: 2
Guillaume
Guillaume 2020년 1월 10일
It's not entirely clear what you need help with. The problem itself is simple, so we don't know what's blocking you. You'd just run a monte-carlo simulation which is easy enough to implement.
The one difficultly I can see is the generation of points in a circular uniform distribution. You have to be careful not to use the naive approach of just picking uniform random radii and angles. However, less than 5 minutes of searching on the internet would give you simple and reliable algorithms for that, if it's not already built in matlab.
elroi berkovits
elroi berkovits 2020년 1월 10일
Can u try to help with the code?
i have this so far:
clear all
clc
clear
% Plot a circle.
angles = linspace(0, 2*pi, 720); % 720 is the total number of points
radius = 20;
xCenter = 50;
yCenter = 40;
x = radius * cos(angles) + xCenter;
y = radius * sin(angles) + yCenter;
% Plot circle.
plot(x, y, 'b-', 'LineWidth', 2);
% Plot center.
hold on;
plot(xCenter, yCenter, 'k+', 'LineWidth', 2, 'MarkerSize', 16);
grid on;
axis equal;
xlabel('X', 'FontSize', 16);
ylabel('Y', 'FontSize', 16);
% Now get random locations along the circle.
s1 = 2; % Number of random points to get.
n=1000; % Number of sim
xRandom = zeros(n,s1);
yRandom = zeros(n,s1);
for i=0:n-1
randomIndexes = randperm(length(angles), s1);
xRandom(i+1,:) = x(randomIndexes);
yRandom(i+1,:) = y(randomIndexes);
plot(xRandom, yRandom, 'ro', 'LineWidth', 2, 'MarkerSize', 16);
end
P = nnz(all(xRandom >=50,2))
R = nnz(all(xRandom <50,2))
PROB = (P+R)/n

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

답변 (1개)

KSSV
KSSV 2020년 1월 10일
% Generate circle
r = 1. ; % radius
th = linspace(0,2*pi) ;
x = r*cos(th) ;
y = r*sin(th) ;
% Geenrate random points inside the circle
N = 5000 ;
a = -1 ; b = 1 ;
xr = (b-a).*rand(N,1) + a;
yr = (b-a).*rand(N,1) + a;
% pick points lying inside the circle
idx1 = inpolygon(xr,yr,x,y) ;
xr = xr(idx1) ;
yr = yr(idx1) ;
% random points inside circle
NP1 = nnz(idx1) ;
% points lying in semicircle
idx2 = inpolygon(xr,yr,x(1:50),y(1:50)) ;
NP2 = nnz(idx2) ;
iwant = NP2/NP1 ; % probability that points lie in semicircle
plot(x,y)
hold on
plot(xr,yr,'.r')
plot(xr(idx2),yr(idx2),'ob')
  댓글 수: 3
elroi berkovits
elroi berkovits 2020년 1월 10일
It is very nice and efficient, but it does not answer the statistical question, for example at 3 points the probability is 0.75, at 4 points the probability is 0.5, at 5 points the probability is 0.3125, etc.
KSSV
KSSV 2020년 1월 10일
편집: KSSV 2020년 1월 10일
Yes you are right..I made the problem complex instead of using the inequality.
@elroi berkovits please noe that, you need not to use inpolygon. We have given you a idea, you should try the one you want.

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

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by