Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to generate a spherical distribution of radial random walk points in 3D space. The following code works, but the random walk lines aren't radial. Why ? Where is my mistake ?

MinSprite := 0.006; (* min radius of sprites *)
MaxSprite := 0.03; (* max radius of sprites *)
SpriteOverlap := 0.75; (* min separation between sprites *)
IterationStep := 0.1;
NumberOfSteps := 20;
thickness = 0.09;
pointsmean = 20;
pointssd = 12;

SpriteSize[p_] := MinSprite + (MaxSprite - MinSprite)Norm[p];

SeedRandom[];
RandomWalk = Flatten[Table[{x,y,z}={dist Sqrt[1 - cosinus^2]Cos[phi],dist Sqrt[1 - cosinus^2]Sin[phi],dist cosinus};
    {u,v, w}={0.0, 0.0, 0.0};
    dist = RandomReal[{5,10}];
    phi = RandomReal[{0,2Pi}];
    cosinus = RandomReal[{-1,1}];
    velocity = Abs[RandomReal[NormalDistribution[0,s]]];

Line[NestList[(
    u+=velocity Sqrt[1 - cosinus^2]Cos[phi];
    v+=velocity Sqrt[1 - cosinus^2]Sin[phi];
    w+=velocity cosinus;
    #+IterationStep{u,v, w})&,{x,y, z},NumberOfSteps]],{s,0.25,0.75,0.007}][[All,1]],1];

CloudsParticles = Flatten[Table[(#+RandomReal@LaplaceDistribution[0,thickness])&/@#,{Max[1,IntegerPart@RandomReal@NormalDistribution[pointsmean,pointssd]]}]&/@RandomWalk, 1];

max=Max[Norm/@CloudsParticles];
NormalizedParticles = CloudsParticles/max;

MinSeparation[p_] := SpriteOverlap SpriteSize[p];
KeepPoint[{p_,q_}] := Norm[p]<Norm[q]||Norm[p-q]>MinSeparation[p];
FilterOnce[pts_] := With[{nf=Nearest[pts]},Select[pts, KeepPoint[nf[#,2]]&]];
PointsCoords = FixedPoint[FilterOnce,NormalizedParticles];

ListPointPlot3D[PointsCoords,BoxRatios->{1,1,1},ImageSize->800,SphericalRegion->True,PlotStyle->{Blue,PointSize[Small]}]

Here's a sample of the output. As you can see, this isn't a radial distribution :

This distribution isn't radial

The mistake most probably lies in the RandomWalk declaration, but I can't see it. Anyone has an idea of what may be wrong ?

Take note that I'm using Mathematica 7.0 only.

EDIT :

I must admit that this method isn't a clever way of defining a random distribution of points around radial lines. I'll have to do it differently.

share|improve this question
    
Could you please specify what is a "radial" random walk? –  belisarius Mar 18 '13 at 14:48
    
I mean a motion on a radial line only, so : steps forward, step back, etc, but toward (or away) the origin of coordinates. –  Cham Mar 18 '13 at 14:59
add comment

1 Answer

up vote 3 down vote accepted

Put the line

{x,y,z}={dist Sqrt[1 - cosinus^2]Cos[phi],dist Sqrt[1 - cosinus^2]Sin[phi],dist cosinus};

Behind

velocity = Abs[RandomReal[NormalDistribution[0,s]]];

and the other expressions that set your variables, rather than before it. With

MinSprite := 0.006; (* min radius of sprites *)
MaxSprite := 0.03; (* max radius of sprites *)
SpriteOverlap := 0.75; (* min separation between sprites *)
IterationStep := 0.1;
NumberOfSteps := 20;
thickness = 0.09;
pointsmean = 20;
pointssd = 12;

You will see that setting

randomLines =
 Table[
  {u, v, w} = {0.0, 0.0, 0.0};
  dist = RandomReal[{5, 10}];
  phi = RandomReal[{0, 2 Pi}];
  cosinus = RandomReal[{-1, 1}];
  velocity = Abs[RandomReal[NormalDistribution[0, s]]];

  {x, y, z} =
   dist { Sqrt[1 - cosinus^2] Cos[phi], Sqrt[1 - cosinus^2] Sin[phi], 
     cosinus};

  Line[
   NestList[
    (u += velocity Sqrt[1 - cosinus^2] Cos[phi];
      v += velocity Sqrt[1 - cosinus^2] Sin[phi];
      w += velocity cosinus;
      # + IterationStep {u, v, w}) &,
    {x, y, z},
    NumberOfSteps
    ]
   ]

  ,
  {s, 0.25, 0.75, 0.007}

  ]

and then doing

Graphics3D@randomLines

yields a picture with radial random lines.

Remark

Note that

randomLines =
  Table[
   {u, v, w} = {0.0, 0.0, 0.0};
   dist = RandomReal[{5, 10}];
   velocity = Abs[RandomReal[NormalDistribution[0, s]]];

   {x, y, z} = dist RandomReal[{-1, 1}, 3];

   Line[
    NestList[
     ({u, v, w} = {u, v, w} + velocity {x, y, z};
       # + IterationStep {u, v, w}) &,
     {x, y, z},
     NumberOfSteps
     ]
    ]
   ,
   {s, 0.25, 0.75, 0.007}
   ];

also creates some radial random lines. Just as a side remark.

share|improve this answer
    
Thanks a lot for the answer ! The first suggestion solved my issue. But I don't understand the rest of the answer ; I don't see any difference with my code and the last part of your answer. –  Cham Mar 18 '13 at 14:53
    
@Cham haha yeah it must have been some copy paste error :). It should be ok now. –  Jacob Akkerboom Mar 18 '13 at 14:55
    
Ok, everything appears to be fine. Thanks again for your help. It's very appreciated ! –  Cham Mar 18 '13 at 14:57
    
@Cham note that all of this can be made a lot faster if you want. It is probably also good to note that the definitions probably yield strange random directions and that it is probably "better" to generate random angles and then apply a Cos or Sin to it. Note that in your code you make the Lines and then throw away the heads Line after that, which is a bit pointless :). –  Jacob Akkerboom Mar 18 '13 at 14:58
    
I'm not sure to understand. About the random angle, I used cosinus as a random variable between -1 and 1 to be sure to have an uniform distribution on a sphere. And yes, my code is a bit slow. What would you suggest ? Notice that the lines should have a thickness (random points around the radial lines). –  Cham Mar 18 '13 at 15:01
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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