8
$\begingroup$

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}]
$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – kglr Sep 16 '14 at 2:58
  • $\begingroup$ 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! $\endgroup$ – Dr. belisarius Sep 16 '14 at 18:13
12
$\begingroup$

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}]

enter image description here

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}}]

Mathematica graphics

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]];
$\endgroup$
  • $\begingroup$ That makes a lot of sense, thank you. I don't actually know what the use of the @# is in the NestList function? $\endgroup$ – Karl Sep 16 '14 at 18:08
  • $\begingroup$ @Karl stackoverflow.com/a/4272181/353410 $\endgroup$ – Dr. belisarius Sep 16 '14 at 18:20
  • $\begingroup$ 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? $\endgroup$ – Karl Sep 18 '14 at 21:33
  • 1
    $\begingroup$ @Karl See edit please $\endgroup$ – Dr. belisarius Sep 18 '14 at 21:48
  • 1
    $\begingroup$ @eldo The culprit is Kepler $\endgroup$ – Dr. belisarius Sep 19 '14 at 15:26

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.