2
$\begingroup$

I'm trying to make an illustration for teaching purposes, but the code is a little long to post. I've made up a toy problem that seems to have the same issue.

Below, I have some code with the same fundamental structure as my desired illustration which allows the user to pick a point from the options and then does an animated plot of the line between the origin and the selected point:

Manipulate[
 Animate[
  Module[{m},
   m = y1/x1;
   Show[
    Plot[m*x, {x, -3, t}, PlotRange -> {{-3, 3}, {-3, 3}}],
    Graphics[{PointSize[0.02], Red, Point[{x1, y1}]}]
    ]
   ],
  {{t, -3.01, ""}, -3, 3, AppearanceElements -> None},
  AnimationRepetitions -> 1
  ],
 {{x1, 1, "x_value"}, {-2, -1, 1}},
 {{y1, 2, "y_value"}, {0, 1, 2}}
 ]

What I would like is:

  1. For the control types to stay the same, including the hidden animate control.
  2. For the animate to show the animation exactly once and then show the static plot on the whole range {-3,3}.
  3. For the animate to restart the animation if either x_value or y_value are changed.
  4. For all other functionality to be unchanged, including the "algorithm" used. For example, let us assume that passing m is not an option. Though, it would be OK, and perhaps preferable, if the Animate were wrapped into the Manipulate.

I have tried turning the Animate into a Dynamic@Animate with x1 as a tracked symbol, but that did not seem to have any change.

$\endgroup$

1 Answer 1

2
$\begingroup$

When changing the values in Manipulate, Mathematica is being smart and it doesn't reevaluate the whole Animate expression but just certain subexpressions. I suggest reading this answer from @MichaelE2.

You have to "trick" Mathematica into reevalution of the whole expression. I think the easiest approach is to simply put your Animate into a separate symbol. Then, Mathematica will think it has to reevaluate it every time values change.

animation[x1_, y1_] := Animate[Module[{m}, m = y1/x1;
    Show[Plot[m*x, {x, -3, t}, PlotRange -> {{-3, 3}, {-3, 3}}, 
      PlotLabel -> {x1, y1}], 
     Graphics[{PointSize[0.02], Red, Point[{x1, y1}]}]]], {{t, -3.01, 
     ""}, -3, 3, AppearanceElements -> None}, 
   AnimationRepetitions -> 1];

Manipulate[animation[x1, y1], 
  {{x1, 1, "x_value"}, {-2, -1, 1}}, {{y1, 2, "y_value"}, {0, 1, 2}}]
$\endgroup$
1
  • $\begingroup$ Nice! Thank you. Basically a perfect answer with a simple solution, and yet one I would never had known enough to think of. I will check out the info you link. $\endgroup$ Apr 10, 2023 at 20:14

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.