;; cell/id: intro ;; # A simple ephemeris ;; ;; Positions for the four inner planets, computed by actually solving ;; **Kepler's equation** for an elliptical orbit — not the circular ;; shortcut. Each planet's orbit is an ellipse with the Sun at one ;; focus, not the center, and the planet sweeps it out at *varying* ;; speed (faster near the Sun, slower far away). This still ignores ;; orbital inclination — every planet is treated as if it shares ;; exactly the Sun's reference plane — which is a fine simplification ;; for the inner planets (their real inclinations are all under 4°) ;; but the one real shortcut left in "simple" Kepler here. ^{:cell/id "planets"} (def planets ;; Mean orbital elements at epoch J2000 (Standish 1992, rounded to ;; 6 figures) -- plenty precise for a solar-system sketch, nowhere ;; near precise enough to point a telescope with. ;; ;; a = semi-major axis (AU) ;; e = eccentricity ;; T = orbital period (days) ;; L0 = mean longitude at epoch (degrees) ;; peri = longitude of perihelion (degrees) -- where the ellipse's ;; near point to the Sun is oriented [{:name "Mercury" :a 0.387098 :e 0.205630 :T 87.969 :L0 252.251 :peri 77.456 :color "orange"} {:name "Venus" :a 0.723330 :e 0.006773 :T 224.701 :L0 181.980 :peri 131.533 :color "red"} {:name "Earth" :a 1.000000 :e 0.016710 :T 365.256 :L0 100.464 :peri 102.937 :color "blue"} {:name "Mars" :a 1.523710 :e 0.093412 :T 686.980 :L0 355.433 :peri 336.041 :color "pink"}]) ;; cell/id: kepler-explainer ;; ## Kepler's equation ;; ;; A planet's *mean anomaly* $M$ advances at constant speed, exactly ;; like the angle in a circular approximation: ;; ;; $$M(t) = M_0 + nt, \qquad n = \frac{2\pi}{T}$$ ;; ;; but $M$ isn't the planet's actual angle around the Sun — it's a ;; bookkeeping angle for an imaginary planet on a circumscribed circle, ;; moving at *uniform* speed. The planet's real angle on the ellipse, ;; the *eccentric anomaly* $E$, is related to $M$ by Kepler's equation: ;; ;; $$M = E - e\sin(E)$$ ;; ;; This has no closed-form solution for $E$ — it's solved numerically. ^{:cell/id "kepler-solve"} (defn solve-kepler "Eccentric anomaly E (radians) solving Kepler's equation M = E - e·sin(E) for mean anomaly M (radians) and eccentricity e, via Newton's method." [M e] (loop [E M i 0] (let [f (- E (* e (js/Math.sin E)) M) f' (- 1 (* e (js/Math.cos E))) E' (- E (/ f f'))] (if (or (< (js/Math.abs (- E' E)) 1e-9) (>= i 30)) E' (recur E' (inc i)))))) ;; cell/id: position-explainer ;; ## From eccentric anomaly to position ;; ;; Once $E$ is known, the planet's coordinates in its own orbital ;; plane (Sun at the origin, perihelion along the +x axis) are ;; ;; $$x' = a(\cos E - e), \qquad y' = a\sqrt{1 - e^2}\sin E$$ ;; ;; Rotating that by the orbit's longitude of perihelion `peri` places ;; it correctly relative to every other planet's orbit, since they all ;; share the one reference direction (0° longitude) rather than each ;; measuring from their own perihelion. ^{:cell/id "position-fn"} (defn position "Heliocentric [x y] position (AU) of a planet at time t (days since epoch J2000)." [{:keys [a e T L0 peri]} t] (let [deg->rad (/ js/Math.PI 180) n (/ (* 2 js/Math.PI) T) ;; mean motion (rad/day) M0 (* deg->rad (- L0 peri)) ;; mean anomaly at epoch M (+ M0 (* n t)) E (solve-kepler M e) x' (* a (- (js/Math.cos E) e)) y' (* a (js/Math.sqrt (- 1 (* e e))) (js/Math.sin E)) w (* deg->rad peri)] [(- (* x' (js/Math.cos w)) (* y' (js/Math.sin w))) (+ (* x' (js/Math.sin w)) (* y' (js/Math.cos w)))])) ;; cell/id: mercury-explainer ;; ## One orbit, traced out ;; ;; Sampling `position` across a full period and connecting the dots ;; traces the orbit's actual shape. Mercury has the most eccentric ;; orbit of the four (e ≈ 0.206), so its ellipse -- and the Sun sitting ;; noticeably off-center, at one focus -- is the easiest to see. ^{:cell/id "mercury-orbit"} (let [mercury (first (filter #(= "Mercury" (:name %)) planets))] (mapv #(position mercury (* % (/ (:T mercury) 200))) (range 201))) ;; cell/id: all-orbits-explainer ;; ## All four, together, at one instant ;; ;; Every planet's full orbit, drawn in one scene with the Sun at the ;; origin -- each ellipse sampled over its own period, since the ;; planets don't share one -- plus a dot marking where each planet ;; actually sits at a chosen time `t` (days since epoch J2000), and a ;; legend naming which color is which. `all-orbits` builds the plot by ;; hand (rather than returning a plain list of points) because it has ;; a lot more than one curve to draw: it's a Mafs scene assembled from ;; a `Polyline` per orbit, a `Point` per planet's current position, ;; and a `Text` label per legend entry. ^{:cell/id "epoch"} (def t ;; Days since J2000 to mark each planet's position at. Edit this and ;; re-evaluate (then re-evaluate `all-orbits-plot`) to move them -- ;; e.g. 365 for roughly a year later. 0) ^{:cell/id "all-orbits"} (defn all-orbits [planets t] (let [legend-x -1.9 legend-y0 1.9 legend-dy 0.2] (into ['mafs.core/Mafs {:height 460 :view-box {:x [-2 2] :y [-2 2]}} ['mafs.coordinates/Cartesian {}] ['mafs.core/Point {:x 0 :y 0 :color "yellow"}]] (concat ;; One traced-out ellipse per planet. (for [p planets] ['mafs.core/Polyline {:points (mapv #(position p (* % (/ (:T p) 200))) (range 201)) :color (:color p) :stroke-opacity 0.6}]) ;; Each planet's actual position at time `t`. (for [p planets] (let [[x y] (position p t)] ['mafs.core/Point {:x x :y y :color (:color p)}])) ;; A small color-keyed legend in the corner. (map-indexed (fn [i p] ['mafs.core/Text {:x legend-x :y (- legend-y0 (* i legend-dy)) :attach "e" :color (:color p) :size 14} (:name p)]) planets))))) ^{:cell/id "all-orbits-plot"} (all-orbits planets t) ;; cell/id: animate-explainer ;; ## Animated, over one Earth year ;; ;; A cell that evaluates to a *function* (rather than a plain value) ;; is treated as an animation: it's called over and over with a ;; looping fraction from 0 up to (but not including) 1, redrawing the ;; plot each time. Mapping that fraction onto 365.25 days sweeps every ;; planet all the way around once -- Mercury several times over, since ;; its year is so much shorter than Earth's. ^{:cell/id "animate"} (fn [frac] (all-orbits planets (* frac 365.25)))