# Operator notation and the infinite square well

Let's say that I'd like to calculate $\langle x^2 \rangle$ for the state $n=1$ of the infinite square well $\sqrt{\frac{L}{2}} \sin(\frac{n \pi x}{L})$.

My first approach is to rewrite the kets as

$$\langle x^2 \rangle = (\langle \psi | x)(x | \psi \rangle) = \left( \int x \sqrt{\frac{2}{L}} \sin(\frac{\pi x}{L}) dx \right) ^2 = \left( \sqrt{\frac{2}{L}} \frac{L^2}{ \pi } \right)^2$$

The second is straightforward calculations:

$$\langle x^2 \rangle = \frac{2}{L} \int x^2 \sin^2 \left(\frac{\pi x}{L} \right) dx = L^2 \left(\frac{1}{3} - \frac{1}{2\pi^2}\right)$$

Why won't the two approaches yield the same result?

Your first line has a mistake. The correct version is: \begin{align}\langle x^2 \rangle &= \langle\psi| \hat{x}^2|\psi\rangle \\ & = \langle\psi| \hat{x}^2 \left[\int |x\rangle \langle x| \operatorname{d} x\right]|\psi\rangle \\ & = \int x^2 \langle \psi|x\rangle \langle x|\psi\rangle \operatorname{d}x \\ & = \int x^2 \psi^*(x) \psi(x)\operatorname{d}x .\end{align}