# Finding an Expression for the Universal Gravitational Potential Energy — with Calculus and Python

Here is a physics problem. You have an object above the moon and you drop it. What is the final speed before the object hits the surface? Note: this object is NOT the lunar lander since it also has a horizontal motion — but you get the idea. The best way to solve this problem is to use the gravitational potential energy (with respect to infinity) of the object-moon system. This potential looks like this.

In this expression, *r* is the distance from the center of the moon to the object and *G* is the universal gravitational constant (6.67 x 10^-11 N*m²/kg²). But how do you get this expression? I’m going to show you.

Let me start with the work-energy principle. This says that the work done on a system is equal to its change in energy. As an equation, it just looks like this.

If you have a constant force applied to an object over a displacement in a straight line, then the work would be defined as:

Where this is the dot product of two vectors. However, if the force is NOT constant or the displacement is not in a straight line, then you would need to calculate the work as a line integral.

Line integrals can get tricky if you aren’t moving in a straight line, so we will just keep it simple here. Oh, but if you want some more details on line integrals, I have you covered with this older post.

But the cool thing about work is that IF the work done by a force does not depend on the path, but just the end points — then that work can be moved to the other side of the work-energy equation and we will write it as a change in potential (we normally use the symbol U for potential energy).

This means the change in potential (for some special forces for which the work doesn’t depend on the path) can be calculated as:

But notice that the expression for the gravitational potential wasn’t a CHANGE in potential, but just U? That’s because it IS a change in potential. It’s the change in gravitational potential from a point an infinite distance away to some point *r.* So, really, it should look like this:

Yes, I know I have just violated the international treaty on the use of definite integrals by including r in the limits of integration AND in the function. It’s cool though.

OK, we are just about ready to integrate. But we need an expression for the gravitational force. It looks like this.

In this expression, m1 and m2 are the masses and r is a vector from the center of one mass to the other. Since this center-to-center distance is squared, you have to first find the magnitude of this vector. But that means you need r-hat to make the force a vector again.

So, that’s the force but what about the dr vector? In Cartesian coordinates, the dr vector is going to be the following.

I wrote this vector two ways so that you can see that I’m flexible and open minded.

Now, let’s imagine that I want to find the change in potential for an object moving in a straight line. It’s going to start at x = infinity and move down to x = r, so everything is in the x-direction.

Since I’m moving in the x-direction, I can write the gravitational force as:

Now I have the force in cartesian coordinates, I can take the dot product with the displacement vector (dr).

This is better. I have a scalar function in the x-direction and limits in terms of x. It’s pretty much win-win. Let me put it all together to find the change in potential energy (don’t forget that negative out front).

OK. I can integrate that. Let’s do it.

Boom. That’s it. Win.

But wait! I’m going to do this again. Let’s do it numerically. Of course, by numerically I mean breaking the problem into a bunch of small steps. During each step, I will make some approximations that aren’t exactly true. However, if the step sizes are small, everything should work out fine.

The key to a numerical calculation is “numbers”. Yes, you have to use actual values. So, I am going to use an object with a mass of 1 kg moving towards the moon (with a mass of 7.347 x 10²² kg). I want to start at “infinity” and move to a distance of twice the radius of the moon (radius = 1.7371 x 10⁶ m).

So, here’s how this numerical calculation is going to work.

- Start at some numerical value for the distance. It can’t actually be infinity, so I will just pick some large value. Right now, I’m thinking about 100 times the radius of the moon.
- Pick some step size for the distance. Again, I’m just using my creativity for a value here — I’m feeling 100 meters. Maybe that won’t work — if that’s the case, I will pick something else.
- Now I want to calculate the gravitational force on my 1 kg object at this distance and assume it’s constant over my step size. Use this value to calculate the work (done by a constant force over a straight line — the easiest kind of work).
- Add this work to the total work (which started at 0 Joules).
- Move to the next position and repeat.
- Once I get to the final position, I can take the negative of this work (since the potential is the negative of work).

Simple, right? Oh, I didn’t pick a final position. Let’s go with the ending point being on the surface of the moon (so the final x-position is the radius of the moon).

Of course, no one wants to do thousands of calculations — so, I’ll just make a computer do it. Yes, I’m going to do this in python. It’s cool. Here is the code for this. I’m going to go over the important parts below.

Here are some comments.

- Line 3–5 just sets up the graph. Here is everything you need to know about graphs in python.
- Lines 6–12 are really just constants and stuff. Nothing exciting.
- Line 13 (
`U = 0`

) just sets up the potential sum so that I can add to it. - Line 15 is the loop.
- Line 16, calculate the force.
- Line 17–18, calculate the negative of the work and add it to the total potential.
- The rest is boring.

OK, check out the plot. This is both the numerical AND the theoretical calculation.

Those two plots mostly agree. I’m calling it a win. But seriously, go play with the code — that’s how you really learn stuff.