Classical mechanics with calculus of variations, and optimal control