I am trying to find the differential equation which implies a smooth path $q:[a,b] \rightarrow \mathbb{R}^n$ subject to $|\dot{q}(t)| = 1$ (i.e. $q$ has unit speed) is a stationary point of $$ \int_a^b L(t,q(t), \dot{q}(t)) \ dt $$ for a certain $L(t,q(t), \dot{q}(t))$. This question and answer here appears to be exactly what I need, but when I tried to apply their formula to an easy example, it didn't work. Did I make a mistake or is their formula wrong? The example I tried to apply it to is below.
Following the notation of their question, suppose $F(x,y) = x^2+y^2$ and $g(x',y') = {x'}^2 + {y'}^2 -1 = 0$ and $x(0) = y(0) = 0$. So you are trying to maximize
$$
\int_0^1 x(t)^2 + y(t)^2 dt \tag 1
$$
subject to $(x(t),y(t))$ having unit speed. Because $x(t)^2 + y(t)^2 \leq t^2$, $(1)$ is less than $\int_0^1 t^2 dt = 1/3$. On the other hand, this maximal value is obtainable by having $(x(t),y(t))$ move in a straight line at unit speed. However their equation
$$
\frac{\partial F}{\partial x}-\frac{d}{dt}\frac{\partial F}{\partial x'}=\lambda(t)\left(\frac{\partial g}{\partial x}-\frac{d}{dt}\frac{\partial g}{\partial x'}\right) \tag 2
$$
yields
$$2x = -\lambda(t) 2x''.
$$
Their other equation for $y$ yields
$$
2y = -\lambda(t) 2y''.
$$
When $(x(t),y(t))$ moves in a straight line at unit speed, $x'' = y'' = 0$. Their equations imply $(x,y) = 0$. So $(x,y)$ maximizing $(1)$ by moving in a line is not detected by their equations.
I tried to rederive their formula by considering $$ \Phi(q) = \int_a^b L(t,q(t),\dot{q}(t)) + \lambda(t) g(q(t),\dot{q}(t)) \ dt $$ but got a different formula. For $\eta:[a,b] \rightarrow \mathbb{R}^n$ smooth with $\eta(a) = \eta(b) = 0$, one calculates $$ \begin{align} \frac{d}{d\epsilon} \Phi(q+\epsilon \eta) = \int_a^b &\eta(t) L_q(t,q(t)+\epsilon \eta(t), \dot{q}(t) + \epsilon \dot{\eta}(t)) \\ &+\dot{\eta}(t) L_{\dot{q}}(t,q(t)+\epsilon \eta(t), \dot{q}(t) + \epsilon \dot{\eta}(t)) \\ &+\lambda(t) \eta(t) g_q(q(t) + \epsilon \eta(t), \dot{q}(t) + \epsilon \dot{\eta}(t)) \\ &+\lambda(t) \dot{\eta}(t) g_{\dot{q}}(q(t)+\epsilon \eta(t),\dot{q}(t)+\epsilon \dot{\eta}(t) ) dt \end{align} $$ I don't fully understand why, but presumably if $q$ is a stationary point satisfying $g(q,\dot{q}) = 0$, setting $\epsilon = 0$ makes the previous equation $0$. $$ \begin{align} 0 = \int_a^b &\eta(t) L_q(t,q(t), \dot{q}(t)) +\dot{\eta}(t) L_{\dot{q}}(t,q(t), \dot{q}(t)) \\ &+\lambda(t) \eta(t) g_q(q(t), \dot{q}(t)) +\lambda(t) \dot{\eta}(t) g_{\dot{q}}(q(t),\dot{q}(t)) \ dt \end{align} $$ Integrating the terms multiplied by $\dot{\eta}(t)$ by parts and using $\eta(a) = \eta(b) = 0$ yields $$ 0 = \int_a^b \eta(t)\Big[L_q(t,q(t),\dot{q}(t)) - \frac{d}{dt} L_{\dot{q}}(t,q(t), \dot{q}(t)) + \lambda(t) g_q(q(t),\dot{q}(t)) - \frac{d}{dt} \big(\lambda(t) g_{\dot{q}}(q(t),\dot{q}(t)) \big)\Big] dt $$ Since $\eta(t)$ was arbitrary, the term in brackets is $0$. So $$ L_q(t,q(t),\dot{q}(t)) - \frac{d}{dt} L_{\dot{q}}(t,q(t), \dot{q}(t)) = - \lambda(t) g_q(q(t),\dot{q}(t)) + \frac{d}{dt} \big(\lambda(t) g_{\dot{q}}(q(t),\dot{q}(t)) \big) $$ which is different from $(2)$ due to $\lambda(t)$ being differentiated.


I think the problem is that you’re not actually solving a variational problem. You don’t mention the boundary conditions (and neither does the post you link to), and usually you don’t need to worry about boundary conditions in such problems, but in this case the boundary conditions of the global optimum that you’re considering (namely, that $(x(1),y(1))$ is a given unit vector) are such that the constraint $x'^2+y'^2=1$ only allows a single solution (namely $(x(t),y(t))=t(x(1),y(1))$). Thus, there are no other functions to compare against, so we shouldn’t expect a variational approach to work.
For other boundary conditions, with $|(x(1),y(1)|\lt1$, the optimal solution isn’t differentiable (it consists of a line segment that goes beyond $(x(1),y(1))$ and then a line segment that returns to $(x(1),y(1))$), so you can’t find it with a variational approach, either.