Exercise 1.13: Higher-derivative Lagrangians (code)

(require '[sicmutils.env :refer :all])

This exercise completes exercise 1.10 by asking for implementations of the higher-order Lagrange equations that we derived. This was a nice Scheme exercise; I would argue that this implementation should exist in the standard library. But that would ruin the fun of the exercise…

Part A: Acceleration-dependent Lagrangian Implementation

From the book:

Write a procedure to compute the Lagrange equations for Lagrangians that depend upon acceleration, as in exercise 1.10. Note that Gamma can take an optional argument giving the length of the initial segment of the local tuple needed. The default length is 3, giving components of the local tuple up to and including the velocities.

Now that we know the math, the implementation is a straightforward extension of the Lagrange-equations procedure presented in the book:

(defn Lagrange-equations3 [L]
  (fn [q]
    (let [state-path (Gamma q 4)]
      (+ ((square D) (compose ((partial 3) L) state-path))
         (- (D (compose ((partial 2) L) state-path)))
         (compose ((partial 1) L) state-path)))))

Part B: Applying HO-Lagrangians

Now it's time to use the new function. From the book:

Use your procedure to compute the Lagrange equations for the Lagrangian

formula not implemented

Do you recognize the resulting equation of motion?

Here is the Lagrangian described in the problem:

(defn L-1-13 [m k]
  (fn [[_ x _ a]]
    (- (* (/ -1 2) m x a)
       (* (/ 1 2) k (square x)))))

Use the new function to generate the Lagrange equations. This call includes a factor of inline_formula not implemented to make the equation look nice:

(- (((Lagrange-equations3 (L-1-13 'm 'k))
     (literal-function 'x)) 't))

This looks like the equation of motion for a classical harmonic oscillator. Again, I leave this problem with no new physical intuition for what is going on here, or what type of system would need an acceleration dependent Lagrangian. I suspect that we could build a harmonic oscillator for Lagrange equations of any order by properly tuning the Lagrangian. But I don't know why this would be helpful.

Part C: Generalized Lagrange Equations

Now, some more fun with Scheme. It just feels nice to implement Scheme procedures. From the book:

For more fun, write the general Lagrange equation procedure that takes a Lagrangian that depends on any number of derivatives, and the number of derivatives, to produce the required equations of motion.

As a reminder, this is the equation that we need to implement for each coordinate:

formula not implemented

There are two ideas playing together here. Each term takes an element from:

  • an alternating sequence of inline_formula not implemented and inline_formula not implemented

  • a sequence of increasing-index inline_formula not implemented terms

The alternating inline_formula not implemented sequence is similar to a more general idea: take any ordered collection arranged in a cycle, start at some point and walk around the cycle for inline_formula not implemented steps. In Clojure this can be expressed as

(comment
  (cycle [1 -1]))

Now, the general Lagrange-equations* implementation:

(defn Lagrange-equations* [L n]
  (fn [q]
    (let [state-path (Gamma q (inc n))
          terms (map (fn [coef i]
                       (* coef
                          ((expt D i)
                           (compose ((partial (inc i)) L)
                                    state-path))))
                     (cycle [1 -1])
                     (range n))]
      (reduce + terms))))

Generate the Lagrange equations from part b once more to check that we get the same result:

(- (((Lagrange-equations* (L-1-13 'm 'k) 3)
     (literal-function 'x)) 't))

There it is again, the harmonic oscillator. I don't have any intuition for higher order Lagrangians, so I can't cook up any further examples to test the implementation.

Runtimes (1)