Group Abstract Group Abstract

Message Boards Message Boards

0
|
11.4K Views
|
4 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Animation of a Random Walk on a lattice

Posted 11 years ago
POSTED BY: Philipp Krönert
4 Replies

Here's a random walk animation on a fancy lattice, done with the help of IGraph/M.

<< IGraphM`

(* IGraph/M 0.3.99.1 (May 1, 2018)

Evaluate IGDocumentation[] to get started. *)

lattice = IGLatticeMesh["Herringbone"]

graph = IGMeshCellAdjacencyGraph[lattice, 2, VertexCoordinates -> Automatic]

walk = IGRandomWalk[graph, First@VertexList[graph], 200]

len = 8;
colors = Reverse@NestList[Lighter, Red, len - 1];
MeshRegion[lattice, MeshCellStyle -> Thread[# -> colors]] & /@ Partition[walk, len, 1] // ListAnimate

enter image description here

POSTED BY: Szabolcs Horvát

Maybe more intriguing the following pseudo-animation of a random lattice walk including some interactivity. The code is based on an example from a WTC talk held by Lou D'Andria on $Manipulate[]$ almost 15 years ago.

Manipulate[
 dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
 ; rwinit = {{0, 0}}
 ; Graphics[
  {{Opacity[0.33], Dynamic@Line[
      If[! paused
       , AppendTo[rw, Last[rw] + stepsize*RandomChoice[dirs]]
       , rw]]}
   , Red, PointSize[Large], Dynamic@Point[Last[rw]]
   }, PlotRange -> All]
 , {{rw, rwinit}, ControlType -> None} (* InputField[], made invisible *)
 , {stepsize, Range[10]} (* PopupMenu[] *)
 , {paused, {False, True}} (* Checkbox[] *)
 ]
POSTED BY: Raspi Rascal

Hi Philipp,

try this (the code - I think - explains itself):

ClearAll["Global`*"]

numberOfSteps = 1000;
startingPostion = {0, 0};

randomStep = 
  Table[RandomChoice[{{0, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, 0}}], {n, 
    numberOfSteps}];
randomPosition = Prepend[Accumulate[randomStep], startingPostion];
randomLines = Partition[randomPosition, 2, 1];
Manipulate[
 Graphics[{{Green, Line[randomLines[[n + 1 ;;]]]}, {Blue, Thick, 
    Line[randomLines[[;; n - 1]]]}, {Red, Thick, 
    Arrow[randomLines[[n]]]}}, Axes -> True, ImageSize -> Large], {n, 
  1, numberOfSteps, 1}]

As a discreet remark: If possible try to avoid looping constructs (like "For[ ]") as this does not agree very well with the programming paradigm of Mathematica.

Cheers Henrik

POSTED BY: Henrik Schachner

Wow, that's great!

Thank you Henrik.

POSTED BY: Philipp Krönert
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard