Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to programme a random walk on a sphere with Mathematica. I found this code while I was searching, but I'm not getting results from it. I waited an entire day, but my pc didn't finish evaluating rw. I would like some help please (:

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}]]];
share|improve this question

closed as off-topic by gpap, m_goldberg, bobthechemist, Artes, ciao Mar 23 '14 at 1:34

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – gpap, m_goldberg, bobthechemist, Artes, ciao
If this question can be reworded to fit the rules in the help center, please edit the question.

I don't know where did you get this from but in (1-Cos[theta]) the minus is in fact \[Dash]. Just rewrite this expression and it should work. – Kuba Mar 22 '14 at 11:45
p.s. minor edit: #/Norm[#] & is Normalize. – Kuba Mar 22 '14 at 11:47
my minus is [Dash], when i write that mathematica replace it with - didnt work anyway :c but thanks (: – Mariana da Costa Mar 22 '14 at 12:47
I'm not quite sure how have you managed to copy the code here without revealing this? While posting an answer I had to manually change \[Dash] to -. – Kuba Mar 22 '14 at 13:12
I do not agree that it is simple mistake. :) – Kuba Mar 23 '14 at 8:06
up vote 16 down vote accepted

Here is an approach.

rws[n_, p0_?(Norm@# == 1 &), ang_] := 
     ang/(2 Pi), (Function[{u, v}, {Cos[u] Cos[v], Cos[u] Sin[v], 
         Sin[u]}] @@ RandomReal[{0, 2 Pi}, 2])].# &, p0, n]


Graphics3D[{Sphere[], Line[rws[10000, {1, 0, 0}, 1]]}, Boxed -> False]

enter image description here

enter image description here

share|improve this answer
I am not convinced that this is correct. You seem to be trying to rotate around a random axis by ang/(2Pi) (why the 2Pi division at all?). But this random axis doesn't have a uniform distribution on the surface of the sphere (it has a higher density close to the poles). – Szabolcs Mar 22 '14 at 15:34
Also, rotating around an axis randomly chosen from the surface doesn't seem to be what's required. The rotation should be around a random axis chosen uniformly from the great circle defined by the plan perpendicular to the vector corresponding to the current point. – Szabolcs Mar 22 '14 at 15:35
thanks for help (: – Mariana da Costa Mar 22 '14 at 20:59
+1, nice idea as always. – ciao Mar 22 '14 at 22:28
@Szabolcs thank you...I agree it depends sensitively on what one means by random a naive first approach I merely imagined myself as an ant on the surface of perfectly symmetric surface, no preferred direction/isotopic and walking in regular angular step sizes. The rotation of the axis just reflects random choice of direction., the sphere rotating randomly under me is equivalent to me randomly walking. If a particular distribution is aim or other definition I look forward to seeing – ubpdqn Mar 23 '14 at 1:31

If something is evaluating forever it is because of poor implementation, an error or because it is just too much work to do :).

What I'm doing while debugging, is: if it looks ok -> run the minimal example, if it fails -> run it step by step

enter image description here

As you can see Abs[0. - 0.0987745-] is an expression that has now way to appear if - is real Minus.

Fortunatelly there aren't many explicitly written subtractions, you can copy each and try:

(1–Cos[theta]) // FullForm
share|improve this answer
yes, i'm gonna do that thanks :) – Mariana da Costa Mar 22 '14 at 20:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.