1
$\begingroup$

I want to be able to start, stop, and move the dynamically moving point simultaneously in both animations. The "PlayPauseButton" in the animations only controls the given animation. Indeed, I want both animations to always by synchronized.

system = {x'[t] == 2 x[t] - 0.08 x[t] y[t], x[0] == 400,
          y'[t] == -y[t] + 0.01 x[t] y[t], y[0] == 25};
sol = NDSolve[system, {x[t], y[t]}, {t, 0, 21.5}];
{x1[t_], y1[t_]} = {x[t], y[t]} /. Flatten[sol];

px4 = Plot[x1[t], {t, 0, 21.5}, PlotRange -> {0, 400}, PlotStyle -> {Black}];
py4 = Plot[y1[t], {t, 0, 21.5}, PlotRange -> {0, 400}, PlotStyle -> {Black, Dashed}];

Row[
 {Animate[
   Show[px4, py4,
    Graphics[{PointSize[Large], Red, Point[Dynamic[{t, x1[t]}]], 
              Point[Dynamic[{t, y1[t]}]]}]], {t, 0, 21.5, 
   AppearanceElements -> "PlayPauseButton"}, SaveDefinitions -> True,
   AnimationRate -> 0.251, AppearanceElements -> "PlayPauseButton", 
   BaselinePosition -> Bottom],
  Animate[
   Show[ParametricPlot[{x1[t], y1[t]},{t, 0, 5.375}, AxesLabel -> {"Prey", "Predator"}, 
                        AspectRatio -> 1, PlotRange -> {{0, 400}, {0, 80}}], 
        Graphics[{Red, PointSize[Large], Point[{x1[t], y1[t]}]}]],
   {t, 0, 5.375, AppearanceElements -> "PlayPauseButton"}, 
   SaveDefinitions -> True, AnimationRate -> 0.251, 
   BaselinePosition -> Bottom]}, Frame -> True, RoundingRadius -> 10]

The desired slider should fit into the frame space above the first "Animate". Perhaps I could have rewritten this code with Manipulate, but I don't know how to do that either.

$\endgroup$
1
  • $\begingroup$ Stephen, thanks for the accept - you might also wait some time and see if other answers drop in (Qs with accepted answers tend to draw less new answers) and then decide to accept mine ;-) $\endgroup$
    – Yves Klett
    Commented Oct 28, 2013 at 13:09

1 Answer 1

4
$\begingroup$

A slight rewrite of your code using Manipulate:

Manipulate[
 Column[{Show[px4, py4, 
    Graphics[{PointSize[Large], Red, Point[Dynamic[{t, x1[t]}]], 
      Point[Dynamic[{t, y1[t]}]]}]], 
   Show[ParametricPlot[{x1[t], y1[t]}, {t, 0, 5.375}, 
     AxesLabel -> {"Prey", "Predator"}, AspectRatio -> 1, 
     PlotRange -> {{0, 400}, {0, 80}}], 
    Graphics[{Red, PointSize[Large], Point[{x1[t], y1[t]}]}]]}, 
  Frame -> True], {t, 0, 21.5}]

Mathematica graphics

You have different domains for your two Animates, but it somehow seems to work out in a cyclical fashion in this case.

As noted by @MichaelE2, a closer fit to the original Animate can be achieved in this way (layout changed to accomodate larger control bar):

Manipulate[
 Grid[{{Show[px4, py4, 
     Graphics[{PointSize[Large], Red, Point[Dynamic[{t, x1[t]}]], 
       Point[Dynamic[{t, y1[t]}]]}]], 
    Show[ParametricPlot[{x1[t], y1[t]}, {t, 0, 5.375}, 
      AxesLabel -> {"Prey", "Predator"}, AspectRatio -> 1, 
      PlotRange -> {{0, 400}, {0, 80}}], 
     Graphics[{Red, PointSize[Large], Point[{x1[t], y1[t]}]}]]}}, 
  Frame -> True], {t, 0, 21.5}, ControlType -> Animator]

Mathematica graphics

$\endgroup$
6
  • $\begingroup$ Control type Animator for t might be more in line with the OP's wishes. $\endgroup$
    – Michael E2
    Commented Oct 28, 2013 at 12:39
  • 1
    $\begingroup$ @MichaelE2 feel free to modifiy - I just took the quickest route :-) $\endgroup$
    – Yves Klett
    Commented Oct 28, 2013 at 12:43
  • $\begingroup$ I'll just leave it as comment. One can always click the + and use the animator that comes with the slider. I really just meant to point out another possibility. $\endgroup$
    – Michael E2
    Commented Oct 28, 2013 at 12:46
  • $\begingroup$ @MichaelE2 added - thanks for the tip (in fact I never used that Control type specifically). $\endgroup$
    – Yves Klett
    Commented Oct 28, 2013 at 13:07
  • $\begingroup$ The domain for the first animate reflects just 4 periods. Had I used the same domain for the second animate, errors from NDSolve creep in which are reflected in the orbit. Thus I used just one period for the second animate and cut its animation rate by 1/4. Thanks both of you for your help $\endgroup$
    – Stephen
    Commented Oct 28, 2013 at 13:12

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.