2
$\begingroup$

I am making a little animation in Mathematica, along the lines of some of the Wolfram demo projects. I'd like to animate a simulated trajectory of a random process (say a Poisson process), and while it is running, change the parameters using "MouseDown".

Here's a trajectory animated in the front end:

Framed @ DynamicModule[{t = 0.001},
  trajnormal = RandomFunction[PoissonProcess[1], {0, 100}]["PathFunction"];
  step = .05;
  Column[{
     Dynamic[t = If[trajnormal[t] <= 50, t + step, t] ] , 
     Dynamic[Plot[trajnormal[x], {x, 0.001, t}, PlotRange -> {{0, 60}, {0, 60}}, 
                                                PlotPoints -> 1000, ImageSize -> 500] ]
    }]
  ]

And here's my failed attempt at using EventHandler to change the simulated function with a mouse click:

Framed @ DynamicModule[{t = 0.001, i = 1},
s = 0;
step = .05; 
trajnormal = RandomFunction[PoissonProcess[1], {0, 100}]["PathFunction"]; 
trajdiscount = RandomFunction[PoissonProcess[5], {0, 100}]["PathFunction"];

(* show[1] is trajectory of Poisson[1] process *)
show[1] = Dynamic[Plot[50 - trajnormal[x], {x, 0.001, t},  PlotPoints -> 1000,
                                  PlotRange -> {{0, 70}, {0, 55}}, ImageSize -> 500] ]; 
(* show[2] is trajectory of Poisson[1] with Poisson[5] added at time of click = s *)
show[2] = Dynamic[Plot[50 - trajnormal[x] - trajdiscount[Max[x - s, 0]], {x, 0, t}, 
              PlotRange -> {{0, 70}, {0, 55}}, ImageSize -> 500, PlotPoints -> 1000] ]; 

EventHandler[ 
  EventHandler[ 
    Column[{
      Dynamic[t = If[trajnormal[t] <= 50, t + step, t] ] ,
      Dynamic[s],
      Dynamic[ show[i] ]
      }],
  "MouseDown" :> (i = 2)], 
    "MouseDown" :>  (s = t), 
PassEventsUp -> True ] ] 

The mouse click changes the function from show[1] to show[2], but the plots are not displayed.

I'd appreciate any corrections or help.

$\endgroup$
  • $\begingroup$ @Kuba, thanks for cleaning it up. $\endgroup$ – zab Apr 15 '14 at 6:19
  • 2
    $\begingroup$ Just add, show to the scope of DynamicModule. But don't ask me why :/ p.s. you can manage updating with UpdateInterval -> .1, TrackedSymbols->{}. $\endgroup$ – Kuba Apr 15 '14 at 6:33
  • $\begingroup$ @Kuba, wonderful! Thanks so much! Next question, how do I stop the clock once the trajectories hit zero? $\endgroup$ – zab Apr 15 '14 at 6:41
  • 2
    $\begingroup$ Just add such condition to If, $\endgroup$ – Kuba Apr 15 '14 at 6:42
3
$\begingroup$

Based on the discussion in chat among Kuba halirutan and Michael Hale:

trajnormal = RandomFunction[PoissonProcess[1], {0, 100}]["PathFunction"];
trajdiscount = RandomFunction[PoissonProcess[5], {0, 100}]["PathFunction"];

Clear[show]
show[1, t_, step_] :=
      ListLinePlot[
             {#, 50 - trajnormal[#]}\[Transpose] & @ Range[0, t, step],
             InterpolationOrder -> 0,
             PlotRange -> {{0, 70}, {0, 55}}, ImageSize -> 500]
show[2, s_, t_, step_] :=
      ListLinePlot[
             {#, 50 - trajnormal[#] - trajdiscount[Max[# - s, 0] & /@ #]}\[Transpose] & @
                 Range[0, t, step],
             PlotStyle -> Red, InterpolationOrder -> 0,
             PlotRange -> {{0, 70}, {0, 55}}, ImageSize -> 500]

step = .05;
Framed@DynamicModule[{t = 0, i = 1, s = 0},

        EventHandler[
            Dynamic[
                Column[{
                        t = Clock[{1, 50, step}, 30, 1],
                        s, i,
                        If[i == 1, show[1, t, step], show[2, s, t, step]]
                        }]
                ],
            "MouseDown" :> {i = 2, s = t}]
        ]
$\endgroup$
  • $\begingroup$ Genius! Thank you! $\endgroup$ – zab Apr 15 '14 at 23:13
  • $\begingroup$ a small extension to the code is refusing to work. Please have a look here: mathematica.stackexchange.com/questions/46557/… $\endgroup$ – zab Apr 22 '14 at 21:26
  • $\begingroup$ I'd like to stop the trajectories when they hit zero. My attempt (linked to in the previous comment) is behaving weirdly. Could someone please have a look? $\endgroup$ – zab Apr 23 '14 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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