10
$\begingroup$

What are some approaches to integrate Ghost trails methods with Animate, which otherwise seem more suited for exporting individual frames?

For example, given this rootPlot function, how can trails be added and displayed as a dynamic alterative to RootLocusPlot?

Animate[rootPlot[(Range[0, 20, 3] // Map[x^# &] // Apply[Plus]) + 
    k x^3, x] // Show[{Graphics@{Gray, Circle[{0, 0}, 1]}, #},
    PlotRange -> 2 {{-1, 1}, {-1, 1}}, Axes -> True] &,
 {k, -50, 50, 1}]

enter image description here

$\endgroup$
2
  • $\begingroup$ possible duplicate of Creating ghost trail effects $\endgroup$
    – chuy
    Commented Feb 26, 2015 at 15:16
  • 3
    $\begingroup$ @chuy, I linked to that question already (first line), but here the focus is on Animate. $\endgroup$ Commented Feb 26, 2015 at 15:32

1 Answer 1

14
$\begingroup$

It really helps others reading your code if you split things up and name parts of the code. Let's start with the polynomial.

polynomial[k_] := Total[x^Range[0, 20, 3]] + k x^3;

and the function that finds the roots, and the function that visualizes them

roots[poly_, x_] := roots[poly, x] = Through@*{Re, Im} /@ (x /. NSolve[poly, x])
frame[k_, opacity_] := ListPlot[
  roots[polynomial[k], x],
  PlotStyle -> Opacity[opacity]
  ]

And the background can also have its own name so that it is clearly distinguishable in the code.

background = Graphics[{
    Gray, Circle[{0, 0}, 1]
    },
   PlotRange -> 2 {{-1, 1}, {-1, 1}},
   Axes -> True
   ];

Now, a ghost trail is nothing but a plot that includes the current frame and the last lag number of frames, with each frame faded by age.

visualize[k_, lag_, {min_, max_}] := 
 visualize[k, lag, {min, max}] = Module[{mink, frames},
   mink = Max[min, k - lag];
   frames = frame[#, (# - mink)/(k - mink)] & /@ Range[mink, k];
   Show[background, Sequence @@ frames]
   ]

And so finally:

Animate[
 visualize[k, 10, {-50,50}],
 {k, -50, 50}
 ]

enter image description here

$\endgroup$
10
  • $\begingroup$ And it helps copying the code if it is not riffled with text :P $\endgroup$
    – Kuba
    Commented Feb 26, 2015 at 9:13
  • $\begingroup$ @Kuba I know, but I think my reasons for sacrificing copyability are sound. $\endgroup$
    – C. E.
    Commented Feb 26, 2015 at 11:13
  • 1
    $\begingroup$ Maybe text as comment in code block is a good idea? I don't know, I'm just thinking about general approach because often when I have to do multiple copy+paste I just skip that :P $\endgroup$
    – Kuba
    Commented Feb 26, 2015 at 11:16
  • 2
    $\begingroup$ @Kuba But then, you wouldn't have the joy of writing code to extract the code. :) code=StringJoin[Cases[First[Cases[Import["http://mathematica.stackexchange.com/questions/75936/ghost-trails-with-animate","XMLObject"], XMLElement["td", {_, _, "class" -> "answercell"}, ___],Infinity]],XMLElement["pre", {}, {XMLElement["code", _, {code_}]}] :> code, Infinity]] $\endgroup$ Commented Feb 26, 2015 at 13:44
  • 1
    $\begingroup$ @MarkMcClure Maybe it is OS specific. Your code worked as it is for me. UndocumentedTestFEParserPacket $\endgroup$
    – Kuba
    Commented Feb 26, 2015 at 15:02

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.