Calculation of the $\langle H \rangle$ for a particle in a box I am working through a problem in which a particle is in an infinite potential well of length $L$ at $t=0$ before the spontaneous change of the box being expanded to length $2L$. I have calculated the wave function $$\Psi(x,t)=\Sigma_{n=0}^{\infty}c_{n}\sqrt{\dfrac{2}{L}}\sin(\frac{n \pi x}{L})\exp(-i(n^{2}\pi^{2}\hbar^{2}/2m(2L)^{2})/t)$$ including all coefficients $c_{n}$ where $c_{n}=0$ if $n$ is even and $c_{n}=\dfrac{\pm 4\sqrt{2}}{\pi(4-n^{2})}$ if $n$ is odd. 
To calculate $\langle H \rangle$ I'd like to use $\langle H \rangle=\Sigma_{n=1}^{\infty}|c_{n}|^{2}E_{n}$ where the allowed values of E after the change in the well length are $E_{n}=\dfrac{n^{2}\pi^{2}\hbar^{2}}{2m(2L)^{2}}$.
My result is $$\langle H \rangle=\dfrac{16\hbar^{2}}{mL^{2}}\Sigma_{n=0}^{\infty}\dfrac{(2n+1)^{2}}{(4-(2n+1)^{2})^{2}}=\dfrac{\pi^{2}\hbar^{2}}{4mL^{2}},$$ which is different than the Hamiltonian before the change in length ($\dfrac{\pi^{2}\hbar^{2}}{2mL^{2}}$). 
I suspected that the Hamiltonian should not change, since, after all, $2L$ is just a label, and I could call that distance some other number without a factor of 2, and it shouldn't change the physics involved. 
What is the Hamiltonian after the change? If it is different from the Hamiltonian before the change, then why is it different?
 A: Would it be simpler to consider only the second Hamiltonian with the appropriate initial condition?  As an intial condition, I am imagining some non-zero amplitude from $0$ to $L$ and zero amplitude from $L$ to $2L$.  That would be consistent with the particle having been confined to the narrower well prior to time $t=0$.  Is the wavefunction, as you have written it, consistent with the condition that the wavefunction is initially non-zero only over an interval that is $L$ wide?  I am not be sure what your "$\pm$" means in your expression for $c_n$, but it looks to me like the wavefunction is spread over the entire $2L$ interval at $t=0$.  Also, shouldn't the exponential factors be $\exp(-i(n^{2}\pi^{2}\hbar/8mL^{2})t)$, consistent with a well of width $2L$?  
Approach
Just to be clear, the hamiltonian could be written as a single expression involving step functions of time.  It is usually said that such a time dependent system does not have eigenfunctions and eigenvalues.  Various approaches are available for dealing with time dependent hamiltonians.  To predict what happens after the potential well changes suddenly from width L to width 2L, one approach is to consider a first time independent hamiltonian that acts up to time $t=0$ and a second time independent hamiltonian that acts after that time.  The first hamiltonian is considered only to the extent that it is required to determine the initial conditions for predicting the dynamics after time $t=0$.
The first hamiltonian, $H_1$, and its eigenfunctions
The first hamiltonian is $H_1=\hat{p}^2/2m+V_1(x)$ where $V_1(x)=0$ on the interval $[0,L]$ and infinite everywhere else.  The infinite potential energy just means the probability of finding the particle in that region is zero.  The normalized eigenfunctions are $u_j(x)=\sqrt{\frac{2}{L}}\sin(j \pi x/L)$ on the interval $[0,L]$ and $u_j(x)=0$ everywhere else.  This ensures the probability of finding the particle outside of the interval $[0,L]$ is zero.  It also means the wavefunction is zero outside the interval $[0,L]$.  There is a discontinuity in the slope of the eigenfunctions at 0 and at L.  This is usually accepted by making an analogy to a classically rigid wall.
Expectation value of $H_1$
Calculating $\langle H_1\rangle$ is tricky, because of the infinite potential function.  I do not know a mathematical argument for saying $\int u^*_j(x)V_1(x)u_j(x) dx =0$, but physically the integral is zero because there is no wavefunction outside the interval $[0,L]$ and $V_1(x)$ is zero where there is a wavefunction.  Except for that issue, it is straight forward to show $\langle H_1 \rangle=\Sigma_{j=1}^{\infty}|a_{j}|^{2}\epsilon_{j}$ where $\epsilon_j=j^2\pi ^2 \hbar ^2/2mL^2$ and $\psi (x)=\Sigma_{j=1}^{\infty} a_{j}u_j(x)$.
The second hamiltonian, $H_2$, and its eigenfunctions
The second hamiltonian is $H_2=\hat{p}^2/2m+V_2(x)$ where $V_2(x)=0$ on the interval $[0,2L]$ and infinite elsewhere.  The normalized eigenfunctions are $w_n(x)=\sqrt{\frac{1}{L}}\sin(n \pi x/2L)$ on the interval $[0,2L]$ and $w_n(x)=0$ everywhere else.  The eigenvalues are $E_n=n^2\pi ^2 \hbar ^2/8mL^2$
Note that the eigenfunctions $w_n(x)$ of $H_2$ cannot be expanded in terms of the $u_j(x)$ because the $u_j(x)$'s are zero on the interval $[L,2L]$.  Except for the usual details, it is possible to expand the $u_j(x)$'s in terms of the $w_n(x)$'s.  
One detail is continuity of $\hat{p}\psi(x)$ at $x=L$ and at $x=2L$.  With the first hamiltonian the momentum was discontinuous at $x=L$, consistent with the classical turning point.  With the second hamiltonian, there should be no turning point at $x=L$.  That is, the momentum should be continuous there.  
The wavefunction in the second potential well
If the wavefunction is $\psi (x,0)=\Sigma_{j=1}^{\infty} a_{j}u_j(x)$ at $t=0$, the wavefunction in the second potential well will evolve according to $\psi (x,t)=\Sigma_{n=1}^{\infty} b_{n}w_n(x)\exp(-iE_nt/\hbar)$ for $t>0$, where the $b_n$'s are to be determined.  The derivation of an expression for the $b_n$'s in terms of the $a_n$'s goes like this:
$$
\psi (x,0)=\Sigma_{n=1}^{\infty} b_{n}w_n(x)=\Sigma_{j=1}^{\infty} a_{j}u_j(x)$$
$$
\Sigma_{n=1}^{\infty} b_{n}w_k^*(x)w_n(x)=\Sigma_{j=1}^{\infty} a_{j}w_k^*(x)u_j(x)
$$
$$
b_{k}=\Sigma_{j=1}^{\infty} a_{j}\int_{-\infty}^{\infty}w_k^*(x)u_j(x)dx
$$
$$
b_{k}=\Sigma_{j=1}^{\infty} a_{j}\int_{0}^{L}w_k^*(x)u_j(x)dx
$$
Note that the integration only goes from zero to L because the $u_j(x)$'s are zero everywhere else.  
A simple example
For example, if a particle in an infinite square well of width of 2L with hamiltonian $H_2$ somehow starts out with a wavefunction $\psi(t,0)=u_1(x)$, then
$$
b_n=\frac{4 \sqrt{2}\sin(n\pi /2)}{(4-n^2)\pi}, n=1,2,3,5,7,...
$$
$$b_k=0, k=4,6,8,10,...$$
The particle is not in an energy eigenstate.  Note that in this example I am not saying the particle started in a narrow well or that there are two hamiltonians, but I am saying these results are the same as in the original problem with the time dependent hamiltonian.
What is the expectation value $\langle H_2 \rangle$?  It is straight forward to show
$$\langle H_2 \rangle=\frac{\hbar^2 \pi^2}{8mL^2}\Sigma_{n=1}^{\infty}n^2 \left| b_n\right|^2=\frac{\hbar^2 \pi^2}{2mL^2}=\epsilon_1
$$
I used Mathematica to do the integrals and derive that the $n^2 \left| b_n \right| ^2$'s all add up 4.  This shows that the expectation value of the energy is equal to the eigenvalue of $H_1$ acting on $u_1(x)$, as expected.  This is only an expectation value, though.  It is not "the energy", since a particle with that initial condition does not have a definite energy in this square well.
It looks like the difference between this result and yours is at $b_2=1/\sqrt{2}$.  Leaving that $2^2b_2^2$ out of the sum reduces the sum by a factor of 2, so your result was too small by a factor of 2.
A: You're right, the Hamiltonian doesn't change. The Hamiltonian is that of a free particle, and doesn't depend on $L$. It is the boundary condition, which in turn determines the energies, that changes when the wall expands. A wall that expands to 2x its original width will give energy eigenvalues reduced by a factor of $2^2=4$.
