# Planet's Moon attrated by sun [closed]

I'm currently writing a code to generate solar system and $N$ number of planets / moons.

I use real data to test (earth / sun / moon data).

I succeeded in placing the earth and make it orbit around the sun using the correct mass and velocity. But when I place the moon around the earth and launch the simulation, the moon is a kind of ignoring Earth.

At start $(x,y)$:
Sun : 0,0
Earth : 1.4959826e8
Moon : 1.4959826e8-384400

using $F = (G \times a.mass \times b.mass) / (dist \times dist)$

At start $F$:
Sun & Earth: 3.5855635298968626E22
Earth & moon : 1.9817101152925866E20
Moon & Sun: 4.379973928784021E20

Since I placed them on $y=0$, moon has an $f_y$ of $0$ and $f_x$ of -2.39826381349143456E17 (because she's placed between sun and earth

At start, the moon has a velocity of 0.001022 km/sec

After velocity update ($v = v + dt \times F / mass$) $v =$ 0.28200845732138924
$v_x =$ -0.28200660544886147
$v_y =$ 0.001022

So regarding the result, it make sense that the moon is attracted more by the sun than the earth (since vx is negative), but why? Where in my formulas I make a mistake ? I tested also to start the simulation with the velocity of moon = 29.783 + 0.001022 , (earth velocity) but then the moon just turn around the sun like the earth

(I surely forgot to put some info, don't hesitate to tell me to add things , I'm not really used to ask physics questions)

(Here's a video if you want an idea of what happen)

-
Hey eephyne. I think you know that we use a TeX markup called MathJax, same as Math.SE. The markup is very much helpful in understanding equations, etc. Please have a look here for an introductory, or atleast have a look at our FAQ for an overview. For now, I'll help revising your post. – Ϛѓăʑɏ βµԂԃϔ Jan 28 at 7:30
thanks for the edit @Crazy Buddy , I'll do it next time – eephyne Jan 28 at 7:40
That's OK eephyne. My edit is somewhat minor. You can add many other markups if you require ;-) – Ϛѓăʑɏ βµԂԃϔ Jan 28 at 7:43
The behavior in the video certainly looks wrong (the gray dot is the Moon, right?) -- but the Moon does pretty much orbit the Sun. From an Earth-centered frame of reference, the Moon is in a basically elliptical orbit around the Earth. From a Sun-centered frame of reference, the Moon's path around the Sun is distorted by the Earth's gravity, but the path is still convex. – Keith Thompson Jan 28 at 8:29
I'm going to close this at this point because it has become clear the the issue was computational, and computational questions are off topic. The stupid automatic downvote will time out eventually. – dmckee Jan 29 at 17:47

## closed as off topic by dmckee♦Jan 29 at 17:47

Questions on Physics Stack Exchange are expected to relate to physics within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

## 2 Answers

The velocity of the Moon is obviously about the same as the Earth because it orbits at about the same distance. If you set the Moon's initial velocity to it's orbital velocity relative to the Earth (NB 1.022km/sec not 1.022m/sec as you state) then it will just fall towards the Sun, as indeed you've found.

You say:

I tested also to start the simulation with the velocity of moon = 29.783 + 0.001022 , (earth velocity) but then the moon just turn around the sun like the earth

Well you would expect the Moon to orbit the Sun just like the Earth. The orbit of the Moon looks extremely similar to the Earth because the distance from the Earth to the Moon is just 0.25% of the distance from the Earth to the Sun. The movement around the Earth is a small perturbation of the orbit round the Sun.

-
 the problem is that with the velocity of moon+earth , the moon do not stick around earth, it doesn't behave like a satelite of earth. (i mean by that , that the path between the two is not the same and it's visible after a little time , i can post video to see if you like – eephyne Jan 28 at 8:51 The trouble is that for an object at the distance of the Moon the escape velocity from the earth is only 1.4 km/sec. So you need to get the Moon's velocity just right. I would start with the Earth and Moon at the same distance from the Sun, so the Moon is 384,400 km further round the orbit. At this point the velocity tangential to the orbit will be the same as the Earth, and the velocity radial to the orbit will be about the orbital velocity of 1.022 km/sec. – John Rennie Jan 28 at 9:05 i just tested that and the same happen , the moon orbit around the sun without noticing the earth , it's orbit speed is faster (because of the mass I guess). For the moon to remain in the same distance from earth , i need the force to be almost null , no? – eephyne Jan 28 at 15:12

I just understand why this was happening.

I use the BarnesHut method and it doesn't work here , I don't really know why , the only explanation I see is because the tree ignore some force update sometimes , I tried to look out but didn't managed to get why.

Anyway when I use brute force on it , it's work like a charme (here's a video)

-
Barnes-Hut should work, but it is definitely overkill for this problem. It is meant as a very good and efficient approximation for $n$-body simulations with $n\sim10^6$ or more. Moreover, Barnes-Hut does not specify which precise updating methodology to use. For more, you should check out scicomp.stackexchange.com, perhaps starting with this question – Chris White Jan 29 at 18:11