- numpy - matplotlib Physim

Hooke's Law Grapher

In this section you will analyze the results you observed from running the Hooke's Law Simulation by plotting the points of the spring force versus the extension of the string.

Below is a graph of a the spring force simulation run at a spring constant of 0.05, and a mass of 10 kg, and how much the spring force changed as the spring extension was changed. As you can see, the graph outputs a linear graph. This means that there is a liner relationship between the force on the spring and extension of the spring.


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


# import required libraries import matplotlib.pyplot as plt import numpy as np def doafit(force, extension, order): # initialize layout fig, ax = plt.subplots(figsize = (9,9)) # add scatterplot ax.scatter(extension, force, s=60, alpha=0.7, edgecolors="k") params = np.polyfit(extension, force, deg=order) ta = np.array(extension) 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="Extension", ylabel="Force", title="Hooke's Law") ax.grid() return plt # extension and force inputs force = [0.25, 0.185, 0.16, 0.155, 0.13, 0.12, 0.10, 0.09, 0.065, 0.06, 0.055, 0.045, 0.035, 0.025, 0.020, 0.020, 0.015, 0.010, 0.010, 0.005, 0.005] extension = [5, 3.7, 3.2, 3.1, 2.6, 2.4, 2, 1.8, 1.3, 1.2, 1.1, 0.9, 0.7, 0.5, 0.4, 0.4, 0.3, 0.2, 0.2, 0.1, 0.1] deg = 2 doafit(force, extension, deg)

Here is a code editor so that you can input your spring force and extension data points based on your observations made using the Hooke's Law simulation.


This is how the spring force and extension inputs should look like:

force = [0.25, 0.185, 0.16, 0.155, 0.13, 0.12, 0.10, 0.09, 0.065, 0.06, 0.055, 0.045, 0.035, 0.025, 0.020, 0.020, 0.015, 0.010, 0.010, 0.005, 0.005]

extension = [5, 3.7, 3.2, 3.1, 2.6, 2.4, 2, 1.8, 1.3, 1.2, 1.1, 0.9, 0.7, 0.5, 0.4, 0.4, 0.3, 0.2, 0.2, 0.1, 0.1]

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 spring force simulation, graph the force and extension. 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 Hooke's Law worksheet provided in the worksheet section of the Exercises Page.


force = [] extension = [] order = 1 doafit(force, extension, order)


Go Back to Force Exercises Page