← Back to home

Hooke’s law: measuring a spring constant

Fit force against extension to estimate the spring constant from the slope. The lower panel shows residuals, helping you spot patterns that a single best-fit line can hide. The fit assumes independent force uncertainties and does not account for uncertainty in extension.

Force versus extension with a fitted line and residuals

Download the Python source.

"""Fit spring-force measurements and inspect the residuals."""

from labplotlib import Measurement, apply_theme, finish_plot, polynomial_fit, scatter_with_residuals


def main() -> None:
    extension = [0.01, 0.02, 0.03, 0.04, 0.05, 0.06]
    force = Measurement([0.24, 0.48, 0.73, 0.97, 1.22, 1.44], 0.04)

    fit = polynomial_fit(extension, force)
    print(f"Spring constant: {fit.coefficients[0]:.2f} N/m")
    print(f"Intercept: {fit.coefficients[1]:.3f} N")
    print(f"Reduced chi-squared: {fit.reduced_chi_squared:.2f}")

    apply_theme()
    fig = scatter_with_residuals(
        extension, force, title="Hooke's law", xlabel="Extension (m)", ylabel="Force (N)"
    )
    finish_plot(fig, filename="hookes_law.png", show=True)


if __name__ == "__main__":
    main()

The sample data give a spring constant of about 24.17 N/m.