1
$\begingroup$

I have a simple animation of a spacecraft being gravitationally deflected by Mars and wanted to be able to represent Mars as a red disk, but am having trouble doing this. Here is the code (where the last animate is my attempt at adding a red disk to the animation):

m[2] = 6.4185*10^23;
r[2] = 3.3899*10^6;
G = 6.672*10^-11;
tmax2 = 2.5*86400
xr = 5.74143*10^8
yr = 4.64259*10^7
vxr = -3275.65
vyr = -227.858

soln = NDSolve[{
   x''[t] == -((G  m[2] x[t])/(x[t]^2 + y[t]^2)^(3/2)),
   y''[t] == -((G  m[2] y[t])/(x[t]^2 + y[t]^2)^(3/2)), 

   x[0] == xr, y[0] == yr, x'[0] == vxr, y'[0] == vyr}, {x[t], 
   y[t]}, {t, 0, tmax2} , MaxSteps -> 1000000, 
  Method -> "StiffnessSwitching"]

Show[ParametricPlot[Evaluate[{x[t], y[t]} /. soln], {t, 0, tmax2}, 
  AxesLabel -> {x, y}, PlotStyle -> Automatic, PlotRange -> Full, 
  ImageSize -> Large], Graphics[{Red, Disk[{0, 0}, r[2]]}]]

Animate[ParametricPlot[{{x[t], y[t]}} /. soln /. t -> a, {t, 
   Max[0, a - 10000], a}, AxesLabel -> {x, y}, Axes -> False, 
  ImageSize -> Large], {a, 0, tmax2}, AnimationRate -> 1000]

Animate[Graphics[{Red, 
   Disk[{0, 0}, r[2]]}], [ParametricPlot[{{x[t], y[t]}} /. soln /. 
    t -> a, {t, Max[0, a - 10000], a}, AxesLabel -> {x, y}, 
   Axes -> False, ImageSize -> Large], {a, 0, tmax2}, 
  AnimationRate -> 1000]]

As can be seen below the disk shows up nicely in the "still" version, but I get a syntax error when trying to put the disk in the animated version. Any help would very much be appreciated, thanks very much.

enter image description here

$\endgroup$
5
  • 1
    $\begingroup$ Look up Epilog and Prolog. $\endgroup$ Commented Jun 9, 2015 at 12:33
  • $\begingroup$ Also take a look at using Show to combine Graphics objects. You already used it for the still version but not for the animated one. But for a simple Disk object using Epilog is probably the best. $\endgroup$
    – Wizard
    Commented Jun 9, 2015 at 13:01
  • $\begingroup$ I tried many combinations of trying to get Show working with Animate and Graphics but could never get it to work for reasons unknown to me. I've had a look at Epilog and Prolog but there don't seem to be any examples using Animate and so I'm unfortunately still getting syntax errors no matter what I try. Syntax seems to be my problem here, as the tools are staring me right in the face (Show and Epilog), which is extremely frustrating $\endgroup$
    – indigoblue
    Commented Jun 9, 2015 at 13:08
  • $\begingroup$ Some more tinkering got it to work, thanks very much guys. $\endgroup$
    – indigoblue
    Commented Jun 9, 2015 at 13:18
  • $\begingroup$ "got it to work" - then, can you answer your own question please? $\endgroup$ Commented Jun 9, 2015 at 13:52

1 Answer 1

2
$\begingroup$

This seemed to get it to work:

Animate[ParametricPlot[{{x[t], y[t]}} /. soln /. t -> a, {t, 
   Max[0, a - 10000], a}, Prolog -> {Red, Disk[{0, 0}, r[2]]}, 
  AxesLabel -> {x, y}, Axes -> False, ImageSize -> Large], {a, 0, 
  tmax2}, AnimationRate -> 1000]
$\endgroup$

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.