So here is the answer. Lets assume we have the Cartesian coordinate system with (x,y) representing the points. Then joint probability distribution function (PDF) of our random points inside the circle (i.e. the joint distribution of random variable X and random variable Y representing the x and y coordinate of the random point) is given by:
fX,Y(x,y)={1A=1πR2cx2+y2≤R2c0otherwise,
where Rc is the radius of the circle.
Now lets consider the polar coordinate system where r=g1(x,y)=√x2+y2 and θ=g2(x,y)=arctan(y/x). Also, from inverting the functions g1 and g2 we have x=h1(r,θ)=rcosθ and y=h2(r,θ)=rsinθ. Now calculating the Jacobian of the transformation we have:
J(x,y)=det[∂r∂x=x√x2+y2∂r∂y=y√x2+y2∂θ∂x=−yx2+y2∂θ∂y=xx2+y2]=1√x2+y2=1r.
Also we have
|J(r,θ)|=1|J(x,y)|
The joint PDF of random variables R and Θ can then be calculated using the joint PDF of X and Y as:
fR,Θ(r,θ)=fX,Y(h1(r,θ),h2(r,θ))={rπR2c0≤θ≤2π,0≤r≤Rc0otherwise.
We can rewrite the non zero part as:
fR,Θ(r,θ)=12π×2rR2c=fΘ(θ)fR(r)
where
fΘ(θ)=12π
and
fR(r)=2rR2c.
Therefore, Θ is uniformly distributed between 0 and 2π. The random variable R can be generated by first calculating its cumulative distribution function as
FR(r)=∫r02αR2cdα=r2R2c,
and then using a uniformly distributed random variable U over the interval [0,1] to get
U=R2R2c⟹R=Rc√U.
The following MATLAB code generates 1000 random numbers inside a circle with radius 20 centered at −30, −40.
%**********************************************
n = 10000;
Rc = 20;
Xc = -30;
Yc = -40;
theta = rand(1,n)*(2*pi);
r = Rc*sqrt(rand(1,n));
x = Xc + r.*cos(theta);
y = Yc + r.*sin(theta);
plot(x,y,'.'); axis square;
%**********************************************
If you don't understand how I did the PDF transformation see
Chapter 6 (6.2.3 to be exact) or
Chapter 8 (8.3 to be exact).
I hope this helps someone out there.
Thank you for the informative post. It really helps me out of what I am currently stuck with. One typo I found if I am right is that R_{c} on the right hand side of last expression should not be squared.
ReplyDeleteThank you. I fixed the typo.
DeleteMaybe you did not expect your post to help someone after almost one year .. it did for me : )
ReplyDeleteBTW another typo -- one of the "theta"'s should have been "\theta" ..
Thanks a lot for this helpful post!
I am glad it helped. I fixed the typo.
DeleteHelpful . really :)
ReplyDeletei dont know if it's just me but the maths latex writing isn't coming out properly for me
ReplyDeletejust got it sorry ...really helped
ReplyDelete