-1
$\begingroup$

Thanks to @AlexTrounev, I learned how to use ListAnimate, but I still get stuck using it. Before ListAnimate everything is correct. I just cannot successfully use ListAnimate to make the plot. I have checked the format in each argument and I am very sure the code in the ListAnimate function is very close to the correct. However, it does not plot anything for me. I am confused how should I modify this ListAnimate function. The code before ListAnimate is straightforward (please ignore the detail). I really appreciate anyone's help.

PMotion[\[Mu]1_, {x0_, y0_, vx0_, vy0_}, tmax_, step_: 1000] :=

  Module[{rrule, Urule, eqMotion, r1, r2, \[Mu], U},
   rrule =
    {r1 -> Sqrt[(\[Mu] + x[t])^2 + y[t]^2],
     r2 -> Sqrt[(-1 + \[Mu] + x[t])^2 + y[t]^2]};
   Urule = {U -> (1 - \[Mu])/r1 + \[Mu]/r2 + 1/2 (x[t]^2 + y[t]^2)}; 
   eqMotion =
    {x''[t] - 2 y'[t] == D[U /. Urule /. rrule, x[t]],
     y''[t] + 2 x'[t] == D[U /. Urule /. rrule, y[t]]};
   NDSolve[{eqMotion /. \[Mu] -> \[Mu]1, x[0] == x0, y[0] == y0,
       x'[0] == vx0, y'[0] == vy0} //
      Flatten, {x, y}, {t, 0, 
      tmax},
     MaxSteps -> step] // Flatten];

M1 = 1.989*^30;
M2 = 5.972*^24;
\[Mu] = M2/M1;
G = 1;
solSolar = 
  PMotion[\[Mu], {1 - \[Mu] + 1/389, 0, 0, Sqrt[(
    G (\[Mu]/(1 + \[Mu])))/(1/389)]}, 18 \[Pi], \[Infinity]]; 
fxSolar[t_] := x[t] /. solSolar;
fySolar[t_] := y[t] /. solSolar;

tf=2 \[Pi]
ListAnimate[
 Table[
  Graphics[{White, Rectangle[{0.995, 1.004}, {-0.0035, 0.0035}],
    Purple, Thin,
    Line[Table[{x[t], y[t]} /. solSolar, {t, 0, tn, 0.01}]],
    Black, PointSize[Medium],
    Point[{x[tn], y[tn]} /. solSolar]}],
  {tn, 0.1, tf, .01*tf}
  ]
 ]
$\endgroup$
  • 1
    $\begingroup$ Did you just dump the code and told us to fix it? Have you tried anything? You can remove ListAnimat and see that it is not related at all. Repeat that process till you find the problem $\endgroup$ – Kuba Feb 12 at 8:07
  • $\begingroup$ Before ListAnimate everything is correct. I just cannot successfully use ListAnimate to make the plot. I have checked the format in each argument and I am very sure the code in the ListAnimate function is very close to the correct. However, it does not plot anything for me. I am confused how should I modify this ListAnimate function. $\endgroup$ – Yunlin Zeng Feb 12 at 8:37
4
$\begingroup$

Do you use software to switch between English and another language when typing? I work with a few Chinese students and have seen something similar a couple of times. Sometimes their keyboard gets switched back to Chinese, and there's a character that looks very similar to a comma, but is actually different.

The Stack Exchange formatting actually makes it easier to see - it's less noticeable in Mathematica itself. At the end of your Line[Table[{x[t], y[t]}/. solSolar, {t, 0, tn, 0.01}]] line in ListAnimate, you have a character that is not a comma. If you switch it to a comma, it should work. Well, it outputs something anyways.

EDIT: I probably should have included my process in the answer to start with in case it helps someone in the future. I removed the ListAnimate so that it would just output the raw graphics themselves so that I could make sure those were being produced properly. I also increased the step size on the Table as I only wanted a couple of frames, and not a huge list of frames.

I ended up with 10 graphics each surrounded with a red border indicating Mathematica encountered some kind of problem. When I hovered over one of them with my mouse, it says Times is not a Graphics primitive or directive. That part actually just took experience. Mathematica interprets spaces as being multiplication, so that's what it means by Times. I started looking for a missing comma, and just happened to recognize that one of the commas looked different. I've almost pulled my hair out trying to figure out what was wrong with my friends' codes before because everything looked correct, but the commas were a slightly different shape.

$\endgroup$
  • $\begingroup$ @YunlinZeng I've added more information on how I actually found the answer, if it helps. $\endgroup$ – MassDefect Feb 12 at 9:02
  • $\begingroup$ Thank you very much. I thought I made a very silly mistake and just about to delete this post. But your process of debugging is very illuminating. Those information really helps! @MassDefect $\endgroup$ – Yunlin Zeng Feb 12 at 9:22
1
$\begingroup$

It is necessary to correctly determine the area of motion and animation parameters.

PMotion[\[Mu]1_, {x0_, y0_, vx0_, vy0_}, tmax_, step_: 1000] := 
  Module[{rrule, Urule, eqMotion, r1, r2, \[Mu], U}, 
   rrule = {r1 -> Sqrt[(\[Mu] + x[t])^2 + y[t]^2], 
     r2 -> Sqrt[(-1 + \[Mu] + x[t])^2 + y[t]^2]};
   Urule = {U -> (1 - \[Mu])/r1 + \[Mu]/r2 + 1/2 (x[t]^2 + y[t]^2)};
   eqMotion = {x''[t] - 2 y'[t] == D[U /. Urule /. rrule, x[t]], 
     y''[t] + 2 x'[t] == D[U /. Urule /. rrule, y[t]]};
   NDSolve[{eqMotion /. \[Mu] -> \[Mu]1, x[0] == x0, y[0] == y0, 
       x'[0] == vx0, y'[0] == vy0} // Flatten, {x, y}, {t, 0, tmax}, 
     MaxSteps -> step] // Flatten];

M1 = 1.989*^30;
M2 = 5.972*^24;
\[Mu] = M2/M1;
G = 1;
solSolar = 
  PMotion[\[Mu], {1 - \[Mu] + 1/389, 0, 0, 
    Sqrt[(G (\[Mu]/(1 + \[Mu])))/(1/389)]}, 18 \[Pi], \[Infinity]];
fxSolar[t_] := x[t] /. solSolar;
fySolar[t_] := y[t] /. solSolar;

tf = 2 \[Pi];
 ListAnimate[
  Table[Graphics[{Blue, Rectangle[{.996, -.0035}, {1.004, 0.0035}], 
     White, Thin, 
     Line[Table[{x[t], y[t]} /. solSolar, {t, 0, tn, 0.005}]], Red, 
     PointSize[Large], Point[{x[tn], y[tn]} /. solSolar]}], {tn, 0.01,
     tf, .005*tf}], 10]

fig1

$\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.