3

This bit of code runs a little animation of a stochastic process:

 (* a random walk bm, its min and max  *)
 {bm, m, M} = {Table[Random[] - .5, {100}] // Accumulate, Min[bm], Max[bm] }

 (* plot of trajectory of bm up to i-th step *)
 traj[i_] := ListLinePlot[({Range[1, 100], bm} // Transpose)[[;; i]], 
    Joined -> True, PlotRange -> {{0, 100}, {m, M}}]

 (* animation using Clock *)
 q = Dynamic[r = Clock[{1, 100, 1}, 5, 1]; traj[r] ]

Next I'd like to use Button to run the animation. Something like

Button["Run", q]

but this doesn't work.

Eventually I'd like to have two buttons, "Repeat" and "Clear" like this:

x = {{0, 0}};   
min[x_] := (Sort[x, #1[[2]] < #2[[2]]  &])[[2]]; Column[ {Row[{Button["Repeat", 
 x = Append[x, { Random[], Random[]  }]], 
Button["Reset", x = { {0, 0}}]  }  ],
Dynamic[Show[{ ListPlot[x, PlotRange -> {{0, 1}, {0, 1}}, ImageSize -> 300, 
  Frame -> True, Axes -> None] , If[Length[x] > 1, 
  Graphics[{Red, Line[{min[x], {min[x][[1]], -.1} }]}], {}],
 If[Length[x] > 1, 
  Graphics[{Red, Line[{min[x], {0, min[x][[2]]} } ]}], {}]
 }]]}]

but instead of dots, I want to plot animated functions involving Clock.

zab
  • 275
  • 1
  • 8

1 Answers1

4

Have you seen Trigger ?

Panel@Column[{
   Dynamic@traj[r],
   Trigger[Dynamic@r, {1, 100, 1}]
   }]

enter image description here

Simon Woods
  • 84,945
  • 8
  • 175
  • 324
  • Wow, I have not! +1 – Kuba Apr 26 '14 at 21:08
  • @ Simon, Kuba, Trigger looks useful, but doesn't seem to be working on my Linux version. – zab Apr 27 '14 at 07:02
  • @Kuba, oh you deleted your answer :-( It was interesting all the same.

    Anyway, what I have is a function, like q in original post involving Clock Dynamic plots, and works perfectly well with Shift+Return. Now I want to wrap two buttons around it, one to evaluate, and one to repeat.

    Ordinary functions like Button["Do it", Sin[pi] // Print] work fine, but not if there is a Plot or a Dynamic.

    Could this be a Linux Mathematica issue? I know that Manipulate sliders never work in Linux. It's a real pain in the trousers, but I'm not switching to Windows just for this.

    – zab Apr 27 '14 at 07:53
  • 1
    @zab I don't know why your Slider do not work. I think it is not normal behaviour :) but you have to talk with someone who works on Linux, not me. You can ask separate question for this. About Print you can use CellPrint if you use Cell/BoxData/Toboxes earlier, take a look at documentation of those functions. – Kuba Apr 27 '14 at 07:57
  • @Kuba, Slider issue is a known problem:

    http://mathematica.stackexchange.com/questions/24531/manipulate-slider-unresponsive-after-suspend-in-linux

    I will try CellPrint.

    – zab Apr 27 '14 at 08:04
  • OK, so with my original definition of q, Button["Run", q // CellPrint] works, but when when I press it again, it creates a duplicate cell. How do I prevent this? – zab Apr 27 '14 at 08:21