How to resolve the algorithm Haversine formula step by step in the AWK programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Haversine formula step by step in the AWK programming language

Table of Contents

Problem Statement

The haversine formula is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes. It is a special case of a more general formula in spherical trigonometry, the law of haversines, relating the sides and angles of spherical "triangles".

Implement a great-circle distance function, or use a library function, to show the great-circle distance between:

Most of the examples below adopted Kaimbridge's recommended value of 6372.8 km for the earth radius. However, the derivation of this ellipsoidal quadratic mean radius is wrong (the averaging over azimuth is biased). When applying these examples in real applications, it is better to use the mean earth radius, 6371 km. This value is recommended by the International Union of Geodesy and Geophysics and it minimizes the RMS relative error between the great circle and geodesic distance.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Haversine formula step by step in the AWK programming language

Source code in the awk programming language

# syntax: GAWK -f HAVERSINE_FORMULA.AWK
# converted from Python
BEGIN {
    distance(36.12,-86.67,33.94,-118.40) # BNA to LAX
    exit(0)
}
function distance(lat1,lon1,lat2,lon2,  a,c,dlat,dlon) {
    dlat = radians(lat2-lat1)
    dlon = radians(lon2-lon1)
    lat1 = radians(lat1)
    lat2 = radians(lat2)
    a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
    c = 2 * atan2(sqrt(a),sqrt(1-a))
    printf("distance: %.4f km\n",6372.8 * c)
}
function radians(degree) { # degrees to radians
    return degree * (3.1415926 / 180.)
}


  

You may also check:How to resolve the algorithm Create an object at a given address step by step in the Ada programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Quackery programming language
You may also check:How to resolve the algorithm Balanced brackets step by step in the Batch File programming language
You may also check:How to resolve the algorithm Benford's law step by step in the BCPL programming language
You may also check:How to resolve the algorithm Scope/Function names and labels step by step in the PL/I programming language