# Animate a point that follows a function inside a plot

I'm trying to create a plot that shows an animation of a point moving along a function. The idea behind this is that a I want the point to follow the function until the discontinuity point.

For every discontinuity point p of f, a button for that discontinuity is showed and when clicked it should open a dialog box where the f is plotted and then a point should move toward p.

p is defined as a pt[x_] := Graphics[{Red, AbsolutePointSize[8], Point[{x, f[x]}]}]; so that it can be plotted in function of the value of x controlled by a manipulate. The manipulate is then used for animation.

When I call my animate function I pass two values, one is the function to plot, and the other is a list of the discontinuity points. Everything works fine, I get a button for every discontinuity and, when clicked, a dialog window is opened and the f is plotted, but the point doesn't follow the function form.

I can't understand what is the cause of this behaviour because the pt[] works correctly if used in a notebook without using function calls.

(*
f : function to plot
l : list of discontinuity points of f
*)

AnimateFunction[f_,l_] := Module[{list = l, data, g},
pt[x_] := Graphics[{Red, AbsolutePointSize[8], Point[{x, f[x]}]}];
Grid[{
Table
[
With[{data00=data},
Button[
data00,{CreateDialog[
Manipulate[
Show[plot,pt[x]],
{{x, -10,"Point"}, -10, 10, .1, AppearanceElements -> All},
{{plot,Plot[{f[x],Line[{{data00, Infinity}, {data00, Infinity}}]},
{x,data00-1,data00+1},
PlotRange->{{data00-1,data00+1},{-Infinity,Infinity}},
ImageSize->500,
ScalingFunctions->"Infinite",
Exclusions->{data00},
PlotStyle->{Thick,Dashed},
Epilog->{Red,Dashed,Line[{{data00, 10}, {data00, -10}}],PointSize[Medium],Point[{data00,0}]}]},None}
]]}
]
],{data,list}]}
]
]

$$$$


I honestly couldn't follow your code, but I think I get the gist of your question. One thing that you need to take care of is what the range of the animated point is. You're just making assumptions about the range of the full function and the ranges of the point-animations. It would be easier to make these things parameters. And the concept of a discontinuity isn't important to the animation. You are certainly wanting to animate near discontinuities, but that is "invisible" to the animation itself. Now, maybe you want to integrate some sort of discontinuity identifier into what I have below, but I think that's a different question.

Here is a function to generate a single button that does what I think you want (MakeButton is a bad name, but I don't want to rathole on finding a better name):

MakeButton[fn_, {xmin_, xmax_}, {submin_, submax_}] :=
Button[
{submin, submax},
CreateDialog[
Manipulate[
Show[Plot[fn[x], {x, xmin, xmax}], Graphics[{Red, AbsolutePointSize[10], Point[{range, fn[range]}]}]],
{range, submin, submax, .05}]]]


Usage would look something like this:

MakeButton[Sin, {-Pi, Pi}, {-1, 1}]


Now we want to make a row of such buttons (again, the name, sigh):

MakeButtons[fn_, {xmin_, xmax_}, subranges : {{_, _} ...}] :=
Row[MakeButton[fn, {xmin, xmax}, #] & /@ subranges]


Usage would look like this:

MakeButtons[Sin, {-Pi, Pi}, {{-1, 0}, {0, 1}}]
`

Now, you would pick your subranges depending on the discontinuities of your function, but you need to decide whether your animation runs from the left of the right of the discontinuity (or just right across it--it doesn't really matter to the animation).

• Thank you very much, that's perfect!
– cgcg
May 14, 2022 at 8:07