7

In a plane (two dimensional), a path can be represented by a sequence of (n+1) points (Xo,Yo),(X1,Y1),...,(Xn,Yn) such that, for any i (integer 1 < i < n-1):

Pi(vector) = [Xi-X(i-1),Yi-Y(i-1)]

representing the ith step, is a vector with the length Pi, and a value of the change of direction between the vectors Pi and P(i+1) is measured algebraically (don't know how) by the turning angle a(i).

Like any angular distribution (of changes of direction) it is characterized by a mean vector which is taken to be symmetrical and to have angular mean Φ = o. The approach to this analysis involves numerical simulations, since the algebraic approach seems to be too complex and I have to use a pseudo-random Gaussian generator to obtain continuous values from a normal distribution with a mean of 0 and a standard deviation σ (0.1-1.2)radians to simulate a path.

So after each step with the length P (is constant i.e 125km), the value of the change of direction (turning angle a(i)) is determined by the pseudo-random generator for a given value of σ, which is constant along the path. And then makes a step in the next direction, and so on.

Some useful equations:

a(i) ~ n(0,σ)
Θ(i+1) = Θ(i) + a(i)
X(i+1) = Xi + P Cos[Θ(i+1)]
Y(i+1) = Yi + P Sin[Θ(i+1)]

where Θi represents the direction of the ith step. The direction of the first step Θi, is chosen at random according to a uniform angular distribution by a pseudo-random uniform generator. Turning angles are recorded from -Pi to Pi

So my question is:

How can I take i.e 12 families of 500 step paths each characterized by a given value of standard variation σ ranging between 0.1 and 1.2 radians, of the distribution of changes of direction between successive steps and PLOT it in Mathematica? I don't know anything about Mathematica specially how to write the code for this problem.

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
G.Xara
  • 73
  • 1
  • 3
  • Welcome to stackoverflow G.Xara! Don't forget to vote for the answer(s) below that you like and if one of them answers your question to your satisfaction, please accept it by using the check-mark next to the answer. You can change your choice whenever you like. BTW If this is a homework assignment you should add the tag 'homework'. – Sjoerd C. de Vries May 26 '11 at 21:15
  • 2
    Oh, and by the way, we here at StackOverflow are such a nice bunch of people that we don't need any niceties in the post (no salutations or greetings). That's all implied. We still like you even if you don't use them, so I took the liberty to remove them from your post (see also the faq at http://stackoverflow.com/faq, section tagline). – Sjoerd C. de Vries May 26 '11 at 22:07

3 Answers3

8

Following your notations in the question, you use independent normal increments to build an angle, and then use it as a direction for next step of size size.

Block[{size=0.5}, Graphics[
 Line[Accumulate[
   Function[x, size*{Re[x], Im[x]}, Listable][
    Exp[I Accumulate[
       RandomVariate[NormalDistribution[0, Pi/4], 10^3]]]]]]
 ]]

enter image description here


EDIT: This is a response to G. Xara's request to visualize random walk on Robinson's projection of a sphere.
RandomRobinsonWalk[coords_List] := 
 Show[CountryData["World", {"Shape", "Robinson"}], 
  Graphics[{Thick, Red, 
    Line[Map[ GeoGridPosition[ GeoPosition[#], "Robinson"][[1]] & , 
      coords]]}], Frame -> True]

Generation of random walk on a sphere is performed as follows:

Coordinates[{\[Theta]_, \[Phi]_}, {cosa_, 
    sina_}, \[CapitalDelta]\[Theta]_] := {ArcCos[
    Cos[\[CapitalDelta]\[Theta]] Cos[\[Theta]] - 
     cosa Sin[\[CapitalDelta]\[Theta]] Sin[\[Theta]]], 
   ArcTan[cosa Cos[\[Theta]] Cos[\[Phi]] Sin[\[CapitalDelta]\[Theta]] \
+ Cos[\[CapitalDelta]\[Theta]] Cos[\[Phi]] Sin[\[Theta]] + 
     sina Sin[\[CapitalDelta]\[Theta]] Sin[\[Phi]], (cosa \
Cos[\[Theta]] Sin[\[CapitalDelta]\[Theta]] + 
        Cos[\[CapitalDelta]\[Theta]] Sin[\[Theta]]) Sin[\[Phi]] - 
     Cos[\[Phi]] sina Sin[\[CapitalDelta]\[Theta]]]};

Clear[SphereRandomWalk];
SphereRandomWalk[ipos_, steps_, stepsize_, prec_: MachinePrecision] :=
 FoldList[Function[{up, cossin}, Coordinates[up, cossin, stepsize]], 
  ipos, Function[u, {Re[u], Im[u]}, Listable][
   Exp[I RandomVariate[UniformDistribution[{-Pi, Pi}], steps]]]]

The formula used to obtain the next {\[Theta], \[Phi} pair was obtained as follows:

Expand[Simplify[((RotationMatrix[\[Alpha], {Sin[\[Theta]] Sin[\[Phi]],
          Sin[\[Theta]] Cos[\[Phi]], 
         Cos[\[Theta]]}].({Sin[\[Theta]] Sin[\[Phi]], 
          Sin[\[Theta]] Cos[\[Phi]], 
          Cos[\[Theta]]} /. {\[Theta] -> \[Theta] + \[CapitalDelta]\
\[Theta]}))) /. {Conjugate -> Identity} /. {Abs[x_]^2 :> x^2}]]

that is, perform a fixed size rotation in [Theta] and then rotate around the previous vector by random angle \[Alpha].

Usage:

((# - {90, 0}) & /@ (SphereRandomWalk[{Pi/2, 0} // N, 2500, Pi*0.01]/
     Degree)) // RandomRobinsonWalk

enter image description here

Sasha
  • 5,935
  • 1
  • 25
  • 33
  • `RandomVariate` is not defined in mma7. – Mr.Wizard May 27 '11 at 06:57
  • @Mr.Wizard You can replace `RandomVariate` with `RandomReal`. It will work the same for the normal variate generation. `RandomVariate` also handles RNG for distributions which might be discrete, but not integers, or which are of mixed type, i.e. neither continuous, nor discrete, like a component mixture of exponential and geometric, for example. – Sasha May 27 '11 at 13:32
  • @G.Xara Do you mean to project 2D graphics on a sphere with radius 0.5 ? Any preferences for the map ? Will you settle for stereographic projection, or were you interested in a random walk on a sphere from the outset ? – Sasha May 29 '11 at 13:19
5

Sasha's answer is the way to go, but as you are beginning with Mma, perhaps a procedural program is easier to understand.
Please note that I am NOT saying that this is a good way of doing things in Mma.

P = 1;
For[iter = 1, iter < 13, iter++,
 sigma = iter/10;
 a = RandomVariate[NormalDistribution[0, sigma], 500];
 Clear[theta, x];
 theta[i_] := theta[i] = theta[i - 1] + a[[i]];
 x[i_] := x[i] = x[i - 1] + P {Cos[theta[i]], Sin[theta[i]]};
 theta[0] = RandomReal[{0, 2 Pi}];
 x[0] = {0, 0};
 For[step = 1, step < 500, step++,
  r[iter, step] = x[step];
  ]
 ]
Show@Table[
  ListLinePlot[Table[r[s, i], {i, 500}], PlotStyle -> ColorData[1][s],
    PlotRange -> 300 {{-1, 1}, {-1, 1}}], {s, 1, 12}]

enter image description here

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
5

The following code plots your twelve families in different colors and if you hover your mouse over a line you get sigma in a tooltip.

Graphics[
 Table[
  {x, y} = {0, 0};
  p = 1;
  \[Theta] = RandomReal[{-\[Pi], \[Pi]}];
  Tooltip[
   {
    Hue[\[Sigma]/1.3],
    Line[
     NestList[(\[Theta] += 
         RandomVariate[NormalDistribution[0, \[Sigma]]]; # + 
         p {Cos[\[Theta]], Sin[\[Theta]]}) &, {x, y}, 500]
     ]
    }, \[Sigma]
   ],
  {\[Sigma], 0.1, 1.2, 0.1}
  ]
 ]

enter image description here

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
  • As belisarius indicated, Mathematica allows you to write in various programming styles. His example is in a procedural style (similar to C/pascal/fortran) and mine is in functional programming style (see, e.g., http://reference.wolfram.com/mathematica/guide/FunctionalProgramming.html and the tutorials linked to on that page). `NestList` is one element from functional programming, and the use of 'pure' functions (the # and & bits above) another (see http://reference.wolfram.com/mathematica/tutorial/PureFunctions.html). – Sjoerd C. de Vries May 26 '11 at 21:42
  • `RandomVariate` is not defined in mma7. – Mr.Wizard May 27 '11 at 06:59
  • 1
    Yeah, annoying isn't it? ;-) In this case replacing `RandomVariate` with `RandomReal` will make this example work for mma7. But really, Mr.W, you should upgrade. – Sjoerd C. de Vries May 27 '11 at 10:33
  • @Sjoerd Luddites could have saved the world from unemployment. I think @Mr. is denying to upgrade as a protest against progress :D – Dr. belisarius May 27 '11 at 12:36
  • @Mr.Wiz @belisarius LOL. And in the meantime, Daniel Lichtblau starts spreading rumors about version 9 (http://stackoverflow.com/questions/6071502/function-minimization-with-equality-constraints-in-mathematica-8/6073104#6073104). – Sjoerd C. de Vries May 27 '11 at 13:02
  • @Sjoerd @bel I'll upgrade at version 10 or so. ;-p (To version 8, probably...) – Mr.Wizard May 27 '11 at 13:07
  • @Sjoerd We could start a wishlist here ... or we could spare SO of a few GBs. – Dr. belisarius May 28 '11 at 03:17
  • @belisarius are you referring to the new contourplot requirement that I just saw popping up below your question? – Sjoerd C. de Vries May 28 '11 at 07:42