# Plotting a moving ball for projectile motion with animate

Here is my code for plotting an orbit. What I want to do is have the "planet" shown as it orbits in the simulation. Can anyone help with this?

Clear["Global"];
Off[General::spell, General::spell1];
dt = 0.01;
vx = 0.0;
vy = 1.2;
x = 1.0;
y = 0.0;
r2 = x*x + y*y;
r = Sqrt[r2];
f = -1/r2;
fx = x/r*f;
fy = y/r*f;
Clear[orbit];
orbit = {};
Do[{vx = vx + fx*dt, vy = vy + fy*dt, x = x + dt*vx, y = y + dt*vy,
r2 = x^2 + y^2, r = Sqrt[r2], f = -1/r2, fx = x/r*f, fy = y/r*f,
AppendTo[orbit, {x, y}]}, {i, 1, 2000}];
orbit1 = Table[orbit[[i]], {i, 1, Length[orbit]}];
Animate[ListPlot[orbit1[[1 ;; j]],
PlotRange -> {{-3, 3}, {-2.5, 2.5}}], {j, 1, Length[orbit], 1}]

• Is Animate[ListPlot[{orbit1, {orbit1[[j]]}}, Joined -> {True, False}, PlotStyle -> PointSize[.05], PlotRange -> {{-3, 3}, {-2.5, 2.5}}], {j, 1, Length[orbit], 1}] close to what you wish to get? – kglr Sep 16 '14 at 2:58
• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Dr. belisarius Sep 16 '14 at 18:13

The same, but more Mathematica-ish (working with functions, representing vectors as lists and using NestList instead of variables and looping-appending):

Clear["Global"];
dt = .01;
velInit = {0, 1.2};
posInit = {1.0, 0};
f[pos_] := -pos/(pos.pos)^(3/2);
vel[{velP_, posP_}] := velP + f[posP] dt
pos[{velP_, posP_}] := posP + vel[{velP, posP}] dt

orbit = NestList[{vel@#, pos@#} &, {velInit, posInit}, 1500][[All, 2]];

Animate[ListPlot[orbit, Joined -> True,
Epilog -> {PointSize@.05, Purple, Point[orbit[[j]]]}],
{j, 1, 1500, 1}] Edit

You may experiment with different initial conditions:

orbitFun[{velInit_, posInit_}] := NestList[{vel@#, pos@#} &, {velInit, posInit}, 1500][[All, 2]]

Manipulate[ DynamicWrapper[
Column[{Grid[{{"Intial Velocity", "Initial Position"}, {velInit,  posInit}}],
Animate[ListPlot[orb, Joined -> True,
Epilog -> {PointSize@.05, Purple, Point[orb[[j]]]}], {j, 1, 1500,1}]}],
orb = orbitFun[{velInit, posInit}]],
{velInit, {.1, .1}, {1.2, 1.2}}, {posInit, {-2, -2}, {2, 2}}] Edit

Code dissection

(* Your formula for "f" *)
f[pos_] := -pos/(pos.pos)^(3/2);
(* New vel based on old vel and old pos *)
vel[{velP_, posP_}] := velP + f[posP] dt
(*New pos based on old vel and old pos *)
pos[{velP_, posP_}] := posP + vel[{velP, posP}] dt

(* Now the tricky NetsList[] *)
(* It will recurse 1500 times storing in the results list the {vel, pos} for each iteration *)
(* Each itertion get feeded with the last iteration {vel, pos} and calculate the new one*)
(*AFTER it finishes, the [[All,2]] thingy will keep only the positions
and discard the velocities so that we can plot the pos list*)

orbit = NestList[{vel@#, pos@#} &, {velInit, posInit}, 1500][[All, 2]];

• That makes a lot of sense, thank you. I don't actually know what the use of the @# is in the NestList function? – Karl Sep 16 '14 at 18:08
• – Dr. belisarius Sep 16 '14 at 18:20
• Okay, so let me get this straight: The := allows the function to be called whenever it shows up in the problem. So f gets called into the vel function which then gets called into the position function. The NestList function then uses the initial velocity and position as its initial value and then I get a little lost. It has to refresh the values but I don't see where the new values of pos and vel get put. Is that just part of the NestList function? Assuming that all works, the All function takes only the position, correct? And then ListPlot knows to just take the position part? – Karl Sep 18 '14 at 21:33
• @Karl See edit please – Dr. belisarius Sep 18 '14 at 21:48
• – Dr. belisarius Sep 19 '14 at 15:26