Doing Math with Python

math python jupyter

Graphing the Relationship Between Gravitational Forces and Bodies

The relationship between gravitational force and distrance between two bodies

We want to graph the results so we need to import matplotlib

import matplotlib.pyplot as plt

Next we need to initialize the graph that we want to plot

def draw_graph(x,y):
    plt.plot(x, y, marker='o')
    plt.xlabel('Distance in meters')
    plt.ylabel('Gravitational force in newtons')
    plt.title('Gravitational force and distance')
    plt.show()

Next we need to setup the range for the results and define constants.
Lastly we want to create a list of values for each value of the range r.

def generate_F_r():
    # Generate values for r
    r = range(100,2001, 50)
    # Empty list to store the calculated values of f
    F = []

    # Constant, G
    G = 6.674*(10**-11)
    # Two masses
    m1 = 0.5
    m2 = 1.5

    # Calculate force and add it to the list, F
    for dist in r:
        force = G*(m1*m2)/(dist**2)
        F.append(force)

    # Call the draw_graph function
    draw_graph(r, F)

Then we call the function to see the results.

if __name__=='__main__':
    generate_F_r()

This is an image