Derivation (1D wave equation)

Try separation of variables $$ \begin{align*} y(x,t) = X(x) \, T(t) \end{align*} $$ and the wave equation becomes $$ \begin{align*} \frac{\ddot T}{c^2 T} = \frac{X''}{X}. \end{align*} $$ Since $x$ and $t$ are independent, both sides have to be constant. Therefore $$ \begin{align*} \ddot T + \lambda c^2 T = 0, \qquad X'' + \lambda X = 0. \end{align*} $$ The equation for $X$ is a Sturm-Liouville problem. Using the boundary conditions $X(0) = X(L) = 0$, $$ \begin{align*} X_n(x) = \sin \frac{n\pi x}{L}, \qquad \lambda_n = \left(\frac{n\pi}{L}\right)^2, \qquad n=1,2,3,\ldots. \end{align*} $$

The equation for $T$ becomes $$ \begin{align*} \ddot T + \left(\frac{n\pi c}{L}\right)^2 T = 0 \end{align*} $$ which has solutions $$ \begin{align*} T_n(t) = A_n \cos \frac{n\pi c t}{L} + B_n \sin \frac{n\pi c t}{L}. \end{align*} $$

By superposition, $$ \begin{align*} y(x,t) = \sum_{n=1}^{\infty} \left(\sin \frac{n\pi x}{L}\right) \left(A_n \cos \frac{n\pi ct}{L} + B_n \sin \frac{n\pi c t}{L}\right) \end{align*} $$ solves the wave equation and the boundary conditions.

The initial conditions become $$ \begin{align*} f(x) = y(x,0) ={}& \sum_{n=1}^{\infty} A_n \sin \frac{n\pi x}{L}, \\ g(x) = y_x(x,0) ={}& \sum_{n=1}^{\infty} \frac{n\pi c}{L} B_n \sin \frac{n\pi x}{L}. \end{align*} $$ This is the Fourier sine series! Therefore we obtain $$ \begin{align*} A_n ={}& \frac{2}{L} \int_{0}^{L} f(x) \sin \frac{n\pi x}{L} \d x \\ B_n ={}& \frac{2}{n\pi c} \int_{0}^{L} g(x) \sin \frac{n\pi x}{L} \d x. \end{align*} $$