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.