Lagrangian-transformations.scm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. ;;; Coordinate Transformation to State Transformation
  21. #|
  22. ;;; if defined F as state function F(t, q, v); (partial 2) F = 0
  23. (define (F->C F)
  24. (define (C state)
  25. (up (time state)
  26. (F state)
  27. (+ (((partial 0) F) state)
  28. (* (((partial 1) F) state)
  29. (velocity state)))))
  30. C)
  31. |#
  32. #|
  33. (define ((F->C F) local)
  34. (let ((n (vector-length local)))
  35. ((Gamma-bar
  36. (lambda (qp)
  37. (Gamma
  38. (compose F (Gamma qp))
  39. n)))
  40. local)))
  41. |#
  42. #|
  43. ;;; version for display in text
  44. (define (F->C F)
  45. (define (f-bar q-prime)
  46. (define q
  47. (compose F (Gamma q-prime)))
  48. (Gamma q))
  49. (Gamma-bar f-bar))
  50. |#
  51. (define (F->C F)
  52. (define (C local)
  53. (let ((n (vector-length local)))
  54. (define (f-bar q-prime)
  55. (define q
  56. (compose F (Gamma q-prime)))
  57. (Gamma q n))
  58. ((Gamma-bar f-bar) local)))
  59. C)
  60. ;;; The following transformations are applicable to
  61. ;;; configuration coordinates.
  62. (define (rectangular->polar rectangular-tuple)
  63. (let ((x (ref rectangular-tuple 0))
  64. (y (ref rectangular-tuple 1)))
  65. (let ((r (sqrt (+ (square x) (square y))))
  66. (phi (atan y x)))
  67. (up r phi))))
  68. (define (r->p tqv)
  69. (rectangular->polar (coordinate tqv)))
  70. (define (polar->rectangular polar-tuple)
  71. (let ((r (ref polar-tuple 0))
  72. (phi (ref polar-tuple 1)))
  73. (let ((x (* r (cos phi)))
  74. (y (* r (sin phi))))
  75. (up x y))))
  76. (define (p->r tqv)
  77. (polar->rectangular (coordinate tqv)))
  78. #|
  79. (show-expression
  80. (velocity
  81. ((F->C p->r)
  82. (->local 't
  83. (coordinate-tuple 'r 'phi)
  84. (velocity-tuple 'rdot 'phidot)))))
  85. (up (+ (* -1 r phidot (sin phi)) (* rdot (cos phi)))
  86. (+ (* r phidot (cos phi)) (* rdot (sin phi))))
  87. (define (L-central-polar m V)
  88. (compose (L-central-rectangular m V)
  89. (F->C p->r)))
  90. (show-expression
  91. ((L-central-polar 'm (literal-function 'V))
  92. (->local 't (coordinate-tuple 'r 'phi)
  93. (velocity-tuple 'rdot 'phidot))))
  94. (+ (* 1/2 m (expt phidot 2) (expt r 2))
  95. (* 1/2 m (expt rdot 2))
  96. (* -1 (V r)))
  97. |#
  98. #|
  99. ;;; Driven pendulum example
  100. (define ((T-pend m l g ys) local)
  101. (let ((t (time local))
  102. (theta (coordinate local))
  103. (thetadot (velocity local)))
  104. (let ((ysdot (D ys)))
  105. (* 1/2 m
  106. (+ (square (* l thetadot))
  107. (square (ysdot t))
  108. (* 2 (ysdot t) l (sin theta) thetadot))))))
  109. (define ((V-pend m l g ys) local)
  110. (let ((t (time local))
  111. (theta (coordinate local)))
  112. (* m g (- (ys t) (* l (cos theta))))))
  113. (define L-pend (- T-pend V-pend))
  114. (show-expression
  115. ((L-pend 'm 'l 'g (literal-function 'y_s))
  116. (->local 't 'theta 'thetadot)))
  117. (+ (* 1/2 (expt l 2) m (expt thetadot 2))
  118. (* l m thetadot ((D y_s) t) (sin theta))
  119. (* g l m (cos theta))
  120. (* -1 g m (y_s t))
  121. (* 1/2 m (expt ((D y_s) t) 2)))
  122. (show-expression
  123. (((Lagrange-equations
  124. (L-pend 'm 'l 'g (literal-function 'y_s)))
  125. (literal-function 'theta))
  126. 't))
  127. (+ (* g l m (sin (theta t)))
  128. (* (expt l 2) m (((expt D 2) theta) t))
  129. (* l m (((expt D 2) y_s) t) (sin (theta t))))
  130. |#
  131. #|
  132. ;;; Same driven pendulum by coordinate transformation
  133. (define ((Lf m g) local)
  134. (let ((q (coordinate local))
  135. (v (velocity local)))
  136. (let ((h (ref q 1)))
  137. (- (* 1/2 m (square v)) (* m g h)))))
  138. (define ((dp-coordinates l y_s) local)
  139. (let ((t (time local))
  140. (theta (coordinate local)))
  141. (let ((x (* l (sin theta)))
  142. (y (- (y_s t) (* l (cos theta)))))
  143. (coordinate-tuple x y))))
  144. (define (L-pend m l g y_s)
  145. (compose (Lf m g)
  146. (F->C (dp-coordinates l y_s))))
  147. (show-expression
  148. ((L-pend 'm 'l 'g (literal-function 'y_s))
  149. (->local 't 'theta 'thetadot)))
  150. (+ (* 1/2 (expt l 2) m (expt thetadot 2))
  151. (* l m thetadot (sin theta) ((D y_s) t))
  152. (* g l m (cos theta))
  153. (* -1 g m (y_s t))
  154. (* 1/2 m (expt ((D y_s) t) 2)))
  155. (show-expression
  156. (((Lagrange-equations
  157. (L-pend 'm 'l 'g (literal-function 'y_s)))
  158. (literal-function 'theta))
  159. 't))
  160. (+ (* g l m (sin (theta t)))
  161. (* (expt l 2) m (((expt D 2) theta) t))
  162. (* l m (((expt D 2) y_s) t) (sin (theta t))))
  163. |#
  164. ;;; Spherical Coordinates (radius, colatitude, longitude)
  165. (define (spherical->rectangular q)
  166. (let ((r (ref q 0))
  167. (theta (ref q 1))
  168. (phi (ref q 2)))
  169. (let ((x (* r (sin theta) (cos phi)))
  170. (y (* r (sin theta) (sin phi)))
  171. (z (* r (cos theta))))
  172. (coordinate-tuple x y z))))
  173. (define (s->r local)
  174. (spherical->rectangular (coordinate local)))
  175. (define (rectangular->spherical q)
  176. (let ((x (ref q 0))
  177. (y (ref q 1))
  178. (z (ref q 2)))
  179. (let ((r (sqrt (+ (* x x) (* y y) (* z z)))))
  180. (let ((theta (acos (/ z r)))
  181. (phi (atan y x)))
  182. (up r theta phi)))))
  183. (define (r->s local)
  184. (rectangular->spherical (coordinate local)))
  185. #|
  186. (define (L3-central m Vr)
  187. (define (Vs local)
  188. (let ((r (ref (coordinate local) 0)))
  189. (Vr r)))
  190. (- (T3-spherical m) Vs))
  191. (define ((ang-mom-z m) local)
  192. (let ((q (coordinate local))
  193. (v (velocity local)))
  194. (ref (cross-product q (* m v)) 2)))
  195. (show-expression
  196. ((compose (ang-mom-z 'm) (F->C s->r))
  197. (->local 't
  198. (coordinate-tuple 'r 'theta 'phi)
  199. (velocity-tuple 'rdot 'thetadot 'phidot))))
  200. (* m (expt r 2) phidot (expt (sin theta) 2))
  201. (show-expression
  202. ((Lagrangian->energy
  203. (L3-central 'm (literal-function 'V)))
  204. (->local 't
  205. (coordinate-tuple 'r 'theta 'phi)
  206. (velocity-tuple 'rdot 'thetadot 'phidot))))
  207. (+ (* 1/2 m (expt r 2) (expt phidot 2) (expt (sin theta) 2))
  208. (* 1/2 m (expt r 2) (expt thetadot 2))
  209. (* 1/2 m (expt rdot 2))
  210. (V r))
  211. |#
  212. ;;; Rotations about the rectangular axes
  213. (define* ((Rx angle) q)
  214. (let ((ca (cos angle))
  215. (sa (sin angle)))
  216. (let ((x (ref q 0))
  217. (y (ref q 1))
  218. (z (ref q 2)))
  219. (up
  220. x
  221. (- (* ca y) (* sa z))
  222. (+ (* ca z) (* sa y))))))
  223. (define* ((Ry angle) q)
  224. (let ((ca (cos angle))
  225. (sa (sin angle)))
  226. (let ((x (ref q 0))
  227. (y (ref q 1))
  228. (z (ref q 2)))
  229. (up
  230. (+ (* ca x) (* sa z))
  231. y
  232. (- (* ca z) (* sa x))))))
  233. (define* ((Rz angle) q)
  234. (let ((ca (cos angle))
  235. (sa (sin angle)))
  236. (let ((x (ref q 0))
  237. (y (ref q 1))
  238. (z (ref q 2)))
  239. (up
  240. (- (* ca x) (* sa y))
  241. (+ (* ca y) (* sa x))
  242. z))))