Least action principle — numerical simulation strangeness I'm trying to get some experience with the least action principle, and for this I chose a simple 1-dimensional problem of a particle moving in some field. The least action principle would then look like:
$$\int_{t_1}^{t_2}(T(v)-U(x))dt=\min$$
So I discretize time into some points and try to minimize the sum:
$$\sum_{i=1}^n (T(v_i)-U(x_i))\Delta t.$$
But I get into some strange results: first, if I don't constrain the system, the sum appears unbounded from below. Well, it's understandable because there can be multiple solutions corresponding to different initial/boundary conditions. OK, I choose some values for $x_1$ and $x_n$ as constraints. But even the sum appears unbounded. Well, I then choose to reduce possible range of $x_i$, and the sum finally can be minimized...
But the result appears complete nonsense. Here's the result for $n=10$, $t_1=0$, $t_2=1$, $|x_i|<5$:
Positions

Velocities

Here velocities don't seem to reflect change in positions.
What am I missing here? Should I add some other constraints, or have I done some simple mistake?
 A: As said in this answer, velocity and position are not varied independently. Indeed, when deriving Euler-Lagrange equations, we explicitly use the fact that $\delta v=\frac d{dt}\delta x$.
So, when I add the constraint $v_i=\frac{x_{i+1}-x_i}{\Delta t}$, specifying $x_1$ and $x_n$ remains the only additional thing to converge to the solution. For example, setting $U=x^4-4x^3+4.5x^2$, $x_1=0$, $x_n=2.651$ and $n=51$, I get:
Positions:

Velocities:

Here the last point of velocity is wrong, but it's an artifact of the constraint: I've used a right-hand finite difference derivative, which can't be done for $v_n$. This can be fixed by choosing some other difference scheme, but for the purposes of this answer it's an unimportant implementation detail.
What's more important, is that if we choose $x_n>2.651$ in this example, the action appears unbounded from below even with the correct constraints. I believe this is no longer a problem in the implementation, but rather a result from the fact that the action just has to be stationary, but not minimal, so minimization is not a good enough procedure to obtain a true trajectory.
