Recall that in a classical harmonic oscillator, we defined $\omega=\sqrt{\frac{k}{m}}$ and the total mechanical energy was $E=\frac{p^2}{2m}+\frac{1}{2}kx^2$. Similarly, the quantum mechanical Hamiltonian for the harmonic oscillator is:
$$H=\frac{\hat{p}^2}{2m}+\frac{1}{2}m\omega^2\hat{x}^2$$
As usual, when provided with a potential energy system, we want to solve the energy eigenvalue equation $H\ket{E}=E\ket{E}$ for the allowed energies in the system. We can do this with a power series solution or the operator method.
We define the lowering operator: $a=\sqrt{\frac{m\omega}{2\hbar}}(x+\frac{ip}{m\omega})$ and the raising operator: $\ud{a}=\sqrt{\frac{m\omega}{2\hbar}}(x-\frac{ip}{m\omega})$.
$$\ud{a}a+\frac{1}{2}=\frac{m\omega}{2\hbar}\bigg(x^2+\frac{p^2}{m^2\omega^2}\bigg)$$
$$\begin{align*}
H&=\hbar\omega\bigg[\frac{m\omega}{2\hbar}\bigg(x^2+\frac{p^2}{m^2\omega^2}\bigg)\bigg]\\
&=\hbar\omega(\ud{a}a+\frac{1}{2})
\end{align*}$$
Applying the raising and lowering operators on an energy eigenstate $\ket{E}$,
$$H(a\ket{E})=(E-\hbar\omega)(a\ket{E})\\
H(\ud{a}\ket{E})=(E+\hbar\omega)(\ud{a}\ket{E})$$
Since we cannot have negative energies, we define the base case of $\ket{E_0}$. So, $a\ket{E_0}=0$ and
$$E_n=\hbar\omega(n+\frac{1}{2})$$
$$\begin{align*}
H\ket{n}&=E_n\ket{n}\\
\hbar\omega(\ud{a}a+\frac{1}{2})\ket{n}&=\hbar\omega(n+\frac{1}{2})\ket{n}\\
\ud{a}a\ket{n}&=n\ket{n}
\end{align*}$$
We define the number operator $N=\ud{a}a$.
$$H=\hbar\omega(N+\frac{1}{2})$$
Eigenvalue Equation
$$N\ket{n}=n\ket{n}$$
Wavefunctions
Ground state wavefunction:
$$\phi_0(x)=\bigg(\frac{m\omega}{\pi\hbar}\bigg)^\frac{1}{4}e^{-\frac{m\omega x^2}{2\hbar}}$$
To find the other energy eigenstates, we apply the raising operator to the ground state.
Using the integral form of the wavefunction is too complex. Instead, we use the operator method.
$$\begin{align*}
\braketo{3}{x^2}{3}&=\braketo{3}{\frac{\hbar}{2m\omega}(a+\ud{a})^2}{3}\\
&=\frac{\hbar}{2m\omega}\braketo{3}{(a^2+a\ud{a}+\ud{a}a+\ud{a}^2)}{3}\\
\end{align*}$$
$$*a^2\ket{3}\propto\ket{1}\implies\braket{3}{1}=0\\
*\ud{a}^2\ket{3}\propto\ket{5}\implies\braket{3}{5}=0\\
*\ud{a}a\ket{3}=\ud{a}\sqrt{3}\ket{2}=3\ket{3}\\
*a\ud{a}\ket{3}=a(2\ket{4})=4\ket{3}$$
$$\begin{align*}
\braketo{3}{x^2}{3}&=\frac{\hbar}{2m\omega}(4\braket{3}{3}+3\braket{3}{3})\\
&=\frac{7\hbar}{2m\omega}
\end{align*}$$