When a force is applied on a spring, and the length of the spring changes by a differential amount dx, the work done is
For linear elastic springs, the displacement x is proportional to the force applied
where K is the spring constant and has the unit of N/m. The displacement x is measured from the undisturbed position of the spring (that is,  when ). Substituting the two equations 
where x and x are the initial and the final displacement of the spring respectively, measured from the undisturbed position of the spring.