sourcelibs/Trig/demos/catenary.xtl
1⍝!/usr/bin/env xetal
2⍝ Trig in use: a cable hung between two poles 100 m apart hangs in a
3⍝ catenary, y = k cosh(x / k), whose arc from the middle to x is
4⍝ k sinh(x / k) long. Given the cable's length, find k by bisection
5⍝ (the length grows as k shrinks), then the sag at the middle and
6⍝ the angle the cable leaves each pole at. Run with "just demo Trig catenary".
7"tr:" u̲se< "Trig"
8ᵘp̲laces ← { x → (f̲loat f̲loor 0.5 + x × 1e12) ÷ 1e12 } ⍝ 12 decimals: a logarithm's last digit differs between libms
9span ← 100.0 ⍝ meters between the poles
10cable ← 110.0 ⍝ meters of cable
11h ← span ÷ 2.0
12ᵘl̲ength ← { k → 2.0 × k × ᵗʳs̲inh h ÷ k } ⍝ the cable's length for k
13ᵘs̲tep ← { (lo, hi) → m ← 0.5 × lo + hi◆ cable < ᵘl̲ength m ? (m, hi)◆ (lo, m) }
14(lo, hi) ← 60 'ᵘs̲tep p̲ower (1.0, 1000.0) ⍝ 60 halvings: k to the last bit
15k ← 0.5 × lo + hi
16k ⍝ the catenary's parameter, meters
17ᵘl̲ength k ⍝ 110, the cable
18k × (ᵗʳc̲osh h ÷ k) − 1.0 ⍝ the sag at the middle, meters
19ᵗʳd̲egrees a̲tan ᵗʳs̲inh h ÷ k ⍝ the angle at each pole, degrees
20ᵘp̲laces k × ᵗʳa̲cosh 1.0 + 1.0 ÷ k ⍝ where the cable is 1 m above its lowest point