Calculus of variation with discontinuous solutions?

I'm thinking of the following question:

Consider a function $$f: U\rightarrow\mathbb{R}$$ where $$U=[0,L_1)\cup(L_1,L]$$, and an energy functional $$F=\int_{U}\Big (\frac{\mathrm{d}f}{\mathrm{d}x}\Big)^2\mathrm{d}x.$$ The boundary condition is $$f(0)=f(L)=0$$. Also, assume $$f$$ has a jump discontinuity at $$x=L_1$$, i.e., $$f(L_1^-)=-f(L_1^+)=-a$$, where $$a$$ is a constant, and $$L_1$$ is a variable.

The question is: what is the minimum energy?

Simple answer: the Euler-Lagrange equation on $$[0,L_1)$$ and $$(L_1,L]$$ is $$\frac{\mathrm{d}^2 f}{\mathrm{d}x^2}=0,$$ Therefore we have the following solution: $$f(x) = \begin{cases} -\frac{a}{L_1}x, & \text{x\in [0,L_1)} \\ -\frac{a}{L-L_1}x+\frac{L}{L-L_1}a, & \text{x\in (L_1,L]} \end{cases}$$ Then the energy function $$F(L_1)=\frac{a^2}{L_1}+\frac{a^2}{L-L_1}$$. Therefore we have the minimum energy $$4a^2/L$$ when $$L_1=L/2$$.

Then I tried Fourier series. The Fourier series for the discontinuous solutions are $$f(x)=\sum_{k=1}^{\infty}\Big[\frac{4a}{k\pi}\cos\frac{k\pi L_1}{L}+\frac{2aL}{(k\pi)^2}\Big(\frac{1}{L-L_1}-\frac{1}{L_1}\Big)\sin\frac{k\pi L_1}{L}\Big]\sin\Big(\frac{k\pi x}{L}\Big),$$ When we substitute this representation into the energy functional, we get infinite energy. In particular,\begin{align} F(L_1=L/2) & =\sum_{k=1}^{\infty}\Big(4a\cos\Big(\frac{k\pi}{2}\Big)\Big)^2\cdot\frac{1}{2L}\\ & =\sum_{k=1}^{\infty}\frac{4a^2}{L}(1+\cos k\pi)~\mathrm{e}^{-\lambda_1 k} & \text{\lambda_1\to0^+}\\ & \to\frac{4a^2}{L}\cdot\frac{1}{\lambda_1}\\ \end{align} \begin{align} F(L_1=L/4) & =\sum_{k=1}^{\infty}\Big(4a\cos\Big(\frac{k\pi}{4}\Big)-\frac{16}{3}\frac{a}{k\pi}\sin\Big(\frac{k\pi}{4}\Big)\Big)^2\cdot\frac{1}{2L}\\ & =\sum_{k=1}^{\infty}\frac{4a^2}{L}\Big(1+\cos \Big(\frac{k\pi}{2}\Big)\Big)~\mathrm{e}^{-\lambda_2 k}\\& +\sum_{k=1}^{\infty}\Big(\frac{128a^2}{9L}\frac{1}{(k\pi)^2}\sin^2\Big(\frac{k\pi}{4}\Big)-\frac{64a^2}{3L}\frac{1}{k\pi}\sin\Big(\frac{k\pi}{4}\Big)\cos\Big(\frac{k\pi}{4}\Big)\Big)& \text{\lambda_2\to0^+}\\ & \to\frac{4a^2}{L}\cdot\frac{1}{\lambda_2}-\frac{4}{3}\cdot\frac{a^2}{L}\\ \end{align}

It is not surprising that the energies are infinite due to the Gibbs phenomenon. However, it is surprising when we take a look at the energy difference: $$F(L_1=L/4)-F(L_1=L/2)=\Big(\frac{4a^2}{L}\cdot\frac{1}{\lambda_2}-\frac{4a^2}{L}\cdot\frac{1}{\lambda_1}\Big)-\frac{4}{3}\cdot\frac{a^2}{L}$$ While when we use $$F(L_1)=\frac{a^2}{L_1}+\frac{a^2}{L-L_1}$$, we get $$F(L_1=L/4)-F(L_1=L/2)=\frac{4}{3}\cdot\frac{a^2}{L}$$. My observation is that when we "ignore" the divergent term, the Fourier series give us the correct absolute value for the energy difference but wrong sign (I have checked for all values of $$L_1$$). I don't feel like it is a coincidence.

To get the correct value of energy difference, I'm guessing that maybe when we use Fourier series, we introduce a vanishing length scale for the jump discontinuity, and this length scale should be the same whatever the value of $$L_1$$ is, and somehow $$\lambda_1$$, $$\lambda_2$$ are related to it so that $$\frac{4a^2}{L}\cdot\frac{1}{\lambda_2}-\frac{4a^2}{L}\cdot\frac{1}{\lambda_1}$$ may be some finite number.

My question is whether we can use Fourier series to get the correct value of energy difference.

$$F(L_1)=\sum_{k=1}^{\infty}\frac{(b_kk\pi)^2}{2L}=F_1(L_1)+F_2(L_1)+F_3(L_1)$$

where

$$F_1(L_1)=\frac{1}{2L}\cdot\sum_{k=1}^{\infty}\Big(4a\cos\frac{k\pi L_1}{L}\Big)^2$$

$$F_2(L_1)=\frac{1}{2L}\cdot\sum_{k=1}^{\infty}\Big[\frac{2aL}{k\pi}\Big(\frac{1}{L-L_1}-\frac{1}{L_1}\Big)\sin\frac{k\pi L_1}{L}\Big]^2=\frac{a^2}{L_1}+\frac{a^2}{L-L_1}-\frac{4a^2}{L}$$

$$F_3(L_1)=\frac{1}{L}\cdot\sum_{k=1}^{\infty}\Big(4a\cos\frac{k\pi L_1}{L}\Big)\cdot\Big[\frac{2aL}{k\pi}\Big(\frac{1}{L-L_1}-\frac{1}{L_1}\Big)\sin\frac{k\pi L_1}{L}\Big]$$

To determine the ground state, we may throw away $$F_1(L_1)$$ and $$F_3(L_1)$$, because:

(1) $$F_1(L_1)$$ contains the fictitious core energy, and we can see this by finding the energy density.

$$F(y, L_1)=\int_{0}^{y}(\frac{\mathrm{d}f}{\mathrm{d}x})^2\mathrm{d}x=F_1(y, L_1)+F_2(y, L_1)+F_3(y, L_1)$$,

where $$F(L,L_1)=F(L_1)$$, $$F_1(L,L_1)=F_1(L_1)$$, $$F_2(L,L_1)=F_2(L_1)$$, and $$F_3(L,L_1)=F_3(L_1)$$.

After some calculations, we have

$$F_{1}(y, L_1) =\frac{2a^2y}{L^2}\Big(\frac{\cos\frac{2N\pi L_1}{L}-\cos\frac{2(N+1)\pi L_1}{L}}{1-\cos\frac{2\pi L_1}{L}}-1\Big)-\frac{2a^2}{\pi L}\cdot\frac{\sin\frac{\pi y}{L}}{\cos\frac{\pi L_1}{L}-\cos\frac{\pi y}{L}}+\frac{a^2}{\pi L}\cdot\frac{\cos\frac{(2N+1)\pi L_1}{L}}{\sin\frac{\pi L_1}{L}}\ln\Bigg (\frac{1-\cos\frac{\pi (y-L_1)}{L}}{1-\cos\frac{\pi (y+L_1)}{L}}\Bigg)+\frac{4a^2 y}{L^2}\\ \\ +\begin{cases} 0+\frac{2a^2y}{L^2}\Big(1-\frac{\sin\frac{(2N+1)\pi L_1}{L}}{\sin\frac{\pi L_1}{L}}\Big), & \text{if } 0

where $$N\to\infty$$. And I assume $$N\cdot\frac{L_1}{L}\in \mathbb{Z}$$, because that seems to make sure the core size for different $$L_1$$ and finite $$N$$ are the same (I don't know a better way to justify it). Now we can see $$F_1(L,L_1)=F_1(L_1)$$ is infinite but independent of $$L_1$$. Its energy density profile is

$$\mathcal{F}_1(y, L_1)=\frac{4a^2}{L^2}+\frac{a^2}{2L^2}\Big(\csc ^2\frac{\pi (y-L_1)}{2L}+\cos\frac{\pi (y-3L_1)}{2L}-\cos\frac{\pi (y+L_1)}{2L}+\cot\frac{\pi L_1}{L}\csc ^2\frac{\pi (y-L_1)}{2L}\csc\frac{\pi (y+L_1)}{2L}+\csc ^2\frac{\pi (y+L_1)}{2L}\Big)$$

We can see that the energy jumps to infinity at $$x=L_1$$, so $$F_1(L_1)$$ contains the core energy and the constant energy $$\frac{4a^2}{L}$$. Also, notice that the size of the core becomes very big, and I think it is due to the Gibbs phenomenon.

(2) I think $$F_3(L_1)$$ can be regarded as the interaction between the fattened core and the function $$f$$ on $$[0,L_1)\cup (L_1,L]$$, so it should be thrown away.