Let’s say we want to model the motion of the moon around the Earth. Let’s get started with the following GlowScript program.
Of course, the program won’t run – it is just a starter program. You will need to modify it in order to get it to function correctly. I will go over some of the key parts.
This part isn’t special. It’s just putting down all the constants – such as the mass of the Earth, the radius of the Earth and stuff like that. The “rem” in line 8 is the distance from the Earth to the moon.
The next part (lines 12-17) create a 3D object for both the moon and the Earth. For the Earth, I put the position at vec(a,a,a) – this is wrong. You need to put some values (or variables) in for these “a”. The same is true for the position of the moon. Decide where it should be and change the pos=vec(a,a,a) to something meaningful.
Line 16 and 17 set the mass of the Earth and moon.
After this, there is a part where you need to also set the initial momentum of the moon (and maybe the Earth). It might help if you sketch out a diagram of the Earth and moon along with an x-y axis. Which direction do you want the moon to be initially moving?
You need to set the starting time to 0 (as in line 23) – but what about the time step? I will let you think of an appropriate time step – you can always change it later. Oh, if you are thinking about a time step of dt=0.01 seconds – that’s a bad idea. Your program will run very slowly.
Before getting to the loop of the program, you might want to comment out all lines after line 25 and run the program. This will let you know if the stuff you have added has any errors in it.
Now for the main part of the calculation. Remember that we want to do the following:
1. Calculate the vector that goes from the Earth to the moon. We need to do this in each step because this r vector changes as the moon moves.
2. Calculate the force on the moon. If r is a vector from the Earth to the moon, then the gravitational force will be:
Here we need both the magnitude of r – calculated as mag(r) and the direction of r – calculated as norm(r).
3. Update the momentum. If I know the force and the time interval, you can use the momentum update formula:
Or perhaps you want to use the acceleration. If so, first calculate the acceleration:
And then calculate the new velocity:
4. Now we need to calculate the new position of the moon. Here is the position update formula using the momentum principle:
or with the acceleration and velocity:
5. Now update the time.
Now for some final notes about the sample code.
Here is the start of the loop. While True means to repeat the loop forever. The rate statement (in line 29) tells the program how many calculations to do every second. You need to change “a” to something else.
Here is a line in which I calculate the vector r
Now you just need to add in the missing pieces.
Suppose you want to do some more things?
- Try changing the initial velocity of the moon. What happens?
- What if the moon starts at a different location?
- What if the Earth starts at a different location?
- Calculate the force the moon exerts on the Earth (this would just be the opposite of the force the Earth exerts on the moon).
- What if the moon and Earth have about the same mass?
- Try adding a second moon into your program (if you get this to work, you are a master of programming). Note that each object will have two different gravitational forces on it.