understanding experimental data when plotdata('springdata txt'is runit produces the plot on the left this is not what hooke' law predicts hooke' law tells us that the distance should increase linearly with the massi the points should lie on straight line the slope of which is determined by the spring constant of coursewe know that when we take real measurements the experimental data are rarely perfect match for the theory measurement error is to be expectedso we should expect the points to lie around line rather than on it stillit would be nice to see line that represents our best guess of where the points would have been if we had no measurement error the usual way to do this is to fit line to the data using linear regression to find fit whenever we fit any curve (including lineto data we need some way to decide which curve is the best fit for the data this means that we need to define an objective function that provides quantitative assessment of how well the curve fits the data once we have such functionfinding the best fit can be formulated as finding curve that minimizes (or maximizesthe value of that functioni as an optimization problem (see and the most commonly used objective function is called least squares let observed and predicted be vectors of equal lengthwhere observed contains the measured points and predicted the corresponding data points on the proposed fit the objective function is then defined as!"!"#$%&$!(!"#$%&$!"#$%&'#)!!squaring the difference between observed and predicted points makes large differences between observed and predicted points relatively more important than small differences squaring the difference also discards information about whether the difference is positive or negative how might we go about finding the best least-squares fitone way to do this would be to use successive approximation algorithm similar to the newtonraphson algorithm in alternativelythere is an analytic solution that is usually applicable but we don' have to use eitherbecause pylab provides built-in functionpolyfitthat finds the best least-squares fit