Lagrangian.scm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. ;;;; Variational Mechanics
  21. ;;; Caution... This file is case sensitive!
  22. ;;; However, there are alternative names for the actual data types.
  23. (define coordinate-tuple up)
  24. (define velocity-tuple up)
  25. (define acceleration-tuple up)
  26. (define momentum-tuple down)
  27. ;;; Lagrangian mechanics requires a configuration
  28. ;;; space Q, and a function L:RxQxQ' --> R
  29. ;;; Mechanical systems have state at each instant. The state is the
  30. ;;; information required, along with the equations of motion, to
  31. ;;; determine the future of the system.
  32. ;;; At every instant a system has a kinematic state, which has the
  33. ;;; time, the configuration, and the rate of change of the
  34. ;;; configuration. Lagrangian mechanics is formulated in terms of
  35. ;;; the kinematic state.
  36. ;;; Kinematic states and their derivatives are represented as Scheme
  37. ;;; vectors, with components time, configuration, and derivatives.
  38. (define (->local t q qdot . derivs)
  39. (apply vector t q qdot derivs))
  40. (define ->state ->local)
  41. (define ->L-state ->local)
  42. (define (state->n-dof state)
  43. (let ((q (vector-ref state 1)))
  44. (if (up? q)
  45. (s:length q)
  46. 1)))
  47. ;;; Selectors are provided for the components of a state.
  48. (define (state->t state)
  49. (if (not (and (vector? state) (fix:> (vector-length state) 0)))
  50. (error "Cannot extract time from" state))
  51. (ref state 0))
  52. (define (state->q state)
  53. (if (not (and (vector? state) (fix:> (vector-length state) 1)))
  54. (error "Cannot extract coordinate from" state))
  55. (ref state 1))
  56. (define (state->qdot state)
  57. (if (not (and (vector? state) (fix:> (vector-length state) 2)))
  58. (error "Cannot extract velocity from" state))
  59. (ref state 2))
  60. (define (state->qddot state)
  61. (if (not (and (vector? state) (fix:> (vector-length state) 3)))
  62. (error "Cannot extract acceleration from" state))
  63. (ref state 3))
  64. (define time state->t)
  65. (define coordinate state->q)
  66. (define velocity state->qdot)
  67. (define acceleration state->qddot)
  68. (define coordinates state->q)
  69. (define velocities state->qdot)
  70. (define accelerations state->qddot)
  71. (define Q state->q)
  72. (define Qdot state->qdot)
  73. (define Qdotdot state->qddot)
  74. (define (literal-Lagrangian-state n-dof)
  75. (up (literal-number (generate-uninterned-symbol 't))
  76. (s:generate n-dof 'up
  77. (lambda (i)
  78. (literal-number (generate-uninterned-symbol 'x))))
  79. (s:generate n-dof 'up
  80. (lambda (i)
  81. (literal-number (generate-uninterned-symbol 'v))))))
  82. ;;;; Chapter 1
  83. ;;; Paths in the configuration manifold are functions that give a
  84. ;;; configuration for each time. From such a path we can construct a
  85. ;;; path in the kinematic state space. If such a path is described
  86. ;;; in terms of generalized coordinates, we have
  87. #|
  88. (define (path->state-path q)
  89. (lambda (t)
  90. (->local t
  91. (q t)
  92. ((D q) t))))
  93. |#
  94. (define* (path->state-path q #:optional n)
  95. (if (default-object? n)
  96. (set! n 3)
  97. (assert (fix:> n 1)))
  98. (lambda (t)
  99. (list->vector
  100. (cons t
  101. (cons (g:apply q (list t)) ; FBE: (q t) -> (g:apply q (list t))
  102. (let lp ((i (fix:- n 2)) (fi (D q)))
  103. (if (fix:= i 0)
  104. '()
  105. (cons (fi t)
  106. (lp (- i 1)
  107. (D fi))))))))))
  108. (define Gamma path->state-path)
  109. #|
  110. ;;; Another way to make Gamma
  111. (define* ((path->state-path q #:optional n) t)
  112. (if (default-object? n) (set! n 3))
  113. (list->vector
  114. (stream-head
  115. (cons-stream t
  116. (cons-stream (q t)
  117. (map-stream (lambda (e) (((expt D e) q) t))
  118. natural-number-stream)))
  119. n)))
  120. |#
  121. #|
  122. ;;; Can we do it this way? No...
  123. ;;; We don't know number of degrees of freedom when we build state vector.
  124. (define (path->state q)
  125. (->local identity
  126. q
  127. (D q)))
  128. |#
  129. ;;; A Lagrangian is an example of an L-function.
  130. ;;; An L-function takes a scalar argument and 2 vector arguments
  131. ;;; (t, q, q-dot). An L-function produces a scalar result.
  132. (define (make-Lagrangian kinetic-energy potential-energy)
  133. (- kinetic-energy potential-energy))
  134. #|
  135. (define ((L-free-particle mass) local)
  136. (let ((v (velocity local)))
  137. (* 1/2 mass (square v))))
  138. (show-expression
  139. ((L-free-particle 'm)
  140. (->local 't
  141. (coordinate-tuple 'x 'y 'z)
  142. (velocity-tuple 'xdot 'ydot 'zdot))))
  143. (+ (* 1/2 m (expt xdot 2))
  144. (* 1/2 m (expt ydot 2))
  145. (* 1/2 m (expt zdot 2)))
  146. (show-expression
  147. ((compose
  148. (L-free-particle 'm)
  149. (Gamma (coordinate-tuple (literal-function 'x)
  150. (literal-function 'y)
  151. (literal-function 'z))))
  152. 't))
  153. (+ (* 1/2 (expt ((D x) t) 2) m)
  154. (* 1/2 (expt ((D y) t) 2) m)
  155. (* 1/2 (expt ((D z) t) 2) m))
  156. |#
  157. ;;; Given a Lagrangian, we can obtain Lagrange's equations of motion.
  158. ;;; FBE: start
  159. ;; (define* ((Lagrange-equations Lagrangian #:optional dissipation-function) q)
  160. ;; (let ((state-path (Gamma q)))
  161. ;; (if (default-object? dissipation-function)
  162. ;; (- (D (compose ((partial 2) Lagrangian) state-path))
  163. ;; (compose ((partial 1) Lagrangian) state-path))
  164. ;; (- (D (compose ((partial 2) Lagrangian) state-path))
  165. ;; (compose ((partial 1) Lagrangian) state-path)
  166. ;; (- (compose ((partial 2) dissipation-function) state-path))))))
  167. (define* (Lagrange-equations Lagrangian #:optional dissipation-function)
  168. (lambda (q)
  169. (let ((state-path (Gamma q)))
  170. (if (default-object? dissipation-function)
  171. (- (D (compose ((partial 2) Lagrangian) state-path))
  172. (compose ((partial 1) Lagrangian) state-path))
  173. (- (D (compose ((partial 2) Lagrangian) state-path))
  174. (compose ((partial 1) Lagrangian) state-path)
  175. (- (compose ((partial 2) dissipation-function) state-path)))))))
  176. ;;; FBE end
  177. #|
  178. (define ((Lagrange-equations Lagrangian) q)
  179. (let ((local-path (Gamma q)))
  180. (- (D (compose ((partial 2) Lagrangian) local-path))
  181. (compose ((partial 1) Lagrangian) local-path))))
  182. |#
  183. #|
  184. (define (test-path t)
  185. (coordinate-tuple (+ (* 'a t) 'a0)
  186. (+ (* 'b t) 'b0)
  187. (+ (* 'c t) 'c0)))
  188. (print-expression
  189. (((Lagrange-equations (L-free-particle 'm))
  190. test-path)
  191. 't))
  192. (down 0 0 0)
  193. (show-expression
  194. (((Lagrange-equations (L-free-particle 'm))
  195. (literal-function 'x))
  196. 't))
  197. (* m (((expt D 2) x) t))
  198. ;;; For example, consider the Harmonic oscillator with
  199. ;;; spring constant, k, and mass, m.
  200. (define ((L-harmonic m k) local)
  201. (let ((q (coordinate local))
  202. (v (velocity local)))
  203. (- (* 1/2 m (square v))
  204. (* 1/2 k (square q)))))
  205. (show-expression
  206. (((Lagrange-equations (L-harmonic 'm 'k))
  207. (literal-function 'x))
  208. 't))
  209. (+ (* k (x t)) (* m (((expt D 2) x) t)))
  210. (show-expression
  211. (((Lagrange-equations (L-harmonic 'm 'k))
  212. (lambda (t) (* 'a (cos (+ (* 'omega t) 'phi)))))
  213. 't))
  214. (+ (* a k (cos (+ (* omega t) phi)))
  215. (* -1 a m (expt omega 2) (cos (+ (* omega t) phi))))
  216. |#
  217. #|
  218. (define ((L-uniform-acceleration m g) local)
  219. (let ((q (coordinate local))
  220. (v (velocity local)))
  221. (let ((y (ref q 1)))
  222. (- (* 1/2 m (square v)) (* m g y)))))
  223. (show-expression
  224. (((Lagrange-equations
  225. (L-uniform-acceleration 'm 'g))
  226. (coordinate-tuple (literal-function 'x)
  227. (literal-function 'y)))
  228. 't))
  229. (down (* m (((expt D 2) x) t))
  230. (+ (* g m) (* m (((expt D 2) y) t))))
  231. (define ((L-central-rectangular m V) local)
  232. (let ((q (coordinate local))
  233. (v (velocity local)))
  234. (- (* 1/2 m (square v))
  235. (V (sqrt (square q))))))
  236. (show-expression
  237. (((Lagrange-equations
  238. (L-central-rectangular 'm (literal-function 'V)))
  239. (coordinate-tuple (literal-function 'x) (literal-function 'y)))
  240. 't))
  241. (down
  242. (+ (* m (((expt D 2) x) t))
  243. (/ (* ((D V) (sqrt (+ (expt (x t) 2) (expt (y t) 2)))) (x t))
  244. (sqrt (+ (expt (x t) 2) (expt (y t) 2)))))
  245. (+ (* m (((expt D 2) y) t))
  246. (/ (* ((D V) (sqrt (+ (expt (x t) 2) (expt (y t) 2)))) (y t))
  247. (sqrt (+ (expt (x t) 2) (expt (y t) 2))))))
  248. |#
  249. #|
  250. ;;; Consider planar motion in a central force field, with an arbitrary
  251. ;;; potential, U, depending only on the radius. The generalized
  252. ;;; coordinates are polar.
  253. (define ((L-central-polar m V) local)
  254. (let ((q (coordinate local))
  255. (qdot (velocity local)))
  256. (let ((r (ref q 0))
  257. (phi (ref q 1))
  258. (rdot (ref qdot 0))
  259. (phidot (ref qdot 1)))
  260. (- (* 1/2 m
  261. (+ (square rdot)
  262. (square (* r phidot))) )
  263. (V r)))))
  264. (show-expression
  265. (((Lagrange-equations
  266. (L-central-polar 'm (literal-function 'V)))
  267. (coordinate-tuple (literal-function 'r)
  268. (literal-function 'phi)))
  269. 't))
  270. (down
  271. (+ (* -1 m (r t) (expt ((D phi) t) 2))
  272. (* m (((expt D 2) r) t))
  273. ((D V) (r t)))
  274. (+ (* 2 m ((D r) t) (r t) ((D phi) t))
  275. (* m (((expt D 2) phi) t) (expt (r t) 2))))
  276. |#
  277. #|
  278. ;;; Coupled harmonic oscillators.
  279. (define ((L-coupled-harmonic m k) state)
  280. (let ((q (coordinate state))
  281. (qdot (velocity state)))
  282. (- (* 1/2 qdot m qdot)
  283. (* 1/2 q k q))))
  284. (show-expression
  285. (((Lagrange-equations
  286. (L-coupled-harmonic (down (down 'm_1 0) (down 0 'm_2))
  287. (down (down 'k_1 'c) (down 'c 'k_2))))
  288. (coordinate-tuple (literal-function 'x)
  289. (literal-function 'y)))
  290. 't))
  291. (down (+ (* c (y t)) (* k_1 (x t)) (* m_1 (((expt D 2) x) t)))
  292. (+ (* c (x t)) (* k_2 (y t)) (* m_2 (((expt D 2) y) t))))
  293. |#
  294. #|
  295. ;;; Pendulum of mass m2 and length b, hanging from a support of mass
  296. ;;; m1 that is free to move horizontally (from Groesberg, Advanced
  297. ;;; Mechanics, p. 72)
  298. (define ((L-sliding-pend m1 m2 b g) state)
  299. (let ((q (coordinate state))
  300. (qdot (velocity state)))
  301. (let* ((x (ref q 0))
  302. (xdot (ref qdot 0))
  303. (theta (ref q 1))
  304. (thetadot (ref qdot 1))
  305. (rel-pend-vel
  306. (* b thetadot (velocity-tuple (cos theta) (sin theta))))
  307. (pend-vel (+ rel-pend-vel (velocity-tuple xdot 0)))
  308. (Tpend (* 1/2 m2 (square pend-vel)))
  309. (Tsupport (* 1/2 m1 (square xdot)))
  310. (V (- (* m2 g b (cos theta)))))
  311. (+ Tpend Tsupport (- V)))))
  312. (show-expression
  313. (((Lagrange-equations (L-sliding-pend 'm_1 'm_2 'b 'g))
  314. (coordinate-tuple (literal-function 'x)
  315. (literal-function 'theta)))
  316. 't))
  317. (down
  318. (+ (* -1 b m_2 (sin (theta t)) (expt ((D theta) t) 2))
  319. (* b m_2 (((expt D 2) theta) t) (cos (theta t)))
  320. (* m_1 (((expt D 2) x) t))
  321. (* m_2 (((expt D 2) x) t)))
  322. (+ (* (expt b 2) m_2 (((expt D 2) theta) t))
  323. (* b g m_2 (sin (theta t)))
  324. (* b m_2 (((expt D 2) x) t) (cos (theta t)))))
  325. ;;; Nicer treatment
  326. (define ((F-sliding-pend l) state)
  327. (let ((q (coordinate state)))
  328. (let ((x (ref q 0))
  329. (theta (ref q 1)))
  330. (up (up x 0)
  331. (up (+ x (* l (sin theta)))
  332. (* -1 l (cos theta)))))))
  333. (define ((2-free m1 m2 g) state)
  334. (let ((v1 (ref (velocity state) 0))
  335. (v2 (ref (velocity state) 1))
  336. (h1 (ref (coordinate state) 0 1))
  337. (h2 (ref (coordinate state) 1 1)))
  338. (- (+ (* 1/2 m1 (square v1))
  339. (* 1/2 m2 (square v2)))
  340. (+ (* m1 g h1)
  341. (* m2 g h2)))))
  342. (define (L-sliding-pend m1 m2 l g)
  343. (compose (2-free m1 m2 g)
  344. (F->C (F-sliding-pend l))))
  345. (show-expression
  346. (((Lagrange-equations
  347. (L-sliding-pend 'm_1 'm_2 'b 'g))
  348. (up (literal-function 'x)
  349. (literal-function 'theta)))
  350. 't))
  351. (down
  352. (+ (* -1 b m_2 (sin (theta t)) (expt ((D theta) t) 2))
  353. (* b m_2 (((expt D 2) theta) t) (cos (theta t)))
  354. (* m_1 (((expt D 2) x) t))
  355. (* m_2 (((expt D 2) x) t)))
  356. (+ (* (expt b 2) m_2 (((expt D 2) theta) t))
  357. (* b g m_2 (sin (theta t)))
  358. (* b m_2 (cos (theta t)) (((expt D 2) x) t))))
  359. |#
  360. #|
  361. ;;; Consider a simple pendulum with Rayleigh dissipation:
  362. (define ((L-pendulum g m l) state)
  363. (let ((theta (coordinate state))
  364. (thetadot (velocity state)))
  365. (+ (* 1/2 m (square (* l thetadot)))
  366. (* g m l (cos theta)))))
  367. (define ((Rayleigh-dissipation k) state)
  368. (let ((qdot (velocity state)))
  369. (* qdot k qdot)))
  370. (show-expression
  371. (((Lagrange-equations (L-pendulum 'g 'm 'l)
  372. (Rayleigh-dissipation 'k))
  373. (literal-function 'theta))
  374. 't))
  375. (+ (* 2 k ((D theta) t))
  376. (* g l m (sin (theta t)))
  377. (* (expt l 2) m (((expt D 2) theta) t)))
  378. |#
  379. #|
  380. ;;; Can group coordinates. Procedures don't care.
  381. (define ((L-two-particle m1 m2) local)
  382. (let ((x (coordinate local))
  383. (v (velocity local))
  384. (V (literal-function 'V (-> (X (^ Real 2) (^ Real 2)) Real))))
  385. (let ((x1 (ref x 0)) (x2 (ref x 1))
  386. (v1 (ref v 0)) (v2 (ref v 1)))
  387. (- (+ (* 1/2 m1 (square v1))
  388. (* 1/2 m2 (square v2)))
  389. (V x1 x2)))))
  390. (show-expression
  391. (((Lagrange-equations (L-two-particle 'm_1 'm_2))
  392. (coordinate-tuple
  393. (coordinate-tuple (literal-function 'x_1) (literal-function 'y_1))
  394. (coordinate-tuple (literal-function 'x_2) (literal-function 'y_2))))
  395. 't))
  396. (down
  397. (down
  398. (+ (* m_1 (((expt D 2) x_1) t))
  399. (((partial 0 0) V) (up (x_1 t) (y_1 t)) (up (x_2 t) (y_2 t))))
  400. (+ (* m_1 (((expt D 2) y_1) t))
  401. (((partial 0 1) V) (up (x_1 t) (y_1 t)) (up (x_2 t) (y_2 t)))))
  402. (down
  403. (+ (* m_2 (((expt D 2) x_2) t))
  404. (((partial 1 0) V) (up (x_1 t) (y_1 t)) (up (x_2 t) (y_2 t))))
  405. (+ (* m_2 (((expt D 2) y_2) t))
  406. (((partial 1 1) V) (up (x_1 t) (y_1 t)) (up (x_2 t) (y_2 t))))))
  407. |#
  408. ;;; For integrating Lagrange's equations we need them in a form which
  409. ;;; has the highest derivative isolated.
  410. ;;; The following is an explicit solution for the second-derivative
  411. ;;; from Lagrange's equations, based on the operator form above:
  412. #|
  413. (define (Lagrangian->acceleration Lagrangian)
  414. (let ((P ((partial 2) Lagrangian))
  415. (F ((partial 1) Lagrangian)))
  416. (let ((dP/dq ((partial 1) P))
  417. (dP/dqdot ((partial 2) P))
  418. (dP/dt ((partial 0) P))
  419. (qdot state->qdot))
  420. (/ (- F (+ (* dP/dq qdot) dP/dt))
  421. dP/dqdot))))
  422. (define (Lagrangian->acceleration L)
  423. (let ((P ((partial 2) L))
  424. (F ((partial 1) L)))
  425. (/ (- F
  426. (+ ((partial 0) P)
  427. (* ((partial 1) P) velocity)))
  428. ((partial 2) P))))
  429. (define ((Lagrangian->acceleration L) state)
  430. (let ((P ((partial 2) L))
  431. (F ((partial 1) L)))
  432. (* (s:inverse (velocity state)
  433. (((partial 2) P) state)
  434. (velocity state))
  435. ((- F
  436. (+ ((partial 0) P)
  437. (* ((partial 1) P) velocity)))
  438. state))))
  439. |#
  440. #|
  441. ;;; Note: the inverse may have a determinant of zero.
  442. ;;; FBE start
  443. (define* (Lagrangian->acceleration L #:optional dissipation-function)
  444. (lambda (state)
  445. (if (default-object? dissipation-function)
  446. (let ((P ((partial 2) L))
  447. (F ((partial 1) L)))
  448. (* (s:inverse (velocity state)
  449. (((partial 2) P) state)
  450. (velocity state))
  451. ((- F
  452. (+ ((partial 0) P)
  453. (* ((partial 1) P) velocity)))
  454. state)))
  455. (let ((P ((partial 2) L))
  456. (F ((partial 1) L))
  457. (Diss ((partial 2) dissipation-function)))
  458. (* (s:inverse (velocity state)
  459. (((partial 2) P) state)
  460. (velocity state))
  461. ((- (- F Diss)
  462. (+ ((partial 0) P)
  463. (* ((partial 1) P) velocity)))
  464. state))))))
  465. ;;; FBE end
  466. ;;; FBE start
  467. (define* ((Lagrangian->acceleration L #:optional dissipation-function) state)
  468. (if (default-object? dissipation-function)
  469. (let ((P ((partial 2) L))
  470. (F ((partial 1) L)))
  471. (let ((minv
  472. (s:inverse (velocity state)
  473. (((partial 2) P) state)
  474. (velocity state))))
  475. (* minv
  476. ((- F
  477. (+ ((partial 0) P)
  478. (* ((partial 1) P) velocity)))
  479. state))))
  480. (let ((P ((partial 2) L))
  481. (F ((partial 1) L))
  482. (Diss ((partial 2) dissipation-function)))
  483. (let ((minv
  484. (s:inverse (velocity state)
  485. (((partial 2) P) state)
  486. (velocity state))))
  487. (* minv
  488. ((- (- F Diss)
  489. (+ ((partial 0) P)
  490. (* ((partial 1) P) velocity)))
  491. state))))))
  492. |#
  493. ;;; FBE start
  494. (define* (Lagrangian->acceleration L #:optional dissipation-function)
  495. (lambda (state)
  496. (let ((P ((partial 2) L))
  497. (F ((partial 1) L)))
  498. (if (default-object? dissipation-function)
  499. (solve-linear-left (((partial 2) P) state)
  500. ((- F
  501. (+ ((partial 0) P)
  502. (* ((partial 1) P) velocity)))
  503. state))
  504. (solve-linear-left (((partial 2) P) state)
  505. ((- (- F
  506. ((partial 2) dissipation-function))
  507. (+ ((partial 0) P)
  508. (* ((partial 1) P) velocity)))
  509. state))))))
  510. ;;; FBE end
  511. #|
  512. ;;; Thus, for example, we can obtain the general form of the vector
  513. ;;; of accelerations as a function of the positions, and velocities:
  514. (show-expression
  515. ((Lagrangian->acceleration (L-sliding-pend 'm_1 'm_2 'b 'g))
  516. (->local 't
  517. (coordinate-tuple 'x 'theta)
  518. (velocity-tuple 'xdot 'thetadot))))
  519. (up
  520. (+
  521. (/ (* b m_2 (expt thetadot 2) (sin theta))
  522. (+ (* m_2 (expt (sin theta) 2)) m_1))
  523. (/ (* g m_2 (sin theta) (cos theta))
  524. (+ (* m_2 (expt (sin theta) 2)) m_1)))
  525. (+
  526. (/ (* -1 m_2 (expt thetadot 2) (sin theta) (cos theta))
  527. (+ (* m_2 (expt (sin theta) 2)) m_1))
  528. (/ (* -1 g m_1 (sin theta))
  529. (+ (* b m_2 (expt (sin theta) 2)) (* b m_1)))
  530. (/ (* -1 g m_2 (sin theta))
  531. (+ (* b m_2 (expt (sin theta) 2)) (* b m_1)))))
  532. |#
  533. ;;; Lagrange equations in first-order form.
  534. (define* ((Lagrange-equations-1 L) q v)
  535. (let ((local-path (qv->local-path q v)))
  536. (- (D local-path)
  537. (compose (local-state-derivative L)
  538. local-path))))
  539. (define* ((local-state-derivative L) local)
  540. (->local 1
  541. (velocity local)
  542. ((Lagrangian->acceleration L) local)))
  543. (define Lagrange-equations-first-order
  544. Lagrange-equations-1)
  545. (define* ((qv->local-path q v) t)
  546. (->local t (q t) (v t)))
  547. #|
  548. (define ((Lagrange-equations-1 L) q v)
  549. (let ((local-path (qv->local-path q v)))
  550. (- (D local-path)
  551. (->local 1
  552. (compose velocity local-path)
  553. (compose (Lagrangian->acceleration L)
  554. local-path)))))
  555. |#
  556. #|
  557. (show-expression
  558. (((Lagrange-equations-1 (L-harmonic 'm 'k))
  559. (coordinate-tuple (literal-function 'x)
  560. (literal-function 'y))
  561. (velocity-tuple (literal-function 'v_x)
  562. (literal-function 'v_y)))
  563. 't))
  564. (up 0
  565. (up (+ ((D x) t) (* -1 (v_x t))) (+ ((D y) t) (* -1 (v_y t))))
  566. (up (+ (/ (* k (x t)) m) ((D v_x) t)) (+ (/ (* k (y t)) m) ((D v_y) t))))
  567. |#
  568. #|
  569. (define (Lagrangian->state-derivative L)
  570. (let ((acceleration (Lagrangian->acceleration L)))
  571. (lambda (state)
  572. (up
  573. 1
  574. (velocity state)
  575. (acceleration state)))))
  576. |#
  577. (define* (Lagrangian->state-derivative L #:optional dissipation-function)
  578. (if (default-object? dissipation-function)
  579. (let ((acceleration (Lagrangian->acceleration L)))
  580. (lambda (state)
  581. (up
  582. 1
  583. (velocity state)
  584. (acceleration state))))
  585. (let ((acceleration (Lagrangian->acceleration L dissipation-function)))
  586. (lambda (state)
  587. (up
  588. 1
  589. (velocity state)
  590. (acceleration state))))))
  591. #|
  592. (print-expression
  593. ((Lagrangian->state-derivative (L-pendulum 'g 'm 'l)
  594. (Rayleigh-dissipation 'k))
  595. (up 't 'theta 'thetadot)))
  596. (up 1
  597. thetadot
  598. (+ (/ (* -1 g (sin theta)) l)
  599. (/ (* -2 k thetadot) (* (expt l 2) m))))
  600. |#
  601. ;;; Given a Lagrangian, we can make an energy function on (t, Q, Qdot).
  602. (define (Lagrangian->energy L)
  603. (let ((P ((partial 2) L)))
  604. (- (* P velocity) L)))
  605. ;;; On a trajectory there may be power lost (if dissipation)
  606. ;;; The following produces the power lost.
  607. (define* ((Lagrangian->power-loss L) q)
  608. (D (compose (Lagrangian->energy L)
  609. (Gamma q))))
  610. #|
  611. ;;; Alternatively
  612. (define ((Lagrangian->power-loss L) q)
  613. (- (* ((Lagrange-equations L) q) (D q))
  614. (compose ((partial 0) L)
  615. (Gamma q))))
  616. |#
  617. #|
  618. ;;; For example, on a specified trajectory, we can compute the energy,
  619. ;;; which turns out to be T+V.
  620. (show-expression
  621. ((compose
  622. (Lagrangian->energy (L-central-polar 'm (literal-function 'U)))
  623. (Gamma
  624. (coordinate-tuple (literal-function 'r) (literal-function 'phi))))
  625. 't))
  626. (+ (* 1/2 m (expt (r t) 2) (expt ((D phi) t) 2))
  627. (* 1/2 m (expt ((D r) t) 2))
  628. (U (r t)))
  629. ;;; In fact, we can see how the energy is conserved:
  630. (show-expression
  631. (((Lagrangian->power-loss (L-central-polar 'm (literal-function 'U)))
  632. (coordinate-tuple (literal-function 'r) (literal-function 'phi)))
  633. 't))
  634. (+ (* m (((expt D 2) phi) t) ((D phi) t) (expt (r t) 2))
  635. (* m (expt ((D phi) t) 2) (r t) ((D r) t))
  636. (* m (((expt D 2) r) t) ((D r) t))
  637. (* ((D U) (r t)) ((D r) t)))
  638. ;;; This last expression is (nontrivially!) zero on any trajectory
  639. ;;; which satisfies Lagrange's equations.
  640. |#
  641. #|
  642. ;;; Note, this can be implemented in terms of T-CURVILINEAR.
  643. (define ((T3-spherical m) local)
  644. (let ((t (time local))
  645. (q (coordinate local))
  646. (qdot (velocity local)))
  647. (let ((r (ref q 0))
  648. (theta (ref q 1))
  649. (phi (ref q 2))
  650. (rdot (ref qdot 0))
  651. (thetadot (ref qdot 1))
  652. (phidot (ref qdot 2)))
  653. (* 1/2 m
  654. (+ (square rdot)
  655. (square (* r thetadot))
  656. (square (* r (sin theta) phidot)))))))
  657. (define (L3-central m Vr)
  658. (define (Vs local)
  659. (let ((r (ref (coordinate local) 0)))
  660. (Vr r)))
  661. (- (T3-spherical m) Vs))
  662. (show-expression
  663. (((partial 1) (L3-central 'm (literal-function 'V)))
  664. (->local 't
  665. (coordinate-tuple 'r 'theta 'phi)
  666. (velocity-tuple 'rdot 'thetadot 'phidot))))
  667. (down
  668. (+ (* m r (expt phidot 2) (expt (sin theta) 2))
  669. (* m r (expt thetadot 2))
  670. (* -1 ((D V) r)))
  671. (* m (expt r 2) (expt phidot 2) (cos theta) (sin theta))
  672. 0)
  673. (show-expression
  674. (((partial 2) (L3-central 'm (literal-function 'V)))
  675. (->local 't
  676. (coordinate-tuple 'r 'theta 'phi)
  677. (velocity-tuple 'rdot 'thetadot 'phidot))))
  678. (down (* m rdot)
  679. (* m (expt r 2) thetadot)
  680. (* m (expt r 2) phidot (expt (sin theta) 2)))
  681. |#