-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
lagrange.cljc
299 lines (255 loc) · 8.52 KB
/
lagrange.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
;;
;; Copyright © 2017 Colin Smith.
;; This work is based on the Scmutils system of MIT/GNU Scheme:
;; Copyright © 2002 Massachusetts Institute of Technology
;;
;; This is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3 of the License, or (at
;; your option) any later version.
;;
;; This software is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this code; if not, see <http://www.gnu.org/licenses/>.
;;
(ns sicmutils.mechanics.lagrange
(:refer-clojure :exclude [+ - * / partial])
(:require [sicmutils.numerical.quadrature :as q]
[sicmutils.numerical.minimize :as m]
[sicmutils.calculus.derivative :refer [D partial]]
[sicmutils.function :as f]
[sicmutils.generic :as g :refer [cos sin + - * /]]
[sicmutils.structure :refer [up down]]
[sicmutils.function :as f :refer [compose]]))
(defn state->t
"Extract the time slot from a state tuple.
See [[coordinate]] for more detail."
[s]
(nth s 0))
(defn coordinate
"A convenience function on local tuples. A local tuple describes
the state of a system at a particular time:
```
[t, q, D q, D^2 q]
```
representing time, position, velocity (and optionally acceleration etc.)
[[coordinate]] returns the `q` element, which is expected to be a mapping from
time to a structure of coordinates."
[local]
(nth local 1))
(defn velocity
"Returns the velocity element of a local tuple (by convention, the third
element).
See [[coordinate]] for more detail."
[local]
(nth local 2))
(defn acceleration
"Returns the acceleration element of a local tuple (by convention, the fourth
element).
See [[coordinate]] for more detail."
[local]
(nth local 3))
(def coordinate-tuple up)
(def velocity-tuple up)
(def acceleration-tuple up)
;; The following are the functions that are defined in the SICM
;; book, but NOT in MIT Scmutils. Marked here for possible future
;; relocation
;; ---------------------------------------------------------------
(defn L-free-particle
"The lagrangian of a free particle of mass m. The Lagrangian
returned is a function of the local tuple. Since the particle
is free, there is no potential energy, so the Lagrangian is
just the kinetic energy."
[mass]
(fn [[_ _ v]]
(* (/ 1 2) mass (g/square v))))
(defn L-harmonic
"The Lagrangian of a simple harmonic oscillator (mass-spring
system). m is the mass and k is the spring constant used in
Hooke's law. The resulting Lagrangian is a function of the
local tuple of the system."
[m k]
(fn [[_ q v]]
(- (* (/ 1 2) m (g/square v)) (* (/ 1 2) k (g/square q)))))
(defn L-uniform-acceleration
"The Lagrangian of an object experiencing uniform acceleration
in the negative y direction, i.e. the acceleration due to gravity"
[m g]
(fn [[_ [_ y] v]]
(- (* (/ 1 2) m (g/square v)) (* m g y))))
(defn L-central-rectangular [m U]
(fn [[_ q v]]
(- (* (/ 1 2) m (g/square v))
(U (g/abs q)))))
(defn L-central-polar [m U]
(fn [[_ [r] [rdot φdot]]]
(- (* (/ 1 2) m
(+ (g/square rdot)
(g/square (* r φdot))))
(U r))))
;; ---- end of functions undefined in Scmutils --------
(defn ->L-state
"Constructs a Lagrangian state, also knows as a local tuple"
[t q v & as]
(apply up t q v as))
(def ->local ->L-state)
(defn p->r
"SICM p. 47. Polar to rectangular coordinates of state."
[[_ [r φ]]]
(up (* r (cos φ)) (* r (sin φ))))
(defn Gamma
"Gamma takes a path function (from time to coordinates) to a state
function (from time to local tuple)."
([q]
(let [Dq (D q)]
(-> (fn [t]
(up t (q t) (Dq t)))
(f/with-arity [:exactly 1]))))
([q n]
(let [Dqs (->> q (iterate D) (take (- n 1)))]
(fn [t]
(->> Dqs (map #(% t)) (cons t) (apply up))))))
(def Γ Gamma)
(defn Lagrangian-action
[L q t1 t2]
(q/definite-integral (compose L (Γ q)) t1 t2 {:compile? false}))
(defn Lagrange-equations
[Lagrangian]
(fn [q]
(- (D (compose ((partial 2) Lagrangian) (Γ q)))
(compose ((partial 1) Lagrangian) (Γ q)))))
(defn linear-interpolants
[x0 x1 n]
(let [n+1 (inc n)
dx (/ (- x1 x0) n+1)]
(for [i (range 1 n+1)]
(+ x0 (* i dx)))))
(defn Lagrange-interpolation-function
[ys xs]
(let [n (count ys)]
(assert (= (count xs) n))
(-> (fn [x]
(reduce + 0
(for [i (range n)]
(/ (reduce * 1
(for [j (range n)]
(if (= j i)
(nth ys i)
(- x (nth xs j)))))
(let [xi (nth xs i)]
(reduce * 1
(for [j (range n)]
(cond (< j i) (- (nth xs j) xi)
(= j i) (if (odd? i) -1 1)
:else (- xi (nth xs j))))))))))
(f/with-arity [:exactly 1]))))
(defn Lagrangian->acceleration
[L]
(let [P ((partial 2) L)
F ((partial 1) L)]
(/ (- F
(+ ((partial 0) P)
(* ((partial 1) P) velocity)))
((partial 2) P))))
(defn Lagrangian->state-derivative
"The state derivative of a Lagrangian is a function carrying a state
tuple to its time derivative."
[L]
(let [acceleration (Lagrangian->acceleration L)]
(fn [[_ _ v :as state]]
(up 1 v (acceleration state)))))
(defn qv->state-path
[q v]
#(up % (q %) (v %)))
(defn Lagrange-equations-first-order
[L]
(fn [q v]
(let [state-path (qv->state-path q v)]
(- (D state-path)
(compose (Lagrangian->state-derivative L)
state-path)))))
(defn Lagrangian->energy
[L]
(let [P ((partial 2) L)]
(- (* P velocity) L)))
(defn osculating-path
"Given a state tuple (of finite length), reconstitutes the initial
segment of the Taylor series corresponding to the state tuple data
as a function of t. Time is measured beginning at the point of time
specified in the input state tuple."
[state0]
(let [[t0 q0] state0
k (count state0)]
(fn [t]
(let [dt (- t t0)]
(loop [n 2 sum q0 dt-n:n! dt]
(if (= n k)
sum
(recur (inc n)
(+ sum (* (nth state0 n) dt-n:n!))
(/ (* dt-n:n! dt) n))))))))
(defn Gamma-bar
[f]
(fn [local]
((f (osculating-path local)) (first local))))
(def Γ-bar Gamma-bar)
(defn F->C
"Accepts a coordinate transformation `F` from a local tuple to a new coordinate
structure, and returns a function from `local -> local` that applies the
transformation directly.
[[F->C]] handles local tuples of arbitrary length."
[F]
(fn [local]
(let [n (count local)
f-bar (fn [q-prime]
(let [q (compose F (Gamma q-prime))]
(Gamma q n)))]
((Gamma-bar f-bar) local))))
(defn Dt [F]
(let [G-bar (fn [q]
(D (compose F (Γ q))))]
(Γ-bar G-bar)))
(defn Euler-Lagrange-operator
[L]
(- (Dt ((partial 2) L)) ((partial 1) L)))
(defn L-rectangular
"Lagrangian for a point mass on with the potential energy V(x, y)"
[m V]
(fn [[_ [q0 q1] qdot]]
(- (* (/ 1 2) m (g/square qdot))
(V q0 q1))))
(defn make-path
"SICM p. 23n"
[t0 q0 t1 q1 qs]
(let [n (count qs)
ts (linear-interpolants t0 t1 n)]
(Lagrange-interpolation-function
`[~q0 ~@qs ~q1]
`[~t0 ~@ts ~t1])))
(defn parametric-path-action
"SICM p. 23"
[Lagrangian t0 q0 t1 q1]
(fn [qs]
(let [path (make-path t0 q0 t1 q1 qs)]
(Lagrangian-action Lagrangian path t0 t1))))
(defn find-path
"SICM p. 23. The optional parameter values is a callback which will report
intermediate points of the minimization."
[Lagrangian t0 q0 t1 q1 n & {:keys [observe]}]
(let [initial-qs (linear-interpolants q0 q1 n)
minimizing-qs (m/multidimensional-minimize
(parametric-path-action Lagrangian t0 q0 t1 q1)
initial-qs
:callback observe)]
(make-path t0 q0 t1 q1 minimizing-qs)))
(defn s->r
"SICM p. 83"
[[_ [r θ φ] _]]
(up (* r (sin θ) (cos φ))
(* r (sin θ) (sin φ))
(* r (cos θ))))