Many equations of motion can be derived from a variational principle. To take a simple example, the wave equation $h^{ij} \partial_i \partial_j u = 0$ (where $h^{ij}$ is the Minkowski metric $\mathrm{diag}[-1,+1,+1,+1]$) can be derived from the Lagrangian density
$L = \frac{1}{2} h^{ij} (\partial_i u) (\partial_j u)$ :
It is $\frac{\delta L}{\delta \partial_i u} = h^{ij} \partial_j u$, from which follows the equation of motion via the usual ansatz $\frac{\delta L}{\delta u} - \partial_i \frac{\delta L}{\delta \partial_i u} = 0$.
This calculation neglects boundary conditions, which it should not since the ansatz implies an integration by parts which has a boundary term. Assuming e.g. I want to solve the wave equation in a box $0 \le x \le 1$, how would I e.g. impose Dirichlet or von Neumann boundary conditions?
I can think of two approaches:
(1) Assume that $u$ lives in a function space that already satisfies the boundary condition. However, this seems complicated in general -- is this workable?
(2) Impose a constraint (via a Lagrange multiplier) that corresponds to the boundary condition. This seems weird because the multiplier would live only on the boundary of the domain.
Do these approaches work? Is there another way?