[Swlug] Math calculations
Rhys Sage
rhys_sage at yahoo.com
Wed Jul 20 12:25:59 UTC 2022
I looked up the Haversine formula and found it nicely explained.
Law of Haversine:
To derive law of Haversine one needs to start the calculation with spherical law of cosine i.e cos a = cos b * cos c + sin b * sin c * cos A
One can derive Haversine formula to calculate distance between two as:
a = sin²(ΔlatDifference/2) + cos(lat1).cos(lt2).sin²(ΔlonDifference/2)
c = 2.atan2(√a, √(1−a))
d = R.c
where,
ΔlatDifference = lat1 – lat2 (difference of latitude)
ΔlonDifference = lon1 – lon2 (difference of longitude)
R is radius of earth i.e 6371 KM or 3961 miles
and d is the distance computed between two points.
The extra libraries would be interesting. I'm not 100% sure that they're available for the Pi Pico and its limited memory. However, I just said to myself, it really can't be that hard to cave-man it.
I came up with this:
#a = sin²(ΔlatDifference/2) + cos(lat1).cos(lt2).sin²(ΔlonDifference/2)
A = math.sin((LocationOneV-LocationTwoV)/2) + ((math.cos(LocationOneV)*(math.cos(LocationTwoV))*math.sin((LocationOneH-LocationTwoH)/2)))
print (A)
#C = 2*(math.atan2((math.sqrt(A)), (math.sqrt(1-A))))
#d = R.c
Distance = RadiusOfEarth*C
print("New distance in miles ", Distance)
However, it must be Friday the thirteenth today. I was getting an error on the big calculation for C which I've traced to math.sqrt(1-A).
I get the unlikely ValueError: math domain error
I resolved that by putting an abs in thus:
C = 2*(math.atan2((math.sqrt(A)), (math.sqrt(abs(1-A)))))
That in turn made the result come up that the distance between The Tower of London and the Arc De Triomphe is 11309.79673477676 miles. I think that would represent quite a detour!
Using my flat-earth calculation I estimated the distance between the two as being 217 miles which seems much more realistic though the actual distance is allegedly 211 miles.
Clearly the abs does not belong where it was put. This is puzzling but not insoluble. Right now I have to rush off on a mission so I'll post this.
I'm thinking that the problem could be that I am representing East and North as positive values with West and South as negative values.
Rhys Sage
More information about the Swlug
mailing list