# Zooming into Parametric Animation Plot that Uses InterpolatingFunctions

I have an interplanetary trajectory solver that calculates a spacecraft route from Earth to Mars, and I am trying to zoom into the spacecraft when it reaches its Mars intercept orbit but am having some trouble. I originally asked a question of a similar nature on these forums (Zooming into animated plot), but used a simple parametric function as an example, and although I was given a good answer, I was unable to translate that solution to the trajectory solver as it is a bit more involved. Here is a stripped down version of the code which shows the orbits of Earth (inner circle), Mars (outer circle) and the spacecraft (line joining the inner and outer circles). Although what I tried hasn't worked, if you look at the PlotRange code I used when making the trajectory animation, I used an if function to change the PlotRange and essentially "zoom-in" to the spacecraft when it got close to Mars; unfortunately this code did not work, but I have left it in as it is essentially what I want to do in pseudo-code if you will. Any help would be appreciated, thanks guys.

Remove["Global*"]
(*Gravitational Constant*)
G = 6.672*10^-11;
(*Simulation running time*)
tmax = (800) (86400);
(*Spacecraft TOF between Earth and Mars*)
dt = (254) (86400);
(*Time at which Mid-course correction is applied*)
dtmcc = dt/2;
(*Mass of Sun, Earth, Mars and spacecraft*)
m = 1.988544*10^30 ;(*Mass of Sun*)
m = 5.97219*10^24 ;(*Mass of Earth*)
m = 6.4185*10^23 ;(*Mass of Mars*)
m = 1000;
(*Planetary radii of Sun, Earth and Mars*)
r = 6.963*10^8 ;(*Mean radius of Sun*)
r = 6.37101 *10^6;(*Mean radius of Earth*)
r = 3.3899*10^6 ;(*Mean radius of Mars*)
(*Heliocentric positions of Earth and Mars on 26 November 2011*)
p = 149597870700 {4.416639858432274*10^-1,
8.798967504313304*10^-1} ;
p = 149597870700 {-8.159382724017646*10^-1, 1.414986880765974*10^0};
p = {6.6078363676586365*^10, 1.3162870491119447*^11}
(*Heliocentric velocities of Earth and Mars on 26 November 2011*)
v = 149597870700/
86400 {-1.563974110293042*10^-2, 7.690252775639107*10^-3} ;
v = 149597870700/
86400 {-1.160326991502370*10^-2, -5.778933879736245*10^-3} ;

vLambert = {-8473.224022968232, -23353.77562466311};
vLambert = {17642.608238929563, -11842.147739330356};
mindr = 4.660029187323423*10^6 + 1000000;
theta = 2.1223566349593104;
v2x = 20874.193923950195;
v2y = -11635.115234375;

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

x == p[], y == p[], x == p[],
y == p[], x == p[], y == p[],
x' == v[], y' == v[],
x' == v[], y' == v[],
x' == v[] + 3373.0439329044348,
y' == v[] + 10880.11479034743,
WhenEvent[
t == dtmcc, {x'[t] -> vLambert[],
y'[t] -> vLambert[]}],
WhenEvent[
t == ((253*86400) + 81460), {x'[t] ->
v2x - Sqrt[(G m)/mindr] Sin[theta],
y'[t] -> v2y + Sqrt[(G m)/mindr] Cos[theta]}]}, {x[
t], y[t], x[t], y[t], x[t], y[t]}, {t, 0, tmax},
StartingStepSize -> 0.001, AccuracyGoal -> 15, PrecisionGoal -> 15,
Method -> "StiffnessSwitching", MaxSteps -> 10000000]

Orbits = ParametricPlot[{{x[t], y[t]}, {x[t],
y[t]}, {x[t], y[t]}} /. Soln, {t, 0, tmax},
AxesLabel -> {x, y}, ImageSize -> Large,
PlotRange -> {{-0.25*10^12, 0.25*10^12}, {-0.25*10^12, 0.25*10^12}}]

OrbitAnimation =
Animate[ParametricPlot[{{x[t], y[t]}, {x[t], y[t]},
{x[t], y[t]}}/. Soln /. t -> a, {t, 0, a},
AxesLabel -> {x, y}, ImageSize -> Large, PlotRange ->If[t>=253*86400, {{x[t]-1000000,x[
3][t]+1000000},{y[t]-1000000,y[t]+1000000}},{{-0.25*10^12,
0.25*10^12},{-0.25*10^12,0.25*10^12}}]], {a, 0, tmax},
AnimationRate -> 1000000] • Unless I overlooked something, everything is fine, there is just a scoping issue. Symbols t, x, and y are not available where you set the PlotRange option value. a and Soln are, use them like you have done in the first argument of your ParametricPlot. – Theo Tiger May 20 '14 at 19:26

You need to apply the /. Soln /. t->a rule that you apply inside ParametricPlot to your parameters in PlotRange as well. The error occurred because the expression given to PlotRange didn't evaluate. It would just say x[somevalue] because x had not been replaced by the corresponding interpolating function. After that I realized that the list had been wrapped with an extra List in the process of doing those replacements, so I wrapped the expression in First.

OrbitAnimation =
Animate[ParametricPlot[{{x[t], y[t]}, {x[t],
y[t]}, {x[t], y[t]}} /. Soln /. t -> a, {t, 0, a},
AxesLabel -> {x, y}, ImageSize -> Large, PlotRange -> If[
a >= 253*86400,
First[{
{x[t] - 1000000, x[t] + 1000000},
{y[t] - 1000000, y[t] + 1000000}
} /. Soln /. t -> a],
{
{-0.25*10^12, 0.25*10^12},
{-0.25*10^12, 0.25*10^12}
}
]
], {a, 0, tmax}, AnimationRate -> 1000000]


In response to the comment about transitioning into the new Mars centric view:

switchViewLimit = 253*86400;
transitionPeriod = 2 1000000;
transitionRatio[a_] := Min[(a - switchViewLimit)/transitionPeriod, 1]
OrbitAnimation = Animate[
ParametricPlot[
{{x[t], y[t]}, {x[t], y[t]}, {x[t], y[t]}} /.
Soln /. t -> a, {t, 0, a},
AxesLabel -> {x, y},
ImageSize -> Large,
PlotRange -> If[
a >= switchViewLimit,
First[
{
{transitionRatio[a] x[t] - 0.25*10^12,
transitionRatio[a] x[t] + 0.25*10^12},
{transitionRatio[a] y[t] - 0.25*10^12,
transitionRatio[a] y[t] + 0.25*10^12}
} /. Soln /. t -> a
], {
{-0.25*10^12, 0.25*10^12},
{-0.25*10^12, 0.25*10^12}
}]], {a, 0, tmax}, AnimationRate -> 1000000]

• +1 for the effort :D I didn't even try to read the OP's code.. :( – Öskå May 21 '14 at 11:18
• Thanks very much Pickett. I apologise for the amount of code, but I tried to cut it down as much as possible and that's about 20% of it. – InquisitiveInquirer May 22 '14 at 21:31
• @user7388 No worries, it actually wasn't a problem for me because I realized from the beginning where the error was. Theo Tiger did too, which proves your question was manageable after all. – C. E. May 23 '14 at 0:11
• @Pickett is there a way to make the zoom transition from the heliocentric view to the areocentric view (mars-centered) instead of it occuring instantaneously? – InquisitiveInquirer May 23 '14 at 17:17
• @user7388 I've added a piece of code that addresses this. transitionPeriod` determines how fast it transitions to the new view. – C. E. May 23 '14 at 17:46