(15C) Haversine Navigation
|
11-05-2022, 11:13 PM
(This post was last modified: 11-06-2022 12:38 AM by Thomas Klemm.)
Post: #2
|
|||
|
|||
RE: (15C) Haversine Navigation
Motivation
Quote:Further solutions using the Spherical Law of Cosines fall apart when the distances are very small; short distances are most important to the vast majority of weekend sailors. Haversines resolved these issues. Haversine Function \( \begin{align} \operatorname {hav} (\theta) = \sin^2\left(\frac{\theta}{2}\right) = \frac{1 - \cos(\theta)}{2} \end{align} \) To avoid cancellation for small angles only the expression using the sine function should be used. Haversine Formula This formula can be used to calculate the great-circle distance between two points on a sphere: \( \begin{align} \operatorname {hav} \left(\theta \right) = \operatorname {hav} \left(\varphi_2 - \varphi_1\right) + \cos \left(\varphi_1\right)\cos \left(\varphi_2\right)\operatorname {hav} \left(\lambda_2-\lambda_1\right) \end{align} \) We can see that this avoids cancellation since only \( \operatorname {hav} \) is used for small differences. This happens if the two points are close which appears to be common in sailing. Compare this to the common formula: \( \begin{align} \cos(\theta) = \sin(\varphi_1) \sin(\varphi_2) + \cos(\varphi_1) \cos(\varphi_2) \cos(\lambda_{2}-\lambda_{1}) \end{align} \) Here cancellation happens if \(\cos(\Delta \lambda) \) is calculated for small \( \Delta \lambda = \lambda_2-\lambda_1 \). A similar problem arises if \( \theta \) is small. Program This is the original program: Code: 001 - 42,21,11 LBL A Assume that the coordinates are stored in the following registers: R5: \(φ_1\) R6: \(λ_1\) R8: \(φ_2\) R9: \(λ_2\) Let's start with the first few lines: Code: 002 - 45 5 RCL 5 The subroutine 2 calculates the complement of the angle: Code: 075 - 42,21, 2 LBL 2 But instead of calculating \((90^{\circ} - \varphi_1) - (90^{\circ} - \varphi_2)\) we could calculate \(\varphi_2 - \varphi_1\) directly. Quote:Complementing an angle takes less programming steps with the Sin aCos method than subtracting the angle from 90°. It is used again when calculating \(\cos \left(\varphi_1\right)\cos \left(\varphi_2\right)\): Code: 008 - 45 5 RCL 5 Then I had a look at the implementation of the haversine function: Code: 059 - 42,21, 0 LBL 0 Unfortunately the variant where cancellation for small angles occur was chosen. This invalidates the reason to use the haversine function. So I came up with this improved version: Code: 001 - 42,21,13 LBL C When I analysed the accuracy of the different approaches I noticed that with 10 digits it doesn't really matter. The coordinates are a couple of minutes appart which translates to a few nautical miles. Here's a typical example: \(φ_1\) = 41.6258 \(φ_2\) = 41.6683 \(λ_1\) = -71.9950 \(λ_2\) = -71.8650 However we'd notice the difference if either the differences were smaller or then we would use a calculator with fewer digits. Can we do better? Great Circle Navigation To match the drawing with our problem consider the following: \( \begin{align} A &= \lambda_2-\lambda_1 \, \text{(the angle at w)} \\ b &= 90^{\circ} - φ_1 \\ c &= 90^{\circ} - φ_2 \\ \end{align} \) Here \(a\) is the great-circle distance while \(C\) is the bearing. Spherical Law of Sines \( \begin{align} \frac{\sin A}{\sin a} = \frac{\sin B}{\sin b} = \frac{\sin C}{\sin c} \end{align} \) Spherical Law of Cosines \( \begin{align} \cos c=\cos a\cos b+\sin a\sin b\cos C \end{align} \) With a little rearrangement we get: \( \begin{align} \cos C&= \frac{\cos c-\cos a\cos b}{\sin a\sin b} \\ \end{align} \) This leads to: \( \begin{align} \sin C &= \sin c \frac{\sin A}{\sin a} \\ \\ \cos C &= \frac{\cos c - \cos a \cos b}{\sin a \sin b} \\ \\ \cos a & = \cos b \cos c + \sin b \sin c \cos A \\ \end{align} \) Or then by multiplying the first two equations by \( \sin a \): \( \begin{align} \sin a \sin C &= \sin c \sin A \\ \\ \sin a \cos C &= \frac{\cos c - \cos a \cos b}{ \sin b} \\ \\ \cos a & = \cos b \cos c + \sin b \sin c \cos A \\ \end{align} \) The goal is to use a rectangular to polar transformation to calculate \(a\) and \(C\) from: \( \begin{align} y &= \sin a \sin C \\ x &= \sin a \cos C \\ z &= \cos a \\ \end{align} \) For this we have to substitute \( \cos a \) in the 2nd equation: \( \begin{align} \sin a \cos C &= \frac{\cos c - \cos a \cos b}{ \sin b} \\ \\ &= \frac{\cos c - (\cos b \cos c + \sin b \sin c \cos A) \cos b}{ \sin b} \\ \\ &= \frac{\cos c - \cos^2 b \cos c - \sin b \cos b \sin c \cos A}{ \sin b} \\ \\ &= \frac{\sin^2 b \cos c - \sin b \cos b \sin c \cos A}{ \sin b} \\ \\ &= \sin b \cos c - \cos b \sin c \cos A \\ \end{align} \) We can define the following polar to rectangular transformation: \( \begin{align} u &= \sin c \cos A \\ v &= \sin c \sin A \\ w &= \cos c \end{align} \) Pluging all this into the equations leads to: \( \begin{align} y &= \sin a \sin C &= \sin c \sin A & = v \\ x &= \sin a \cos C &= \sin b \cos c - \cos b \sin c \cos A &= w \sin b - u \cos b \\ z &= \cos a &= \cos b \cos c + \sin b \sin c \cos A &= w \cos b + u \sin b \\ \end{align} \) This turns out to be a rotation by the angle \(φ_1 = 90^{\circ} - b\): \( \begin{align} \begin{bmatrix} x \\ z \\ \end{bmatrix} &= \begin{bmatrix} \sin b & - \cos b \\ \cos b & \sin b \\ \end{bmatrix} \cdot \begin{bmatrix} w \\ u \\ \end{bmatrix} \end{align} \) Programs Here's the program for the HP-42S: Code: 00 { 37-Byte Prgm } # This is the same program for the HP-15C: Code: 001 - 42,21,11 LBL A But similar programs work for other calculators as well as longs as they provide →REC, →POL, R↑ and R↓. Example 41.6258 ENTER -71.9950 ENTER 41.6683 ENTER -71.8650 GSB A 6.361974738 x<>y 66.32743461 Pros
Cons
Cancellation The cancellation is avoided since with →REC both cosine and sine are calculated. This allows to keep track of small angles. You can test this with a small angle, say 1.23456789e-5. Compare the result when calculating COS ACOS with 1 →REC →POL R↓. Thank you As always, I'm very grateful for your findings of these programs in the wild. References |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
(15C) Haversine Navigation - SlideRule - 11-12-2019, 12:55 AM
RE: (15C) Haversine Navigation - Thomas Klemm - 11-05-2022 11:13 PM
RE: (SHARP EL-5150) Haversine Navigation - C.Ret - 11-06-2022, 05:14 PM
RE: (15C) Haversine Navigation - Thomas Klemm - 11-06-2022, 11:46 PM
|
User(s) browsing this thread: 1 Guest(s)