2

I'm trying to compute the distance in kilometres of the Great Circle using Haversine formula in Java as shown below

/* Program to demonstrate the Floating-point numbers and the Math library.
 * The great-circle distance is the length of the shortest path between two points (x1,y1) and (x2,y2) on the surface of a sphere, where the path is constrained to be along the surface.*/
public class GreatCircle 
{
    public static void main(String[] args) 
    {
        double r = 6371.0; // Equatorial radius of the Earth
        double x1 = Math.toRadians(Double.parseDouble(args[0]));
        double y1 = Math.toRadians(Double.parseDouble(args[1]));
        double x2 = Math.toRadians(Double.parseDouble(args[2]));
        double y2 = Math.toRadians(Double.parseDouble(args[3]));

        // Compute using Haversine formula
        double distance = 2 * r * Math.asin(Math.sqrt(Math.pow(Math.sin((x2 - x1) / 2),2 + Math.cos(x2) * Math.pow(Math.sin((y2 - y1) / 2),2)));

        // Output the distance
        System.out.println(distance + " kilometers ");
    }
}

I'm running with input java GreatCircle 60.0 15.0 120.0 105.0. The expected output is 4604.53989281927 kilometers, But I get 13406.238676180266 kilometers. Could someone please point out where am I going wrong?

Alok Y
  • 93
  • 9

3 Answers3

1

The formula was implemented incorrectly. It worked after making the following corrections. In the formula, we are taking the arc sin of the entire expression.


        // Compute using Haversine formula
        double distance = 2 * r * Math.asin(Math.sqrt(Math.pow(Math.sin((x2 - x1) / 2),2 + Math.cos(x2) * Math.pow(Math.sin((y2 - y1) / 2),2)));

        // Output the distance
        System.out.println(distance + " kilometers ");
    }
}
Alok Y
  • 93
  • 9
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 24 '21 at 10:02
  • Perhaps I need to rework on the implementation of the formula. I might get back to this sometime in the future. – Alok Y Dec 27 '21 at 10:24
0

You have forgotten to calculate Math.cos(x1) * Math.cos(x2), that's why you get a different result.

// Compute using Haversine formula<br>
double distance = 2 * r * Math.asin(Math.sqrt((Math.pow(Math.sin((x2 - x1) / 2),2) + Math.cos(x1) * Math.cos(x2) * Math.pow(Math.sin((y2 - y1) / 2),2))));```

0
double distance = 2 * r * Math.asin(Math.sqrt(Math.pow(Math.sin((x2 - x1) / 2),2)
            + Math.cos(x2) * Math.cos(x1) * Math.pow(Math.sin((y2 - y1) / 2),2)));
Dada
  • 6,313
  • 7
  • 24
  • 43
  • 3
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 30 '22 at 08:41