Hi there,
I started a small project. I tried to animate a Random Walk on a 2 dimensional lattice.
I set the starting point:
list = {{0, 0}};
The Random Walk is performed by:
For[i = 2, i <= 100, i++,
list = Append[list,list[[i - 1]] + RandomChoice[{{0, 0}, {0, 1}, {1, 0}, {0, -1}, {-1,0}}]]]
The animation is done by this piece of code:
ListAnimate[Table[ListLinePlot[Take[list, i],Mesh -> All, MeshStyle -> {Red, Thick}, ColorFunction -> Hue], {i,Length[list2]}], AnimationRate -> 4]
This code performs an animated Random Walk. Now i try to bring more transparency into each single step.
Does someone has an idea how to improve the code in such a way, that every single step will be highlighted?
Thank you for your ideas!
Cheers
Philipp
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
1 Like
szhorvat
(Szabolcs Horvát)
May 2, 2018, 11:02am
4
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
2 Likes
kiwee
(Raspi Rascal)
August 7, 2018, 12:29pm
5
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[] *)
]
1 Like