Solution (source code)

= Solution

Choose homogeneous <Dirichlet boundary conditions> $\psi(a)=\psi(b)=0$, and take a nonzero real admissible trial $\psi$. Write the numerator as $F[\psi]$ and denominator as $G[\psi]>0$. For an endpoint-zero <variation> $\eta$, <integration by parts> gives
$$
\delta F=2\int_a^b[p\psi'\eta'+q\psi\eta]dx=2\int_a^b[-(p\psi')'+q\psi]\eta\,dx,\qquad\delta G=2\int_a^bw\psi\eta\,dx.
$$
Thus the <first variation> of the <Rayleigh quotient> is
$$
\delta\Lambda=\frac2G\int_a^b[-(p\psi')'+q\psi-\Lambda[\psi]w\psi]\eta\,dx.
$$
By the <fundamental lemma of the calculus of variations>, stationarity is equivalent to
$$
\boxed{-(p\psi')'+q\psi=\lambda w\psi,\qquad\lambda=\Lambda[\psi],\qquad\psi(a)=\psi(b)=0.}
$$
Conversely, multiplying an <eigenfunction> equation by $\psi$ and integrating gives $F=\lambda G$, so its <first variation> vanishes. This establishes the equivalence for the chosen <Sturm-Liouville problem>. Homogeneous <Neumann boundary conditions>, or a mixed Dirichlet-Neumann pair, also cancel the boundary term; general <Robin boundary conditions> require adding the corresponding boundary contribution to the numerator.