-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_point_picking.m
56 lines (51 loc) · 1.06 KB
/
disk_point_picking.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
%demonstrate disk point picking
clear all;
%how many points?
n=1000;
%radius
r=1;
%plot offset
offset=0.2;
%the rejection method
for i=1:n
X(i)=2*r;
Y(i)=2*r;
%do until happy
while(X(i)^2+Y(i)^2>r^2)
%draw a random point in the square [-r,r]x[-r,r]
X(i)=unifrnd(-r,r);
Y(i)=unifrnd(-r,r);
end
end
figure
hold
t=(0:100)*2*pi/100;
plot(r*cos(t), r*sin(t),'--');
scatter(X,Y,'b','filled')
title('rejection rule');
axis([-r-offset r+offset -r-offset r+offset])
hold
%the erroneous uniform angle, uniform radius
R=unifrnd(0,r,n,1);
THETA=unifrnd(0,2*pi,n,1);
X=R.*cos(THETA);
Y=R.*sin(THETA);
figure
hold
t=(0:100)*2*pi/100;
plot(r*cos(t), r*sin(t),'--');
scatter(X,Y,'b','filled')
title('uniform R');
axis([-r-offset r+offset -r-offset r+offset])
hold
%the correct transformation
X=sqrt(R).*cos(THETA);
Y=sqrt(R).*sin(THETA);
figure
hold
t=(0:100)*2*pi/100;
plot(r*cos(t), r*sin(t),'--');
scatter(X,Y,'b','filled')
title('sqrt R');
axis([-r-offset r+offset -r-offset r+offset])
hold