pendulum.scm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. ;;; THE PENDULUM
  21. (declare (usual-integrations))
  22. ;;; definition H = p^2/(2alpha) - beta cos(theta)
  23. ;;; ASSUME alpha > 0 and beta > 0
  24. ;;; alpha = ml^2 beta = mgl for ordinary pendulum
  25. (define* ((Hpendulum alpha beta) state)
  26. (let ((theta (state->q state))
  27. (ptheta (state->p state)))
  28. (- (/ (square ptheta) (* 2 alpha))
  29. (* beta (cos theta)))))
  30. (define (pendulum-sysder alpha beta)
  31. (Hamiltonian->state-derivative (Hpendulum alpha beta)))
  32. (define pendulum-Hamiltonian Hpendulum)
  33. ;;;----------------------------------------------------------------
  34. ;;; oscillating case
  35. (define (pendulum-oscillating-frequency alpha beta E)
  36. (let ((k (sqrt (/ (+ E beta) (* 2. beta))))
  37. (omega0 (sqrt (abs (/ beta alpha)))))
  38. (/ (* pi omega0) (* 2. (first-elliptic-integral k)))))
  39. (define (pendulum-oscillating-angle alpha beta E)
  40. (let ((k (sqrt (/ (+ E beta) (* 2. beta))))
  41. (omega-0 (sqrt (/ beta alpha))))
  42. (lambda (time)
  43. (Jacobi-elliptic-functions
  44. (* omega-0 time)
  45. k
  46. (lambda (sn cn dn)
  47. (* 2. (asin (* k sn))))))))
  48. (define (pendulum-oscillating-angular-momentum alpha beta E)
  49. (let ((k (sqrt (/ (+ E beta) (* 2. beta))))
  50. (omega-0 (sqrt (/ beta alpha))))
  51. (lambda (time)
  52. (Jacobi-elliptic-functions
  53. (* omega-0 time)
  54. k
  55. (lambda (sn cn dn)
  56. (* 2. alpha omega-0 k cn))))))
  57. #|
  58. freq = (/ (* pi omega0) (* 2 (first-elliptic-integral k)))
  59. period = 4 K / omega0
  60. omega0 period = 4 K the period of sn
  61. |#
  62. (define (pendulum-oscillating-action alpha beta E)
  63. (let ((k^2 (/ (+ beta E) (* 2. beta))))
  64. (if (= k^2 1.)
  65. (* (/ 8. pi) (sqrt (* beta alpha)))
  66. (elliptic-integrals
  67. (sqrt k^2)
  68. (lambda (Kk Ek)
  69. (* (/ 8. pi)
  70. (sqrt (* beta alpha))
  71. (- Ek (* (- 1. k^2) Kk))))))))
  72. (define* ((pendulum-oscillating-action-to-E alpha beta) action)
  73. (let ((f (/ action (* (/ 8. pi) (sqrt (* beta alpha))))))
  74. (let ((k (pendulum-inverse-f f)))
  75. (* beta (- (* 2. (square k)) 1.)))))
  76. ;;; action angle -pi to pi
  77. (define (pendulum-oscillating-phase alpha beta)
  78. (let ((omega-0 (sqrt (/ beta alpha))))
  79. (lambda (state)
  80. (let ((theta (state->q state))
  81. (ptheta (state->p state)))
  82. (let ((E ((Hpendulum alpha beta) state)))
  83. (if (> E (- beta))
  84. (let ((k (sqrt (/ (+ E beta) (* 2. beta))))
  85. (period (/ 2pi (pendulum-frequency alpha beta E))))
  86. (let* ((sin-phi (/ (sin (/ theta 2.)) k))
  87. (dt0 (/ (elliptic-integral-F (asin sin-phi) k) omega-0)))
  88. (let ((dt (if (< ptheta 0) (- (/ period 2.) dt0) dt0)))
  89. ((principal-value pi) (* 2pi (/ dt period))))))
  90. (error "at the fixed point the phase is undefined")))))))
  91. ;;; time from theta=0 to state
  92. (define* ((pendulum-oscillating-dt alpha beta) state)
  93. (let ((E ((Hpendulum alpha beta) state))
  94. (phase ((pendulum-oscillating-phase alpha beta) state)))
  95. (let ((period (/ 2pi (pendulum-frequency alpha beta E))))
  96. (* phase (/ period 2pi)))))
  97. (define* ((pendulum-oscillating-aa-state-to-state alpha beta) aa-state)
  98. (let ((angle (state->q aa-state))
  99. (action (state->p aa-state)))
  100. (let* ((E ((pendulum-oscillating-action-to-E alpha beta) action))
  101. (period (/ 2pi (pendulum-frequency alpha beta E))))
  102. (let ((dt (* (/ period 2pi) angle)))
  103. (->H-state (state->t aa-state)
  104. ((pendulum-oscillating-angle alpha beta E) dt)
  105. ((pendulum-oscillating-angular-momentum alpha beta E) dt))))))
  106. #|
  107. (define ((pendulum-oscillating-action-to-E alpha beta) action)
  108. (bisect (lambda (E) (- (pendulum-oscillating-action alpha beta E) action))
  109. (- beta)
  110. beta
  111. (* *action-to-E-bugger-factor* *machine-epsilon*)))
  112. (define *action-to-E-bugger-factor* 1000.)
  113. |#
  114. (define* ((pendulum-oscillating-state-to-aa-state alpha beta) state)
  115. (let ((E ((Hpendulum alpha beta) state)))
  116. (let ((action ((pendulum-oscillating-action alpha beta E) state))
  117. (angle ((pendulum-oscillating-phase alpha beta) state)))
  118. (->H-state (state->t state) angle action))))
  119. ;;;----------------------------------------------------------------
  120. ;;; ciculating case
  121. (define (pendulum-circulating-frequency alpha beta E)
  122. (let ((k (sqrt (/ (* 2. beta) (+ E beta))))
  123. (omegaR (sqrt (abs (/ (+ E beta) (* 2. alpha))))))
  124. (/ (* pi omegaR) (first-elliptic-integral k))))
  125. (define (pendulum-circulating-angle alpha beta E)
  126. (let ((k (sqrt (/ (* 2. beta) (+ E beta))))
  127. (omega-R (sqrt (abs (/ (+ E beta) (* 2. alpha)))))
  128. (period (/ 2pi (pendulum-frequency alpha beta E))))
  129. (lambda (time)
  130. (Jacobi-elliptic-functions
  131. (* omega-R ((principal-range period) time))
  132. k
  133. (lambda (sn cn dn)
  134. (* 2. (asin sn)))))))
  135. (define (pendulum-circulating-angular-momentum alpha beta E)
  136. (let ((k (sqrt (/ (* 2. beta) (+ E beta))))
  137. (omega-R (sqrt (abs (/ (+ E beta) (* 2. alpha)))))
  138. (period (/ 2pi (pendulum-frequency alpha beta E))))
  139. (lambda (time)
  140. (Jacobi-elliptic-functions
  141. (* omega-R ((principal-range period) time))
  142. k
  143. (lambda (sn cn dn)
  144. (* 2. alpha omega-R dn))))))
  145. #|
  146. ;;; Defined in kernel/numeric.scm
  147. (define ((principal-range period) time)
  148. (let ((t (- time (* period (floor (/ time period))))))
  149. (if (< t (/ period 2.))
  150. t
  151. (- t period))))
  152. |#
  153. #|
  154. omega = (/ (* pi omegaR) (first-elliptic-integral k)))))
  155. period = 2pi / omega = 2 K / omegaR
  156. so period*omegaR = 2 K but the period of sn is 4 K
  157. so if period*omegaR is in the range 2K to 4K the
  158. program would not work without the principal-range call
  159. |#
  160. (define (pendulum-circulating-action alpha beta E)
  161. (let* ((k (sqrt (/ (* 2. beta) (+ beta E))))
  162. (Ek (second-elliptic-integral k)))
  163. (* (/ 4. pi)
  164. (sqrt (* beta alpha))
  165. (/ Ek k))))
  166. (define* ((pendulum-circulating-action-to-E alpha beta) action)
  167. (let ((g (/ action (* (/ 4. pi) (sqrt (* beta alpha))))))
  168. (let ((k (pendulum-inverse-g g)))
  169. (let ((k^2 (square k)))
  170. (/ (* beta (- 2. k^2)) k^2)))))
  171. (define* ((pendulum-circulating-phase alpha beta) state)
  172. (let ((theta (state->q state))
  173. (ptheta (state->p state)))
  174. (let ((E ((Hpendulum alpha beta) state)))
  175. (let ((k (sqrt (/ (* 2. beta) (+ E beta))))
  176. (omega-R (sqrt (abs (/ (+ E beta) (* 2. alpha)))))
  177. (period (/ 2pi (pendulum-frequency alpha beta E))))
  178. (let ((dt (/ (elliptic-integral-F
  179. (/ ((principal-value pi) theta) 2.) k)
  180. omega-R)))
  181. ((principal-value pi) (* 2pi (/ dt period))))))))
  182. ;;; time from theta=0 to state
  183. (define* ((pendulum-circulating-dt alpha beta) state)
  184. (let ((E ((Hpendulum alpha beta) state))
  185. (phase ((pendulum-circulating-phase alpha beta) state)))
  186. (let ((period (/ 2pi (pendulum-frequency alpha beta E))))
  187. (* phase (/ period 2pi)))))
  188. (define* ((pendulum-circulating-aa-state-to-state alpha beta) aa-state)
  189. (let ((angle (state->q aa-state))
  190. (action (state->p aa-state)))
  191. (let* ((E ((pendulum-circulating-action-to-E alpha beta) action))
  192. (period (/ 2pi (pendulum-frequency alpha beta E))))
  193. (let ((dt (* (/ period 2pi) angle)))
  194. (->H-state (state->t aa-state)
  195. ((pendulum-circulating-angle alpha beta E) dt)
  196. ((pendulum-circulating-angular-momentum alpha beta E) dt))))))
  197. #|
  198. (define ((pendulum-circulating-action-to-E alpha beta) action)
  199. (bisect (lambda (E) (- (pendulum-circulating-action alpha beta E) action))
  200. beta
  201. (+ (/ (square action) (* 2. alpha)) beta)
  202. (* *action-to-E-bugger-factor* *machine-epsilon*)))
  203. |#
  204. (define* ((pendulum-circulating-state-to-aa-state alpha beta) state)
  205. (let ((E ((Hpendulum alpha beta) state)))
  206. (let ((action ((pendulum-circulating-action alpha beta E) state))
  207. (angle ((pendulum-circulating-phase alpha beta) state)))
  208. (->H-state (state->t state) angle action))))
  209. (define (pendulum-f k)
  210. (if (= k 1.)
  211. 1.
  212. (elliptic-integrals
  213. k
  214. (lambda (Kk Ek)
  215. (- Ek (* (- 1. (square k)) Kk))))))
  216. (define (pendulum-g k)
  217. (/ (second-elliptic-integral k) k))
  218. (define (pendulum-inverse-f fk)
  219. (let ((sfk (sqrt fk)))
  220. (bisect (lambda (k)
  221. (- (sqrt (pendulum-f k)) sfk))
  222. 0. 1. 1.e-10)))
  223. (define (pendulum-inverse-g gk)
  224. (let ((inv-gk (/ 1. gk)))
  225. (bisect (lambda (k)
  226. (if (= k 0.)
  227. (- inv-gk)
  228. (- (/ 1. (pendulum-g k)) inv-gk)))
  229. 0.0 1. 1.e-10)))
  230. ;;;----------------------------------------------------------------
  231. ;;; separatrix case
  232. (define* ((pendulum-separatrix-angle alpha beta) time)
  233. (let ((omega-0 (sqrt (abs (/ beta alpha)))))
  234. (* 2. (gudermannian (* omega-0 time)))))
  235. (define* ((pendulum-separatrix-angular-momentum alpha beta) time)
  236. (let ((theta ((pendulum-separatrix-angle alpha beta) time)))
  237. (sqrt (* 2. alpha beta (+ 1. (cos theta))))))
  238. (define (gudermannian x)
  239. (- (* 2. (atan (exp x))) pi/2))
  240. (define (inverse-gudermannian x)
  241. (log (tan (+ (/ x 2.) pi/4))))
  242. ;;; area of "eye"
  243. (define (pendulum-separatrix-action alpha beta)
  244. (* (/ 8. pi) (sqrt (* alpha beta))))
  245. ;;;----------------------------------------------------------------
  246. ;;; pendulum state advancer
  247. (define* ((pendulum-advance alpha beta) state)
  248. (lambda (time)
  249. (let ((E ((Hpendulum alpha beta) state)))
  250. (if (< E beta)
  251. (let ((dt ((pendulum-oscillating-dt alpha beta) state)))
  252. (let ((t (+ dt (- time (state->t state)))))
  253. (->H-state time
  254. ((pendulum-oscillating-angle alpha beta E) t)
  255. ((pendulum-oscillating-angular-momentum alpha beta E) t))))
  256. (if (> (state->p state) 0)
  257. (let ((dt ((pendulum-circulating-dt alpha beta) state)))
  258. (let ((t (+ dt (- time (state->t state)))))
  259. (->H-state time
  260. ((pendulum-circulating-angle alpha beta E) t)
  261. ((pendulum-circulating-angular-momentum alpha beta E) t))))
  262. (let ((dt ((pendulum-circulating-dt alpha beta)
  263. (->H-state (- (state->t state))
  264. (- (state->q state))
  265. (- (state->p state))))))
  266. (let ((t (+ dt (- time (state->t state)))))
  267. (->H-state
  268. time
  269. (- ((pendulum-circulating-angle alpha beta E) t))
  270. (- ((pendulum-circulating-angular-momentum alpha beta E) t)
  271. )))))))))
  272. (define* ((pendulum-integration alpha beta eps) state)
  273. (lambda (time)
  274. (let ((state2
  275. ((state-advancer pendulum-sysder alpha beta)
  276. state (- time (state->t state)) eps)))
  277. (->H-state (state->t state2)
  278. ((principal-value pi) (state->q state2))
  279. (state->p state2)))))
  280. ;;;----------------------------------------------------------------
  281. ;;; series solutions
  282. #|
  283. (define ((pendulum-oscillating-solution-series alpha beta E omega eps) time)
  284. (let ((k (sqrt (/ (+ E beta) (* 2. beta))))
  285. (omega-0 (sqrt (/ beta alpha))))
  286. (let ((Kp (first-elliptic-integral (sqrt (- 1. (square k))))))
  287. (define (term n)
  288. (let ((omega-n (* omega (- (* 2. n) 1.))))
  289. (/ (sin (* omega-n time))
  290. (* omega-n (cosh (/ (* omega-n Kp) omega-0))))))
  291. (* 4. omega (sum-series term eps)))))
  292. (define ((pendulum-circulating-solution-series alpha beta E omega eps) time)
  293. (let ((k (sqrt (/ (* 2. beta) (+ E beta))))
  294. (omega-R (sqrt (abs (/ (+ E beta) (* 2. alpha))))))
  295. (let ((Kp (first-elliptic-integral (sqrt (- 1. (square k))))))
  296. (define ((term time) n)
  297. (let ((omega-n (* omega n)))
  298. (/ (sin (* omega-n time))
  299. (* omega-n (cosh (/ (* omega-n Kp) omega-R))))))
  300. (+ (* omega time)
  301. (* 2. omega (sum-series (term time) eps))))))
  302. ;;; don't use this without thinking...
  303. (define (sum-series term eps)
  304. (let loop ((n 1) (sum 0.) (lastf 1.))
  305. (let ((f (term n)))
  306. (if (and (< (abs f) eps) (< (abs lastf) eps))
  307. sum
  308. (loop (fix:+ n 1) (+ sum f) f)))))
  309. ;;; purpose of checking last two is
  310. ;;; to prevent some premature terminations
  311. ;;; because a term is "accidently" zero
  312. |#
  313. #|
  314. (define (((pendulum-solution-series alpha beta) state) time)
  315. (let ((E ((Hpendulum alpha beta) state)))
  316. (let ((omega (pendulum-frequency alpha beta E))
  317. (beta (abs beta)))
  318. (if (< E beta)
  319. (let ((k (sqrt (/ (+ E beta) (* 2 beta))))
  320. (omega-0 (sqrt (abs (/ beta alpha)))))
  321. (let ((Kp (first-elliptic-integral (sqrt (- 1 (square k))))))
  322. (define (term n)
  323. (let ((omega-n (* omega (- (* 2 n) 1))))
  324. (/ (sin (* omega-n time))
  325. (* omega-n (cosh (/ (* omega-n Kp) omega-0))))))
  326. (* 4 omega (series:generate (lambda (i) (term (+ i 1)))))))
  327. (let ((k (sqrt (/ (* 2 beta) (+ E beta))))
  328. (omega-R (sqrt (abs (/ (+ E beta) (* 2 alpha))))))
  329. (let ((Kp (first-elliptic-integral (sqrt (- 1 (square k))))))
  330. (define ((term time) n)
  331. (let ((omega-n (* omega n)))
  332. (/ (sin (* omega-n time))
  333. (* omega-n (cosh (/ (* omega-n Kp) omega-R))))))
  334. (+ (* omega time)
  335. (* 2 omega (series:generate (lambda (i) ((term time) (+ i 1))))))))))))
  336. (series:print
  337. (((pendulum-solution-series 1. 9.8)
  338. (->H-state 0. 0. 4.9006733894348145)) 't)
  339. 10)
  340. (* 1.8349993630564594 (sin (* 2.5043962735932013 t)))
  341. (* .03821300344597103 (sin (* 7.513188820779604 t)))
  342. (* .00135312864251141 (sin (* 12.521981367966006 t)))
  343. (* 5.702944261999213e-5 (sin (* 17.53077391515241 t)))
  344. (* 2.617233223741749e-6 (sin (* 22.53956646233881 t)))
  345. (* 1.2635138738869227e-7 (sin (* 27.548359009525214 t)))
  346. (* 6.308369363000512e-9 (sin (* 32.55715155671162 t)))
  347. (* 3.225945107424557e-10 (sin (* 37.56594410389802 t)))
  348. (* 1.679527336266625e-11 (sin (* 42.57473665108442 t)))
  349. (* 8.866866369088442e-13 (sin (* 47.583529198270824 t)))
  350. ;Value: ...
  351. |#
  352. #|
  353. ;;; Check that the canonical transformation is area-preserving.
  354. (define (der-qq f state)
  355. ((richardson-derivative
  356. (lambda (q)
  357. (state->q
  358. (f (->H-state (state->t state) q (state->p state)))))
  359. 1.e-8
  360. .01)
  361. (state->q state)))
  362. (define (der-qp f state)
  363. ((richardson-derivative
  364. (lambda (p)
  365. (state->q
  366. (f (->H-state (state->t state) (state->q state) p))))
  367. 1.e-8
  368. .01)
  369. (state->p state)))
  370. (define (der-pq f state)
  371. ((richardson-derivative
  372. (lambda (q)
  373. (state->p
  374. (f (->H-state (state->t state) q (state->p state)))))
  375. 1.e-8
  376. .01)
  377. (state->q state)))
  378. (define (der-pp f state)
  379. ((richardson-derivative
  380. (lambda (p)
  381. (state->p
  382. (f (->H-state (state->t state) (state->q state) p))))
  383. 1.e-8
  384. .01)
  385. (state->p state)))
  386. ;;;----------------------------------------------------------------
  387. (let ((f (pendulum-circulating-aa-state-to-state 2.0 9.8))
  388. (g (pendulum-circulating-state-to-aa-state 2.0 9.8)))
  389. (let* ((state (->H-state 1. 1. 15.))
  390. (aa-state (g state)))
  391. (- (* (der-qq f aa-state) (der-pp f aa-state))
  392. (* (der-pq f aa-state) (der-qp f aa-state)))))
  393. ;Value: 1.0000000000003484
  394. (let ((f (pendulum-circulating-aa-state-to-state 2.0 9.8))
  395. (g (pendulum-circulating-state-to-aa-state 2.0 9.8)))
  396. (let* ((state (->H-state 1. 1. 15.))
  397. (aa-state (g state)))
  398. (- (* (der-qq g state) (der-pp g state))
  399. (* (der-pq g state) (der-qp g state)))))
  400. ;Value: .9999999999986688
  401. (let ((f (pendulum-oscillating-aa-state-to-state 2.0 9.8))
  402. (g (pendulum-oscillating-state-to-aa-state 2.0 9.8)))
  403. (let* ((state (->H-state 1. 1. 1.))
  404. (aa-state (g state)))
  405. (- (* (der-qq g state) (der-pp g state))
  406. (* (der-pq g state) (der-qp g state)))))
  407. ;Value: 1.000000000000521
  408. (let ((f (pendulum-oscillating-aa-state-to-state 2.0 9.8))
  409. (g (pendulum-oscillating-state-to-aa-state 2.0 9.8)))
  410. (let* ((state (->H-state 1. 1. 1.))
  411. (aa-state (g state)))
  412. (- (* (der-qq f aa-state) (der-pp f aa-state))
  413. (* (der-pq f aa-state) (der-qp f aa-state)))))
  414. ;Value: 1.000000000000406
  415. |#
  416. ;;;----------------------------------------------------------------
  417. (define (pendulum-frequency alpha beta E)
  418. (cond ((< E beta) (pendulum-oscillating-frequency alpha beta E))
  419. ((> E beta) (pendulum-circulating-frequency alpha beta E))
  420. (else
  421. 0.)))
  422. ;;;----------------------------------------------------------------
  423. ;;; global action angle coordinates for pendulum
  424. #|
  425. Oscillation region:
  426. -pi < phi < pi
  427. 0 < I < Isep
  428. Upper circulation region:
  429. -pi < phi < pi -> -pi/2 < phi' < pi/2 phi' = phi/2
  430. Isep < 2I Isep < I' I' = 2I
  431. Lower circulation region:
  432. ...
  433. |#
  434. (define* ((pendulum-state-to-global-aa-state alpha beta) state)
  435. (let ((E ((Hpendulum alpha beta) state)))
  436. (cond ((< E beta)
  437. ((pendulum-oscillating-state-to-aa-state alpha beta) state))
  438. ((and (> E beta) (> (state->p state) 0.))
  439. (let ((aa-state
  440. ((pendulum-circulating-state-to-aa-state alpha beta)
  441. state)))
  442. (->H-state (state->t state)
  443. (* 0.5 (state->q aa-state))
  444. (* 2.0 (state->p aa-state)))))
  445. ((and (> E beta) (< (state->p state) 0.))
  446. (let ((aa-state
  447. ((pendulum-circulating-state-to-aa-state alpha beta)
  448. state)))
  449. (->H-state (state->t state)
  450. ((principal-value pi)
  451. (- pi (* 0.5 (state->q aa-state))))
  452. (* 2.0 (state->p aa-state)))))
  453. ((= E beta)
  454. 'go-figure))))
  455. (define (pendulum-global-aa-state-to-state alpha beta)
  456. (let ((separatrix-action (pendulum-separatrix-action alpha beta)))
  457. (lambda (aa-state)
  458. (let ((angle (state->q aa-state))
  459. (action (state->p aa-state)))
  460. (cond ((< action separatrix-action)
  461. ((pendulum-oscillating-aa-state-to-state alpha beta) aa-state))
  462. ((> action separatrix-action)
  463. (if (and (< angle pi/2) (>= angle -pi/2))
  464. ((pendulum-circulating-aa-state-to-state alpha beta)
  465. (->H-state (state->t aa-state)
  466. (* 2. (state->q aa-state))
  467. (* 0.5 (state->p aa-state))))
  468. (let ((state
  469. ((pendulum-circulating-aa-state-to-state alpha beta)
  470. (->H-state (state->t aa-state)
  471. (* 2. (state->q aa-state))
  472. (* 0.5 (state->p aa-state))))))
  473. (->H-state (state->t state)
  474. (- (state->q state))
  475. (- (state->p state))))))
  476. ((= action separatrix-action)
  477. 'oh-well))))))