Solution (source code)

= Solution

At a regular constrained extremum, the method of <Lagrange multipliers> makes $I+\lambda J$ stationary for a constant $\lambda$. For an endpoint-vanishing variation $\eta$, integration by parts gives
$$
\delta(I+\lambda J)=\int_{x_1}^{x_2}\left[f_y+\lambda g_y-\frac d{dx}(f_{y'}+\lambda g_{y'})\right]\eta\,dx.
$$
The <fundamental lemma of the calculus of variations> therefore yields
$$
\boxed{\frac d{dx}(f_{y'}+\lambda g_{y'})-(f_y+\lambda g_y)=0.}
$$
This <Euler-Lagrange equation> must be supplemented by the two endpoint conditions and the prescribed integral constraint, which determine the constants and multiplier. Existence of the normal multiplier assumes that the first variation of $J$ is not identically zero. If that regularity fails, the general multiplier equation uses $\alpha I+\lambda J$, with $(\alpha,\lambda)\ne(0,0)$; one cannot automatically set $\alpha=1$.