Infinite Potential Well Energy for Piece-wise Constant Wave Function I'm trying to compute the expectation value of energy for a certain state in an infinite potential well but I'm getting contradictory answers.
The well has potential
\begin{align}
V(x) = \left\{
      \begin{array}{lr}
          0 & : 0 < x < L\\
          \infty & : \text{ elsewhere}
        \end{array}
     \right.
\end{align}
which has eigenstates $\phi_n(x) = \sqrt{\frac{2}{L}} \sin(\frac{n \pi x}{L})$ with corresponding energies $E_n = \frac{n^2 \pi^2 \hbar^2}{2mL^2}$.
Now, consider the state
\begin{align}
\psi(x) = \left\{
      \begin{array}{lr}
          0 & : 0 < x < L/2\\
          \sqrt{\frac{2}{L}} & : L/2 \leq x < L\\
          0 &: \text{ elsewhere}
        \end{array}
     \right.
\end{align}
We want to compute $\langle H \rangle$ for this state. One way to do this is simply using the definition: $\langle H \rangle = \int_{0}^L \psi^*(x) H \psi(x) dx$. The problem with this though is that $\psi(x)$ is piecewise constant, and therefore this will give you $0$. The other option is to expand $\psi$ in terms of the energy eigenbasis by computing the coefficients $c_n = \langle \phi_n | \psi \rangle$, and getting $\langle H \rangle = \sum_{n=1}^\infty | c_n |^2 E_n$. As $E_n > 0$ for every $n$, this quantity will be strictly greater than $0$ and therefore will differ from the previous answer.
What is the discrepancy? It surely has to do with the discontinuity at $x = L/2$, but I can't figure out how to deal with it.
 A: The expectation value 
$$\tag{1} \langle \psi | V| \psi \rangle~=~0$$
of the potential energy operator $V$ is indeed zero, but the expectation value 
$$\tag{2} \langle \psi |K| \psi \rangle~=\frac{\hbar^2}{2m} \int_{\mathbb{R}}\!dx~ |\psi^{\prime}(x)|^2 ~=~+\infty$$ 
of the kinetic energy operator $K$ is actually infinite for the wave function
$$\tag{3} \psi(x)~=~ \sqrt{\frac{2}{L}}\left(\theta(x-\frac{L}{2}) -\theta(x-L)\right), \qquad  x\in \mathbb{R}.$$
Here $\theta$ is the Heaviside step function. 
The kinetic energy operator $K:=-\frac{\hbar^2}{2m}\frac{d^2}{dx^2}$ is an example of an unbounded operator, which only make sense on its domain ${\cal D}_K\subsetneq {\cal H}$ inside the Hilbert space ${\cal H}:=L^{2}(\mathbb{R})$ of square Lebesgue integrable functions. In particular, it is a non-trivial mathematical problem how to apply the differential operator $K$ to the non-differentiable wave function (3).
The infinite result (2) can be seen (at the physical level of rigor) in at least three ways (ordered with the computationally simplest calculation first):


*

*Plug the Heaviside step function into eq. (2) to get an integral over the square of a pair of Dirac delta function situated at $x=\frac{L}{2}$ and $x=L$. This is strictly speaking mathematically ill-defined. Physically, it makes sense to assign the integral the value infinite, cf. this Phys.SE post.

*Calculate the overlaps $c_n=\langle \phi_n | \psi \rangle$, and show than the sum
$$\tag{4} \langle \psi |H| \psi \rangle=\sum_{n=1}^{\infty}|c_n|^2 E_n ~=~+\infty $$ 
diverges. This infinite conclusion seems physically robust, since all terms in the series (4) are non-negative.

*By regularization, as Emilio Pisanty suggests in a comment. Define a regularized  wavefunction $\psi_{\varepsilon} \in C^1(\mathbb{R})$ in such a way that (i) it  converges $\psi_{\varepsilon}\to \psi$ for $\varepsilon\to 0^{+}$, (ii) the expectation value $\langle \psi_{\varepsilon} |K| \psi_{\varepsilon} \rangle$ is easy to compute and finite for $\varepsilon>0$. Show that $\langle \psi_{\varepsilon} |K|\psi_{\varepsilon}  \rangle\to +\infty$ diverges for $\varepsilon\to 0^{+}$.
