The true path travelled by an object will minimize the Action S, which truly is an amazing insight. The Lagrangian L is given by the potential energy subtracted from the kinetic energy.
The action is given by the integral
Which is approximated with discretization to
Varying the Action with respect to the path in x and y direction gives a system of non-linear equations
These systems of equations are solved by the Jacobi Method, or multidimensional Newton-Raphson method.
The result is seen below for Earth's orbit around the Sun.
The initial guessed path is a straight line from start to end position. As seen, the curved true path has a lower total action S.
The system can also be seen as an "un"constrained optimization problem, if we hardcode the end points into the cost function and the gradient.
The x and y positions are put into a single state vector
The gradient is then