Morse theory and the calculus of variations