I am trying to numerically reproduce the Mathematica result for the density of strings in a sphere as explained here.
The program is to generate random, uniformly distributed pair points on a unit sphere. This pair forms a line segment.
Use shells of constant thickness, incrementally increasing in radius, to calculate the density of lines at that radius by finding the portion of all line segments that lie in the shell.
Plot the density as a function of r.
Code:
noOfTrials = 500; points = RandomPoint[Sphere[], {noOfTrials, 2}]; (*gives the length of a line from pt1 to pt2 that lies inside a sphere at origin of radius r*) dFunc[pt1_, pt2_, r_] := Module[ {midPt, h}, midPt = (pt1 + pt2)*0.5; h = Norm[midPt]; If[h >= r, 0, 2 N@Sqrt[r^2 - h^2]] ] (*simulation*) sim = Module[ {dr, l, dlTotal}, dr = 10^-2; Table[ dlTotal = Plus @@ Table[(dFunc[#1, #2, r + dr] - dFunc[#1, #2, r]) & @@ pair, {pair, points}]; {r, dlTotal/(4 Pi r^2 dr)} , {r, 0.01, 1, dr} ] ]; (*plot*) ListPlot[sim]
Problem
- Even for up to 10000 pts, the plot doesn't seem to tend to a constant value near 1/$\pi$ as it should as seen from the answer mentioned before
I've separately checked and rechecked the dFunc
, which I presumed to be the only source of error, but found it to be correct; tweaked dr value; Checked the geometry -- but nothing seems wrong.