Skip to content

Random walk on a sphere with Mathematica

Followings are the program for random walk on unit sphere sphere.  The key part is the definition of a function whose name is rotateWithAxis.  The function returns the list (3D unit vector) q which derived a rotation of the point vector p on the unit sphere with the axis a.   The third argument theta is the angle of counter clockwise rotation.

rotateWithAxis[p_, a_,  theta_] := #/Norm[#] &@((1 – Cos[theta]) (a.p) a + p Cos[theta] + Cross[a, p] Sin[theta]);

Using the function, the random walk on a unit sphere is written as follows:

rw = With[{stepLength = 0.03, num = 10000},
Module[{rotateWithAxis, p, a, q},
rotateWithAxis[p_, a_, theta_] := #/Norm[#] &@((1 – Cos[theta]) (a.p) a + p Cos[theta] + Cross[a, p] Sin[theta]);
a = {1., 0, 0};
q = {Cos[stepLength], Sin[stepLength], 0};
Table[ p = a; a = q;  q = rotateWithAxis[p, a, RandomReal[{0, 2 Pi}]], {num}]

The result can be displayed using Graphics3D like,

Graphics3D[Line[rw], Boxed -> False]

The following image is one of the resultant of this program.

Random walk on a sphere球面上でランダム・ウォークを行う Mathematica のプログラムを公開します.結果は上の図のようになります.

Post a Comment

Your email is never published nor shared. Required fields are marked *