Hamiltonian for single-mode field in cavity Given that the Hamiltonian for a single-mode field is 
$$ H = \frac{1}{2}\int dV \left[ \epsilon_0E_x^2(z,t) + \mu_0^{-1}B_y^2(z,t) \right],$$
with
\begin{align}
E_x(z,t) =& \sqrt{\frac{2\omega^2}{V\epsilon_0}}q(t)\sin(kz) \\
\text{and} \quad
B_y(z,t) =& \left( \frac{\mu_0\epsilon_0}{k}\right) \sqrt{\frac{2\omega^2}{V\epsilon_0}}p(t)\cos(kz)
\end{align}
it should be possible to show that $H=\frac{1}{2}(p^2+\omega^2 q^2)$.
When I try, however, I can only get some of the way. More specifically
$$H = \frac{1}{2} \int dV \left[ \frac{2}{V}q^2(t)\sin^2(kz) + \frac{2\omega^2}{V}p^2(t)\cos^2(kz) \right]$$
$$=\frac{1}{2} \int dV \left[\omega^2 q^2\frac{1}{V}\left(1-\cos(2kz)\right) + p^2\frac{1}{V}\left(1+\cos(2kz)\right) \right].$$
At this point I'm not sure how to move on. It looks like the integral should be solved with using $\int \frac{1}{V} dV = \log(V)$, but that doesn't seem to make sense. So what makes the cosine-terms disappear and how is the $\int \frac{1}{V} dV$ solved?
 A: Hints:


*

*The notation is confusing. In the first line $\int dV$ is an integral over the space inside a volume $V$. It would be much nicer to write $\int_V d^3r$. Or, since the example seems to be one-dimensional, $\int_V dz$.

*The $V$ in the mode functions is is the actual volume you are integrating over. Hence it comes out of the integral. In our new notation this is easy to see, e.g. $$\int_V dz \frac{1}{V} \sin^2(kz) = \frac{1}{V} \int_V dz \sin^2(kz).$$

*So all we have to do is compute integrals such as $\int_V dz \sin^2(kz)$ and $\int_V dz \cos^2(kz)$. This can be done if the boundary conditions of the mode are correctly specified. E.g. for a mode between 2 perfect mirrors that are separated by a distance $d$ the boundary conditions would impose $E(0)=E(d)=0$ and hence $k=\frac{n\pi}{d}$ where $n \in \mathbb{Z}$. Then, with $V=d$


$$\int_V dz \sin^2(kz) = \int_0^d dz \sin^2\left( \frac{n\pi}{d} z\right) = \frac{d}{2}.$$
Hope that helps.
A: I came across this in Gerry & Knight's Introductory Quantum Optics as an exercise left to the reader.
Anyways, I started with provided solutions to Maxwell's equations without a source/current $$ \begin{cases}E_x(t,z) = \left(\dfrac{2\omega^2}{V\varepsilon_0} \right)^{1/2}q(t)\sin(kz) \\
B_y(t,z) = \left(\dfrac{2\omega^2}{V\varepsilon_0} \right)^{1/2}\dfrac{\mu_0\varepsilon_0}{k}\dot{q}(t)\cos(kz)
\end{cases}
$$
then just checked they actually satisfied the equations of motion. In doing so, I noted this requires the dynamical function (aka state of the field) $q(t)$ satisfy $\partial_t \dot{q}(t) \propto q(t)$, which makes sense here since the $B$-field must satisfy the wave equation $(\partial_t^2-\partial_z^2)B=0 $ in units where the velocity $c$ of the thing that's waving is $1$. In these units, its general solution is of the form $f(x+t)+g(x-t)$, which is equivalent to a product of one cosine with a time argument and another with a spatial argument. If that is unclear, you can consult Feynman's Lecture Series Volume II $\S$ 20.1 (link) for a review.
Next up is the Hamiltonian for a single-mode field consisting of the previous $E$- and $B$-fields, $$H = \dfrac{1}{2}\int dV \left[\varepsilon_0 E^2+\dfrac{1}{\mu_0}B^2 \right], $$ where the integration is over a one-dimensional cavity along the $z$-axis with $z\in[0,L]$ and with each field vanishing at the boundaries. Choosing $\omega = kc$ (i.e. a single mode), recalling $c^2 = (\varepsilon_0\mu_0)^{-1}$, and thinking of $\dot{q}(t)= p$ as canonical momentum, we have $$H = \dfrac{1}{L}\left[\omega^2 q^2\int_0^L\sin^2(kz)\hspace{0.1cm}dz+p^2\int_0^L\cos^2(kz)\hspace{0.1cm} dz \right]. $$ That's reduced the problem to two integrals. Following the boundary data, one can choose $k = m\pi/L$ with $m\in \mathbb{N}$ so that $$\int_0^L \sin^2\dfrac{m\pi z}{L} \hspace{0.1cm} dz = \frac{1}{2}\int_0^L\left[ 1-\cos\frac{2m\pi z}{L}\right]dz  = \dfrac{L}{2}$$ as only the first term's integration survives. Similarly, $\dfrac{L}{2}$ is all that's left of the other integral, leaving \begin{align} H &= \frac{1}{L}\left(\omega^2 q^2 \dfrac{L}{2} + p^2 \dfrac{L}{2}\right)\\  \Rightarrow H &= \dfrac{1}{2}(p^2 + \omega^2 q^2), \end{align} showing the Hamiltonian for a single-mode field is equivalent to that of a simple harmonic oscillator with unit mass. Though this computation was entirely classical, it clearly puts one in a familiar spot to start quantizing the electromagnetic field.
A: I came across this problem in Gerry & Knight's Introductory Quantum Optics too.
I think the reason why the term V can come out of the inregral is, V is the function of $\omega$ since the effective volume V is came from
$$\varepsilon_0 E_0^2 V = \hbar\omega$$
which clearly shows $V$ is $V(\omega)$.
(c.f. Amplitude of a single mode quantized light field in a cavity)
But as referred in the book, the $\omega$ here is given by
$$\omega_m = c(m\pi/L),     m=1, 2, \cdots$$
which implies that the given $V$ is constant in each $\omega$.
I am just an undergraduate student. So if there is any feedback, please feel free to correct my answer.
