An approach to constraint nonlinear optimisation is proposed, where the variables of the objective function and of the constraint function belong to the state of a nonlinear dynamical system. A nonlinear closed-loop feedback system is designed, whose solutions converge to a target set consisting of the optimal points of the underlying optimisation problem. The feedback law for the control input of the nonlinear dynamical system is designed by exact linearisation of a dynamical formulation of a first-order optimality condition. For the problem formulation and the stability analysis of the nonlinear closed-loop feedback system, the notion of V-detectability is considered. An application to energy-optimal feed-forward torque control for different types of synchronous machines is presented, which demonstrates the usefulness of the method. In this regard, anisotropic permanent magnet synchronous machines and electrically excited synchronous machines are covered.