1
$\begingroup$

I want to make an animation of a random walk that rescales every time the graph gets to the end point, with the condition that the plot range on x axis is only on [0,1].

In first step, the graph would go to 1 and then rescale ( the width by 1/2, the height by 1/sqrt(2)). So in the next step, one would need one more step (together 2) to get to the end (x = 1), in the third iteration one would need 4 steps to get to the end and so on...

I've managed to compute the walk, but the animation is causing me trouble. I think the scaling is also not right - but if I multiply the y-axis by 1/root(2) it is also wrong. I also do not know what is causing the screen to go red at the beginning.

randomwalk[n_] := 
  Module[{x = 0}, NestList[# + RandomChoice[{-1, 1}] &, 0, n]];

animateRandomWalk[step_] := 
 With[{g = randomwalk[step]}, 
  Animate[ListLinePlot[Take[g, n], 
    PlotRange -> {{0, 1}, Sqrt[2]*MinMax[g]}, 
    DataRange -> {0, step/(step)}], {n, 1, step, 1}, 
   AnimationRunning -> False]]

Thank you so much for your answers!

$\endgroup$
1
  • 1
    $\begingroup$ why do you have DataRange->{0,step/(step)} ? $\endgroup$ Commented Apr 6, 2017 at 9:37

1 Answer 1

2
$\begingroup$

Is this what you want?

randomwalk[n_] :=
  NestList[# + RandomChoice[{-1, 1}] &, 0, n];

animateRandomWalk[step_] :=
 Module[{mm, g = randomwalk[step]},
  mm = MinMax[g];
  Animate[ListLinePlot[Take[g, n], PlotRange -> {{0, 1}, mm}, 
    DataRange -> {0, (n - 1)/2^⌈Log2[n - 1]⌉}], {n, 2, step, 1},
    AnimationRunning -> False]
 ]

I don't think I understand the y scaling you want but maybe this gives a place to start?

animateRandomWalk[step_] := Module[{mm, g = randomwalk[step]},
  mm = MinMax[g];
  Animate[ListLinePlot[Rescale[Take[g, n], mm, {-2, 2}], 
    PlotRange -> {{0, 1}, {-2, 2}}, 
    DataRange -> {0, (n - 1)/2^⌈Log2[n - 1]⌉}], {n, 2, step, 1},
    AnimationRunning -> False]]
$\endgroup$
3
  • $\begingroup$ Yes, thank you. But I also need to rescale the graph in the direction of the y-axis, so in the end the whole graph would be e.g. between -2 and 2. I was told that 1/srqt(2) is the right coefficient to multiply the graph at rescaling the vertical side of it. That is because the number of steps should not affect the height of the whole plot, it should only make the random walk more precise. $\endgroup$ Commented Apr 6, 2017 at 10:32
  • $\begingroup$ @Randomcloud Please see my edit and reply with clarification as needed. $\endgroup$
    – Mr.Wizard
    Commented Apr 6, 2017 at 10:49
  • $\begingroup$ What I wanted to say is that every time one rescales the graph width by 1/2, its height should also be rescaled by 1/sqrt(2). Your formula rescales it in order to fit into the [-2,2] interval, right, with no specified coefficient? The problem is that sometimes it doesn't begin at (0,0). $\endgroup$ Commented Apr 6, 2017 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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