- numpy - matplotlib Physim

Newton's Law for Gravitational Attraction Grapher

In this section you will analyze the results you observed from running the Newton's Law for Gravitational Attraction Simulation by plotting the points of the gravitational force versus the distance of the planetary objects.

Below is a graph of a the gravitational force simulation run at two planetary objects weighing to be 1962 kg and 478 kg, and how much the gravitational force changed as the distance between the planetary objects increases. As you can see, the graph starts out with a high gravitational force when the planetary objects were close to each other, however as the planetary objects moved farther away from each other, the gravitational force started to become weaker.


If you would like to see the full code for plotting the gravitational force and distance graph, click the Display Code button below:


# import required libraries import matplotlib.pyplot as plt import numpy as np def doafit(force, distance, order): # initialize layout fig, ax = plt.subplots(figsize = (9,9)) # add scatterplot ax.scatter(distance, force, s=60, alpha=0.7, edgecolors="k") params = np.polyfit(distance, force, deg=order) ta = np.array(distance) xseq = np.linspace(ta.min(), ta.max(), num=100) # plot regression line ff = 0. for i in range(len(params)): ff += params[i]*np.power(xseq,order- i) ax.plot(xseq, ff, color="k", lw=2.5) # axis and title plot labelling ax.set(xlabel="Distance", ylabel="Force", title="Newtons's Law for Gravitational Attraction") ax.grid() return plt # distance and force inputs force = [278195502.88e-11, 100150381.04e-11, 25037595.26e-11, 14815145.12e-11, 11127820.12e-11, 6259398.81e-11, 1564849.7e-11, 695488.76e-11, 391212.43e-11, 250375.95e-11, 97803.11e-11, 62593.99e-11, 27819.55e-11, 15648.5e-11, 10015.04e-11, 6954.89e-11, 5109.71e-11, 3912.12e-11, 3091.06e-11, 2503.76e-11] distance = [0.15, 0.25, 0.50, 0.65, 0.75, 1, 2, 3, 4, 5, 8, 10, 15, 20, 25, 30, 35, 40, 45, 50] deg = 2 doafit(force, distance, deg)

Here is a code editor so that you can input your gravitational force and distance data points based on your observations made using the Newton's Law for Gravitational Attraction simulation.


This is how the gravitational force and distance inputs should look like:

force = [278195502.88e-11, 100150381.04e-11, 25037595.26e-11, 14815145.12e-11, 11127820.12e-11, 6259398.81e-11, 1564849.7e-11, 695488.76e-11, 391212.43e-11, 250375.95e-11, 97803.11e-11, 62593.99e-11, 27819.55e-11, 15648.5e-11, 10015.04e-11, 6954.89e-11, 5109.71e-11, 3912.12e-11, 3091.06e-11, 2503.76e-11]

distance = [0.15, 0.25, 0.50, 0.65, 0.75, 1, 2, 3, 4, 5, 8, 10, 15, 20, 25, 30, 35, 40, 45, 50]

order = 1 means linear fit (i.e. polynomial of degree 1)

order = 2 means quadratic fit (i.e. polynomial of degree 2)

doafit is what generates a fit line or curve based on the time and height inputs.


In the gravitational force simulation, graph the force and distance. What do you notice in the shape of the curve? is it linear? parabolic?

Do you notice any similarites or differences in your graph compared with the graph above?

Record your observations in the Newton's Law for Gravitational Attraction worksheet provided in the worksheet section of the Exercises Page.


force = [] distance = [] deg = 2 doafit(force, distance, deg)


Go Back to Force Exercises Page