[Physics] Work done by a spring

newtonian-mechanicsspringwork

I've done enough research before asking this question. The work done by a spring is defined as $$W_\mathrm{spring}=\left|\frac{1}{2}kx^2\right|$$
Where $ k$ is the spring constant and $x$ is the distance moved by the spring.
But when I tried to derive the eqñ I'm getting $|kx^2|$ and no half is present.
I took into consideration that there is displacement at both ends of the spring even when the force is exerted at only one end but still I end up at this eqñ.
Can anyone help me out?

Best Answer

Say $W$ is:

$$W=\frac{1}{2}kx^2$$

Then:

$$\frac{dW}{dx}=\frac{1}{2} k \times (x^2)'=\frac{1}{2} k \times 2x=kx$$.

But is it was restoring force $F$ you were looking for, then:

$$F=-\frac{dW}{dx}=-kx$$

Inversely:

$$W=\int Fdx=\int(-kx)dx=-k\int xdx= -k\frac{1}{2}x^2=-\frac{1}{2}kx^2$$

(If integrated between the correct boundaries). The sign is a matter of convention.