Stored energy in a spring for constant applied force While studying this paper for ideal capacitor I came across this in the first page (which is available for preview)-

Mechanically, Fig. 1 (a) is equivalent
to an idealized spring of spring constant $k$ without mass
or friction. When a constant force $F_0$ is suddenly applied to this
spring and it is either compressed or stretched by a distance $d$, the
same situation results. That is, the work done by the force is $F_0 d$,
while the energy stored in the spring is $kd^2/2 = F_0d/2$.

The work done by a constant force of magnitude $F_0$ on a point that moves a displacement
$d$ in the direction of the force is simply the product $W=F_0d$, so it makes sense. But I cannot understand how the energy being stored in the spring is $\frac{kd^2}{2}$. If I were to integrate the elastic potential energy being stored by regular intregation- $$W=\int_{0}^{d} F_0 \cdot dx= F_0\int_{0}^{d}dx=F_0d$$
Am I missing something? How is the energy being stored for a constant force $F_0$ is $kd^2/2$?
If  $kd^2/2 = F_0d/2$, doesn't that mean $F_0 = kd$? But we said $F_0$ is constant.
Any help is appreciated.
 A: The energy stored can be found from integration, but the force needed to compress the spring is $F=kx$ (Hookes law, it's harder to compress a spring by 1cm, when it's already compressed), so the energy stored in the spring is
$$W=\int_{0}^{d} kx \cdot dx= k\int_{0}^{d}xdx=\frac{kd^2}{2}$$
Since $F_0d$ is twice as big as this, presumably some kinetic energy would be created as well if the compressing force $F_0$ were really constant.
A: This conceptual question is asked frequently on this site—search spring energy half here for many other answers. The confusion generally arises because when dealing with the ideal spring and a constant load, half the applied work seems to magically disappear into heat (strictly, thermal energy). We see below that this is the only reasonable conclusion for our models and idealizations to mesh; nevertheless, one might ask: How does the spring (or capacitor, in the twin example of an idealized circuit with a constant voltage) know to turn half the work into heat? It's a fun puzzle that can be explored in the undergrad curriculum of mechanical engineering, electrical engineering, or physics, for instance.
Let's build up the evidence leading to this conclusion:

*

*We can certainly apply a constant force on both ideal and real springs (for simplicity, by "real springs" I mean springs with mass and friction that still deflect linearly); consider hanging a weight on the end, for example. In addition, modeling expediency requires the existence of ideal forces not associated with any physical mass that needs to be accelerated.


*The strain energy stored in both ideal and real springs is $\boldsymbol{\frac{kx^2}{2}}$. We obtain this by stretching the spring quasistatically and reversibly, applying varying force $F=kx$ from zero deflection to the final deflection $d$. This approach maintains constantly balanced forces and thus avoids energy dispersion to the environment. The integration step is shown in @JohnHunter's answer. (This is the only time a varying force is used in this answer.)


*The ideal spring has no mass and thus no inertia. It cannot accelerate to produce gradual motion. For an applied force, all responses occur instantaneously, producing immediate and final deflection $d=F/k$. (In contrast, real springs have mass and internal friction; when a constant force is applied, the real spring accelerates and exhibits damped oscillation to ultimately obtain its equilibrium final position, making the frictional losses obvious.)


*A constant force applied over deflection $x$ corresponds to work $Fx$. We know from above that half of this work goes into strain energy. What about the other half? For a real spring, we can apply the laws of motion and obtain the solution for damped motion, concluding that the other half goes into some time-varying combination of kinetic energy and thermal energy. (If the spring is critically damped or overdamped, then no kinetic energy remains at $d$.)


*Finally, how should we treat the ideal spring, for which no motion over time can occur? The only reasonable reconciliation is that in addition to completing instantaneous deflection $d$ (instantaneously storing strain energy $\frac{kx^2}{2}$), we also complete instantaneous dissipation $\boldsymbol{\frac{kx^2}{2}}$ into thermal energy.
In other words, idealizing the ideal spring as massless and frictionless, in addition to assuming the existence of incorporeal ideal forces, implies that certain dissipative dynamics occur instantaneously, leaving only their outcome; it's all part of the ideal spring model "package."
In the end, the automatic division into halves arises from the assumptions of linear elasticity, the definition of work, and the law of conservation of energy.
