4
$\begingroup$

I would like to make a movie of an animated trajectory but the computation seems to be kind of slow on my machine. Consider the harmonic oscillator

harmonicsol = {x, x'} /. NDSolve[{x''[t] + 0.05 x'[t] + 0.5 x[t] == 0, x[0] == 1, x'[0] == -1},{x, x'}, {t, 0, 100}][[1]];

the animation doesn't seem to be very laggy

Animate[Graphics[{Line[{harmonicsol[[1]][#], harmonicsol[[2]][#]} & /@
  Range[0, t, 0.01]]}, PlotRange -> 2], {t, 0, 100}, AnimationRate -> 1]

but when I compute a table of graphics (to export them later on)

Table[Graphics[{Line[{harmonicsol[[1]][#], harmonicsol[[2]][#]} & /@ Range[0, t, 0.01]]}, PlotRange -> 2], {t, 0, 100, 0.1}]

it takes quite some time. Are there any ways to speed that up or better ways to make a movie out of this?

$\endgroup$
11
$\begingroup$

ParametricPlot could simplify, e.g.

eqn = {y[t] == x'[t], y'[t] == -0.05  y[t] - 0.5 x[t], x[0] == 1, 
   y[0] == -1};
sol = NDSolve[eqn, {x, y}, {t, 0, 100}][[1]];
p[r_] := ParametricPlot[{x[t], y[t]} /. sol, {t, 0, r}, 
    PlotRange -> {{-2, 2}, {-2, 2}}, 
    Epilog -> {Red, PointSize[0.02], 
      Point[{(x[u] /. sol) /. u -> r, 0}]}] /. Line[u__] :> Arrow[u];
tab = Table[p[j], {j, 0.1, 100, 1}];
Export["e:/mse/mseho.gif", tab, 
 "DisplayDurations" -> Table[0.1, Length@tab], 
 "AnimationRepetitions" -> Infinity]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.