#+begin_src clojure :exports none (ns sicm.part1 (:refer-clojure :exclude [+ - * / = compare zero? ref partial time numerator denominator]) (:require [sicmutils.env :as e :refer :all :exclude [F->C]])) #+end_src * Tutorial Page 1/2 (0) https://kloimhardt.github.io/markus.html#a for general information (1) The most important thing is the left arrow "<" button. It brings you back to this tutorial. (2) Go to the next page with ">". (back with "<") (3) Scroll down. Do not miss some tutorial step at the bottom left or top right! (4) Execute this workspace with "Run". (back with "<") (5) You just saw some formula for polar coordinates. Have you memorized it? Let's find out! (6) Press "Get the Puzzle". (back with "<") (7) "Get the Puzzle" but come back via "Run" + "Clear output". (8) In the workspace above, you should see several blocks containing white slots. (9) Press "Run" and hold the "Color" button. (back via "Clear output") (10) On the top of the workspace, put "phi" into the slot of "sin". (11) Repeat step (9), the "(sin 'phi)" line should turn green. (12) Make the lines more and more green by solving the workspace puzzle (keep pressing "Color"!). The code below might help. When upon completion the ">" button turns green, press it immediately. #+begin_src clojure (->tex-equation (up (* 'R (cos 'phi)) (* 'R (sin 'phi)))) #+end_src #+begin_src clojure :exports none (defn walk [inner outer form] (cond (list? form) (outer (apply list (map inner form))) (seq? form) (outer (doall (map inner form))) (coll? form) (outer (into (empty form) (map inner form))) :else (outer form))) (defn postwalk [f form] (walk (partial postwalk f) f form)) (defn postwalk-replace [smap form] (postwalk (fn [x] (if (contains? smap x) (smap x) x)) form)) (defmacro let-scheme [b & e] (concat (list 'let (into [] (apply concat b))) e)) (defmacro define-1 [h & b] (let [body (postwalk-replace {'let 'let-scheme} b)] (if (coll? h) (if (coll? (first h)) (list 'defn (ffirst h) (into [] (rest (first h))) (concat (list 'fn (into [] (rest h))) body)) (concat (list 'defn (first h) (into [] (rest h))) body)) (concat (list 'def h) body)))) (defmacro define [h & b] (if (and (coll? h) (= (first h) 'tex-inspect)) (list 'do (concat ['define-1 (second h)] b) h) (concat ['define-1 h] b))) (defmacro lambda [h b] (list 'fn (into [] h) b)) (def show-expression simplify) (def velocities velocity) (def coordinates coordinate) (def vector-length count) (defn time [state] (first state)) #+end_src * Tutorial Page 2/2 (1) Notice that the drop down list above shows a second chapter named "Lyrics". (2) Press ">" a few times and note the generic text. Read it. (back to this page with "<") (3) From this page, after having pressed "Get the Puzzle" + "Color", again press ">". (4) Via the drop down, go to the chaper "Lyrics", press "<"+ "Color" + ">". #+begin_src clojure "Tutorial Page 2/2" #+end_src * 1 Lagrangian Mechanics ** 1.4 Computing Actions #+begin_src clojure (define ((L-free-particle mass) local) (let [(:tiles/vert (v (velocity local)))] (* 1/2 mass (dot-product v v)))) #+end_src #+begin_src clojure (define (test-path t) (up (+ (* 4 t) 7) (+ (* 3 t) 5) (+ (* 2 t) 1))) #+end_src #+begin_src clojure (define (Lagrangian-action L q t0 t1) (definite-integral (compose L (Gamma q)) t0 t1)) #+end_src #+begin_src clojure (Lagrangian-action (L-free-particle 3.0) test-path 0.0 10.0) #+end_src #+begin_src clojure (define ((make-eta nu t0 t1) t) (* (- t t0) (- t t1) (nu t))) #+end_src #+begin_src clojure (define ((varied-free-particle-action mass q nu t0 t1) eps) (let [(:tiles/vert (eta (make-eta nu t0 t1)))] (Lagrangian-action (L-free-particle mass) (+ q (* eps eta)) t0 t1))) #+end_src #+begin_src clojure ((varied-free-particle-action 3.0 test-path (up sin cos square) 0.0 10.0) 0.001) #+end_src #+begin_src clojure (minimize (varied-free-particle-action 3.0 test-path (up sin cos square) 0.0 10.0) -2.0 1.0) #+end_src #+begin_src clojure (define ((parametric-path-action Lagrangian t0 q0 t1 q1) qs) (let (:tiles/vert [(path (make-path t0 q0 t1 q1 qs))]) (Lagrangian-action Lagrangian path t0 t1))) #+end_src #+begin_src clojure (define (find-path Lagrangian t0 q0 t1 q1 n) (let [(:tiles/vert (initial-qs (linear-interpolants q0 q1 n))) (:tiles/vert (minimizing-qs (multidimensional-minimize (parametric-path-action Lagrangian t0 q0 t1 q1) initial-qs)))] (make-path t0 q0 t1 q1 minimizing-qs))) #+end_src #+begin_src clojure (define ((L-harmonic m k) local) (let [(:tiles/vert (q (coordinate local))) (:tiles/vert (v (velocity local)))] (- (* 1/2 m (square v)) (* 1/2 k (square q))))) #+end_src #+begin_src clojure (define harmonic-path (find-path (L-harmonic 1.0 1.0) 0.0 1.0 (* 1/2 pi) 0.0 3)) (:tiles/keep (- (cos 0.8) (harmonic-path 0.8))) #+end_src * 1.5 The Euler–Lagrange Equations ** 1.5.2 Computing Lagrange's Equations #+begin_src clojure (define ((Lagrange-equations Lagrangian) q) (- (D (compose ((partial 2) Lagrangian) (Gamma q))) (compose ((partial 1) Lagrangian) (Gamma q)))) #+end_src #+begin_src clojure (define (general-test-path t) (up (+ (* 'a t) 'a0) (+ (* 'b t) 'b0) (+ (* 'c t) 'c0))) #+end_src #+begin_src clojure (((Lagrange-equations (L-free-particle 'm)) general-test-path) 't) #+end_src #+begin_src clojure (show-expression (((Lagrange-equations (L-free-particle 'm)) (literal-function 'x)) 't)) #+end_src #+begin_src clojure (define (proposed-solution t) (* 'A (cos (+ (* 'omega t) 'phi)))) #+end_src #+begin_src clojure (show-expression (((Lagrange-equations (L-harmonic 'm 'k)) proposed-solution) 't)) #+end_src ** Exercise 1.11: Kepler's third law Show that a planet in circular orbit satisfies Kepler's third law $n^2a^3=G(M_1 + m_2)$ , where n is the angular frequency of the orbit and a is the distance between sun and planet. (Hint: use the reduced mass to construct the Lagrangian) #+begin_src clojure (define ((L-Kepler-central-polar m V) local) (let [(:tiles/vert (q (coordinate local))) (:tiles/vert (qdot (velocity local)))] (let [(:tiles/vert (r (ref q 0))) (:tiles/vert (phi (ref q 1))) (:tiles/vert (rdot (ref qdot 0))) (:tiles/vert (phidot (ref qdot 1)))] (- (* 1/2 m (+ (square rdot) (square (* r phidot))) ) (V r))))) #+end_src #+begin_src clojure (define ((gravitational-energy G m1 m2) r) (- (/ (* G m1 m2) r))) #+end_src #+begin_src clojure (define (circle t) (up 'a (* 'n t))) #+end_src #+begin_src clojure (define lagrangian-reduced (L-Kepler-central-polar (/ (* 'M_1 'm_2) (+ 'M_1 'm_2)) (gravitational-energy 'G 'M_1 'm_2))) #+end_src #+begin_src clojure (((Lagrange-equations lagrangian-reduced) circle) 't) #+end_src ** 1.6 How to find Lagrangians #+begin_src clojure (define ((L-uniform-acceleration m g) local) (let [(:tiles/vert (q (coordinate local))) (:tiles/vert (v (velocity local)))] (let [(:tiles/vert (y (ref q 1)))] (- (* 1/2 m (square v)) (* m g y))))) #+end_src #+begin_src clojure (show-expression (((Lagrange-equations (L-uniform-acceleration 'm 'g)) (up (literal-function 'x) (literal-function 'y))) 't)) #+end_src #+begin_src clojure (define ((L-central-rectangular m U) local) (let [(:tiles/vert (q (coordinate local))) (:tiles/vert (v (velocity local)))] (- (* 1/2 m (square v)) (U (sqrt (square q)))))) #+end_src #+begin_src clojure (((Lagrange-equations (L-central-rectangular 'm (literal-function 'U))) (up (literal-function 'x) (literal-function 'y))) 't) #+end_src #+begin_src clojure (show-expression (((Lagrange-equations (L-Kepler-central-polar 'm (literal-function 'U))) (up (literal-function 'r) (literal-function 'phi))) 't)) #+end_src ** 1.6.1 Coordinate Transformations #+begin_src clojure (define ((F->C F) local) (up (time local) (F local) (+ (((partial 0) F) local) (* (((partial 1) F) local) (velocity local))))) #+end_src #+begin_src clojure (define (p->r local) (let [(:tiles/vert (polar-tuple (coordinate local)))] (let [(:tiles/vert (r (ref polar-tuple 0))) (:tiles/vert (phi (ref polar-tuple 1)))] (let [(:tiles/vert (x (* r (cos phi)))) (:tiles/vert (y (* r (sin phi))))] (up x y))))) #+end_src #+begin_src clojure (show-expression (velocity ((F->C p->r) (up 't (up 'r 'phi) (up 'rdot 'phidot))))) #+end_src #+begin_src clojure (define (L-central-polar m U) (compose (L-central-rectangular m U) (F->C p->r))) #+end_src #+begin_src clojure (show-expression ((L-central-polar 'm (literal-function 'U)) (up 't (up 'r 'phi) (up 'rdot 'phidot)))) #+end_src Coriolis and centrifugal forces #+begin_src clojure (define ((L-free-rectangular m) local) (let [(:tiles/vert (vx (ref (velocities local) 0))) (:tiles/vert (vy (ref (velocities local) 1)))] (* 1/2 m (+ (square vx) (square vy))))) #+end_src #+begin_src clojure (define (L-free-polar m) (compose (L-free-rectangular m) (F->C p->r))) #+end_src #+begin_src clojure (define ((F Omega) local) (let [(:tiles/vert (t (time local))) (:tiles/vert (r (ref (coordinates local) 0))) (:tiles/vert (theta (ref (coordinates local) 1)))] (up r (+ theta (* Omega t))))) #+end_src #+begin_src clojure (define (L-rotating-polar m Omega) (compose (L-free-polar m) (F->C (F Omega)))) #+end_src #+begin_src clojure (define (L-rotating-rectangular m Omega) (compose (L-rotating-polar m Omega) (F->C r->p))) #+end_src
r->p added
x_r, y_r: underscore added. Calculation takes a few seconds,
add a blank at the and to start
definitions x_r y_r added
#+begin_src clojure (define x_r (literal-function 'x_r)) #+end_src #+begin_src clojure (define y_r (literal-function 'y_r)) #+end_src #+begin_src clojure (down (+ (* -1 (expt 'Omega 2) 'm (x_r 't)) (* -2 'Omega 'm ((D y_r) 't)) (* 'm (((expt D 2) x_r) 't))) (+ (* -1 (expt 'Omega 2) 'm (y_r 't)) (* 2 'Omega 'm ((D x_r) 't)) (* 'm (((expt D 2) y_r) 't)))) #+end_srcSee here for a presentation of the Driven Pendulum using visual programming
#+begin_src clojure (define ((T-pend m l g ys) local) (let [(t (time local)) (theta (coordinate local)) (thetadot (velocity local))] (let [(vys (D ys))] (* 1/2 m (+ (square (* l thetadot)) (square (vys t)) (* 2 l (vys t) thetadot (sin theta))))))) #+end_src #+begin_src clojure (define ((V-pend m l g ys) local) (let [(t (time local)) (theta (coordinate local))] (* m g (- (ys t) (* l (cos theta)))))) #+end_src Because used later, rename L-pend to L-pendulum
#+begin_src clojure
(define L-pendulum (- T-pend V-pend))
#+end_src
#+begin_src clojure
(show-expression
(((Lagrange-equations
(L-pendulum 'm 'l 'g (literal-function 'y_s)))
(literal-function 'theta))
't))
#+end_src
Change the second define into a let
#+begin_src clojure (define (L3-central m Vr) (let (:tiles/vert [(:tiles/vert (Vs (lambda [state] (let (:tiles/vert [(:tiles/vert (r (ref (coordinate state) 0)))]) (Vr r)))))]) (- (T3-spherical m) Vs))) #+end_src #+begin_src clojure (show-expression (((partial 1) (L3-central 'm (literal-function 'V))) (up 't (up 'r 'theta 'phi) (up 'rdot 'thetadot 'phidot)))) #+end_src #+begin_src clojure (show-expression (((partial 2) (L3-central 'm (literal-function 'V))) (up 't (up 'r 'theta 'phi) (up 'rdot 'thetadot 'phidot)))) #+end_src #+begin_src clojure (define ((ang-mom-z m) rectangular-state) (let [(xyz (coordinate rectangular-state)) (v (velocity rectangular-state))] (ref (cross-product xyz (* m v)) 2))) #+end_src #+begin_src clojure (define (s->r spherical-state) (let [(q (coordinate spherical-state))] (let [(r (ref q 0)) (theta (ref q 1)) (phi (ref q 2))] (let [(x (* r (sin theta) (cos phi))) (y (* r (sin theta) (sin phi))) (z (* r (cos theta)))] (up x y z))))) #+end_src #+begin_src clojure (show-expression ((compose (ang-mom-z 'm) (F->C s->r)) (up 't (up 'r 'theta 'phi) (up 'rdot 'thetadot 'phidot)))) #+end_src #+begin_src clojure (show-expression ((Lagrangian->energy (L3-central 'm (literal-function 'V))) (up 't (up 'r 'theta 'phi) (up 'rdot 'thetadot 'phidot)))) #+end_src
A let within a variable definition is not allowed
in our little Scheme compiler,
... so we split in two expressions.
Also we define D-F-tilde as (D F-tilde)