|
7 | 7 | ;; the [SICMUtils](https://github.com/sicmutils/sicmutils) Clojure library and
|
8 | 8 | ;; the Clerk rendering environment.
|
9 | 9 |
|
| 10 | +#_{:clj-kondo/ignore [:refer-all]} |
10 | 11 | (ns sicmutils
|
11 | 12 | (:refer-clojure
|
12 | 13 | :exclude [+ - * / partial ref zero? numerator denominator compare = run!])
|
13 | 14 | (:require [nextjournal.clerk :as clerk]
|
14 |
| - [sicmutils.env :as e :refer :all])) |
| 15 | + [sicmutils.env :as e :refer :all] |
| 16 | + [sicmutils.expression.render :as xr])) |
15 | 17 |
|
16 | 18 | ;; ## Lagrangian
|
17 | 19 | ;;
|
|
20 | 22 | ;; Lagrangian with the familiar form of `T - V`.
|
21 | 23 |
|
22 | 24 | (defn angles->rect [l1 l2]
|
23 |
| - (fn [[t [theta1 theta2]]] |
| 25 | + (fn [[_ [theta1 theta2]]] |
24 | 26 | (let [x1 (* l1 (sin theta1))
|
25 |
| - y1 (- (* l1 (cos theta1))) |
| 27 | + y1 (- (* l1 (cos theta1))) |
26 | 28 | x2 (+ x1 (* l2 (sin (+ theta1 theta2))))
|
27 |
| - y2 (- y1 (* l2 (cos (+ theta1 theta2))))] |
28 |
| - (up x1 y1 x2 y2)))) |
| 29 | + y2 (- y1 (* l2 (cos (+ theta1 theta2))))] |
| 30 | + (up x1 y1 x2 y2)))) |
29 | 31 |
|
30 | 32 | ;; `T` describes the sum of the kinetic energy of two particles in rectangular
|
31 | 33 | ;; coordinates.
|
32 | 34 |
|
33 | 35 | (defn T [m1 m2]
|
34 | 36 | (fn [[_ _ [xdot1 ydot1 xdot2 ydot2]]]
|
35 |
| - (+ (* (/ 1 2) m1 (+ (square xdot1) |
36 |
| - (square ydot1))) |
37 |
| - (* (/ 1 2) m2 (+ (square xdot2) |
38 |
| - (square ydot2)))))) |
| 37 | + (+ (* 1/2 m1 (+ (square xdot1) |
| 38 | + (square ydot1))) |
| 39 | + (* 1/2 m2 (+ (square xdot2) |
| 40 | + (square ydot2)))))) |
39 | 41 |
|
40 | 42 |
|
41 | 43 | ;; `V` describes a uniform gravitational potential with coefficient `g`, acting
|
|
85 | 87 | ;; And here are the equations of motion for the system:
|
86 | 88 |
|
87 | 89 | (let [L (L-double-pendulum 'm_1 'm_2 'l_1 'l_2 'g)]
|
88 |
| - (binding [sicmutils.expression.render/*TeX-vertical-down-tuples* true] |
| 90 | + (binding [xr/*TeX-vertical-down-tuples* true] |
89 | 91 | (render-eq
|
90 | 92 | (((Lagrange-equations L)
|
91 | 93 | (up (literal-function 'theta_1)
|
|
95 | 97 | ;; What do these mean?
|
96 | 98 | ;;
|
97 | 99 | ;; - the system has two degrees of freedom: $\theta_1$ and $\theta_2$.
|
98 |
| - |
99 | 100 | ;; - at any point `t` in time, the two equations above, full of first and second
|
100 | 101 | ;; - order derivatives of the position functions, will stay true
|
101 | 102 | ;; - the system can use these equations to simulate the system, one tick at a time.
|
|
162 | 163 | horizon
|
163 | 164 | {:compile? true
|
164 | 165 | :epsilon 1.0e-13
|
165 |
| - :observe (fn [t state] |
| 166 | + :observe (fn [_ state] |
166 | 167 | (swap!
|
167 | 168 | collector conj! state))})
|
168 | 169 | (persistent! @collector))))
|
|
221 | 222 |
|
222 | 223 | ;; Here is `transform-data`:
|
223 | 224 |
|
| 225 | +#_{:clj-kondo/ignore [:unresolved-symbol]} |
224 | 226 | (defn transform-data [xs]
|
225 | 227 | (let [energy-fn (L-energy m1 m2 l1 l2 g)
|
226 | 228 | monitor (energy-monitor energy-fn (first xs))
|
|
453 | 455 |
|
454 | 456 | (defn divergence-monitor []
|
455 | 457 | (let [pv (principal-value Math/PI)]
|
456 |
| - (fn [[t [thetas1 thetas2]]] |
| 458 | + (fn [[_ [thetas1 thetas2]]] |
457 | 459 | (safe-log
|
458 | 460 | (Math/abs
|
459 | 461 | (pv
|
460 |
| - (- (nth thetas1 1) |
461 |
| - (nth thetas2 1)))))))) |
| 462 | + (- (nth thetas1 1) |
| 463 | + (nth thetas2 1)))))))) |
462 | 464 |
|
463 | 465 | (defn run-double-double!
|
464 | 466 | "Two different initializations, slightly kicked"
|
465 | 467 | [step horizon initial-q1]
|
466 | 468 | (let [initial-q2 (+ initial-q1 (up 0.0 1e-10))
|
467 | 469 | initial-qdot (up 0.0 0.0)
|
468 | 470 | initial-state (up 0.0
|
469 |
| - (up initial-q1 initial-q2) |
470 |
| - (up initial-qdot initial-qdot)) |
| 471 | + (up initial-q1 initial-q2) |
| 472 | + (up initial-qdot initial-qdot)) |
471 | 473 | collector (atom (transient []))]
|
472 | 474 | ((evolve dd-state-derivative m1 m2 l1 l2 g)
|
473 | 475 | initial-state
|
474 | 476 | step
|
475 | 477 | horizon
|
476 | 478 | {:compile? true
|
477 | 479 | :epsilon 1.0e-13 ; = (max-norm 1.e-13)
|
478 |
| - :observe (fn [t state] |
| 480 | + :observe (fn [_ state] |
479 | 481 | (swap! collector conj! state))})
|
480 | 482 | (persistent! @collector)))
|
481 | 483 |
|
|
0 commit comments