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.
show
to the scope ofDynamicModule
. But don't ask me why :/ p.s. you can manage updating withUpdateInterval -> .1, TrackedSymbols->{}
. $\endgroup$If
, $\endgroup$