Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: random walk visualization

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105259] Re: random walk visualization
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Wed, 25 Nov 2009 06:21:32 -0500 (EST)
  • References: <heimn8$5mc$1@smc.vnet.net>

Alexei Boulbitch schrieb:
> Dear Community,
> 
> I am making a demonstration for a lecture on random walk. This should 
> show the random walk evolving in 2D. The following works nicely:
> 
> x := 0;
> y := 0;
> tab = Table[{x += RandomInteger[{-1, 1}],
>     y += RandomInteger[{-1, 1}]}, {1000}];
> imTab = Table[
>    Show[{Graphics[{Blue, Line[tab[[1 ;; i]]]}],
>      Graphics[{Red, Thick, Arrowheads[0.03],
>        Arrow[{tab[[1]], tab[[i]]}]}]},
>     PlotRange -> {{-40, 40}, {-40, 40}}],   {i, 2, 1000}
>    ];
> ListAnimate[imTab]
> 
> It however, takes a lot of memory, and few minutes to generate the 
> graphics list. That is too long.
> 
> If I could directly Animate the graphics instead of generating initially 
> a graphics list, it would be much faster. This intends to do such a 
> direct animation:
> 
> x := 0;
> y := 0;
> tab = Table[{x += RandomInteger[{-1, 1}],
>     y += RandomInteger[{-1, 1}]}, {1000}];
> Animate[Show[{Graphics[{Blue, Line[tab[[1 ;; i]]]}],
>    Graphics[{Red, Thick, Arrowheads[0.03],
>      Arrow[{tab[[1]], tab[[i]]}]}]},
>   PlotRange -> {{-40, 40}, {-40, 40}}],   {i, 2, 1000}
>  ]
> 
> But it does not work. I cannot understand the reason. Any idea?

the reason is that Animate has no clue that it should only use integer
values for i, so tab[[1;;i]] will give errors. You should use
tab[[1;;Round[i]]] or, even better, the iterator {i,2,1000,1} instead,
which will increase i in steps of 1.

there are some other things you might want to change, the following
should do what you want:

n = 1000;
points = NestList[# + RandomInteger[{-1, 1}, 2] &, {0, 0}, n];
Animate[Graphics[{
   Blue, Dynamic[Line[points[[1 ;; i]]]],
   Red, Thick, Arrowheads[0.03], Arrow[points[[{1, i}]]]
   },
  PlotRange -> Max[Norm /@ points]],
 {i, 2, n, 1},
 AnimationRepetitions -> 1
 ]


hth,

albert


  • Prev by Date: When Wolfram's technical support cannot help
  • Next by Date: Re: random walk visualization
  • Previous by thread: random walk visualization
  • Next by thread: Re: random walk visualization