Created
June 29, 2021 21:38
-
-
Save SimonEnsemble/1f141668b2a9d57cf5bc6b353401eb02 to your computer and use it in GitHub Desktop.
MC sim to determine the fraction of a unit orange that comprises the peel.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using LinearAlgebra | |
""" | |
sample a uniform random point inside a unit orange centered at the origin. | |
* `d::Int`: dimension of the space | |
""" | |
function random_point_inside_unit_orange(d::Int) | |
x = 2 * (rand(d) .- 0.5) | |
if norm(x) < 1.0 | |
return x | |
else | |
return random_point_inside_unit_orange(d) | |
end | |
end | |
""" | |
via MC sim, determine the fraction of a unit orange that comprises the peel. | |
* `δr::Float64`: thickness of the peel | |
* `d::Int`: dimension of the space | |
* `nb_samples::Int`: # of MC samples | |
""" | |
function fraction_peel(δr::Float64, d::Int; | |
nb_samples::Int=10000) | |
nb_points_in_peel = 0 | |
for s = 1:nb_samples | |
x = random_point_inside_unit_orange(d) | |
if norm(x) > 1.0 - δr | |
nb_points_in_peel += 1 | |
end | |
end | |
return nb_points_in_peel / nb_samples | |
end | |
fraction_peel(0.1, 3) # 0.26 | |
fraction_peel(0.1, 10) # 0.65 | |
fraction_peel(0.1, 15) # =-O stack overflow, too many rejections in `random_point_inside_unit_orange` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment