GlowScript Tutorial 8: Gravity and Orbits

How do you model the Earth-moon system? Here is a tutorial using GlowScript to model the motion of the moon around the Sun.
Yes this is a model of the Earth and moon to correct scale.
Yes, this is a model of the Earth and moon to correct scale.Yes, this is a model of the Earth and moon to correct scale.

Wait, where are the GlowScript tutorials 1-7? Yes, I am sort of skipping around. I put the first GlowScript tutorials on a different server, but I figure it's best just to keep everything in one place. If you want to start from the beginning, here are the previous GlowScript tutorials.

What is GlowScript? This is an web-based programming tool that is essentially the same as VPython and it's awesome. Ok, now to continue with the tutorial.

Today let's look at gravity. No, not that boring constant gravity where F = mg. We will look at a better model for gravity. This says that if I have two objects with mass, there is a gravitational force that pulls them together. Suppose my two objects look like this:

Sketches Fall 14 key

First, it should be clear that mass 1 pulls on mass 2 with the same force that 2 pulls on 1 (because they are the same force). Second, this vector r is from mass 1 to mass 2. Now I can write an expression for the gravitational force on mass 2.

La te xi t 1

Here the "r-hat" is a unit vector in the direction of the vector r and G is a constant with a value of 6.67 x 10-11 N*m2/kg2. But enough of gravity. Let's make a program. Here is the start of a GlowScript program that makes the Earth and the moon (instead of mass 1 and 2).

Glow Script ide

If you went through the previous tutorials, there shouldn't be any surprises here. You can run the code and you will see just how far away the moon is from the Earth. It's rarely shown like this because it's difficult to see both the Earth and moon to scale at the correct distance.

Now we want to make things move. If the moon and Earth started from rest, the moon would eventually crash into the Earth. Let's do that. Really, the only difference between this model and projectile motion is that you have to keep calculating the gravitational force. Before you do that, you should calculate the vector distance from the Earth to the moon. I called this "r". Be careful. Don't assume the Earth is always at the origin (trust me on this one).

Once you have an expression for r, the gravitational force on the moon would be:

Glow Script ide

Notice that I have r over the magnitude of r3. This is the same as r-hat over r2 (check it for yourself). What else do you need to do? Here is a reminder:

  • Calculate the change in momentum of the moon.
  • Calculate the change in momentum of the Earth (remember you don't have to re-calculate the gravitational force, it's the same but in the opposite direction).
  • Update the position of the Earth and Moon.
  • Update time.

Speaking of time, what about the time step? If you pick something like a time interval of 0.01 seconds, this will take forever to run. I suggest a time interval of 1 hour. That would be 60*60 seconds (you still need to use the value in units of seconds). For the rate statement, use something like rate(1000) unless you want it to run in real time. Actually, a time step of 1 hour is too long. When the moon is zooming along quite fast you will get to the point where it travels greater than the diameter of the Earth in under an hour. This would be bad for one time step. Change the time interval to

What about the loop? Here is the beginning of my loop.

Glow Script ide

Instead of having a time limit on the loop, this one runs until the distance between the centers of the Earth and moon is less than the sum of their radii. Oh, one more tip. Make sure you calculate the r vector inside the loop. Also, if you print the final time you can check and make sure your program functions. When I run mine I get a time to collision of 416460 seconds. You might get a different value if you have a different time step.

What about another check? Here is a plot of the x-component of momentum as well as the total momentum (see GlowScript tutorial 3 on graphing).

Glow Script ide

Can you make a plot of total energy (including gravitational potential)? Is energy conserved? In case you can't recall, this is the definition of gravitational potential energy.

La te xi t 1

Also, both objects will have kinetic energy but there is only one potential energy term. I guess you would figure that out if you plotted the total energy.

Orbits

Yes, the moon doesn't crash into the Earth. Instead it orbits the Earth. But what's different about the moon that makes it not crash into the Earth? There is only one thing - the initial velocity.

Try this one change to your program. Change the initial momentum of the moon like this:

Glow Script ide

This gives the moon an initial velocity in both the y and negative x directions. Run it. You should get something cool like this.

Glow Script ide

But that's not a circular orbit. What speed would it need to be in a circular orbit? Let's draw a diagram (not to scale).

Sketches Fall 14 key

There is only one force on the moon and it is pointing towards the Earth (which is mostly the center of the circle). Since the moon is moving in a circle, the acceleration would be centripetal acceleration. If I set the gravitational force equal to the mass times the acceleration, I get:

La te xi t 1

I know the mass of the Earth the radius of the circular motion. I can just put in my values to get the velocity. As for the direction of the initial velocity, the moon starts on the x-axis so it needs to be moving in the y-direction. You don't even have to calculate the value of this velocity. Just have GlowScript do the hard work.

Glow Script ide

Boom. A circle. But wait! Does the Earth move too? Yes, indeed it does (and it should). If you did your calculations correctly, there should be a force on the Earth and a change in momentum of the Earth. But there is still a problem. Here is a plot of y-component of momentum so that you can see the problem.

Glow Script ide

The red curve represents the total y-momentum. Clearly the y-component of the Earth and moon add up to the red line - that's good. However, the total y-momentum is NOT zero. This means that the Earth-moon system is moving along in the positive y-direction. Why does it do this? The reason is simple. Since you gave the moon an initial y-momentum, the total initial y-momentum was not zero. How can you fix this? What if you make the total initial y-momentum equal to zero by giving the Earth an initial velocity in the y-direction? Figure out what velocity you would need to do this.

Now for some more homework.

  • How long does it take the moon to make one orbit? Convert your answer from seconds to days.
  • What does the Earth's motion look like? You can get an idea by plotting Earth.pos.x vs. Earth.pos.y (the trajectory).
  • The moon doesn't actually have a circular orbit around the Earth. See if you can make a more accurate model of the Earth-moon system by looking up better starting values for velocity.
  • What if you wanted to include another object in this model? Maybe you have an asteroid you want to put in there. Can you model these three objects?
  • Super awesome question: is there a location and initial velocity that you can place this asteroid such that it stays in the same location relative to the Earth and moon? Hint: this is called a Lagrange point. Here is some extra info.
  • What if you want to model the Earth-moon-Sun system? Can you get that to work? Does the moon orbit the Sun or the Earth? Here is the answer.