From functional optimization to nonlinear programming by the extended Ritz method