kcfert.blogg.se

Matlab calculate pi given a quarter circle in a square
Matlab calculate pi given a quarter circle in a square







matlab calculate pi given a quarter circle in a square

Since the sampling is uniform, it amounts to just computing the length of the line segment. We can compute the integral along this ray geometrically. It’s the points that lie outside the circle that cause this kind of sampling to be biased, non-uniform. This invites a scheme called rejection sampling: choose x,y in the unit cube but only keep those inside the circle. If we had only considered the points inside the circle then we would indeed have a uniform sampling as this integral is then independent of θ. When we project this sampling to the quarter circle (green), we’re really integrating along each ray (blue) of angle θ (grey): Start with our uniform sampling of the unit square: We can verify this with the following small matlab program:Īctually, it’s also pretty straight forward to derive the probability density function geometrically rather than analytically. We begin by acknowledging that for any reasonable function u we have the equality: In other words the probability density functions of the first variables x and y are both 1 for x,y in [0,1) and we’d like to know what the probability density function of θ is. This transformation of normalizing then converting to polar angle, takes these two uniformly samples to a non-uniform sampling of the quarter circle, θ in [0,π/2). The scheme under consideration is to first sample x and y from the unit square uniformly, then normalize (x,y) to unit length and read off the angle θ. This is an exercise in transforming the probability density function of one random variable into that of another.Īs a warmup I followed this systematic approach on math.stackexchange for a 2d problem of non-uniformly sampling a circle, actually just the first quarter of the circle. As I was examining the result, I though it would be more convenient just to derive the probability density function analytically. I recently posted about non-uniformly sampling a sphere and visualizing the result.









Matlab calculate pi given a quarter circle in a square