function dist = sphere_distance2 ( lat1, lon1, lat2, lon2, r )

%*****************************************************************************80
%
%% sphere_distance2() computes great circle distances on a sphere.
%
%  Discussion:
%
%    This computation is written in terms of haversines, and can be more
%    accurate when measuring small angular distances.  It can be somewhat
%    inaccurate when the two points are antipodal.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    18 February 2009
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    "Great-circle distance",
%    Wikipedia.
%
%  Input:
%
%    real LAT1, LON1, the latitude and longitude of
%    the first point.
%
%    real LAT2, LON2, the latitude and longitude of
%    the second point.
%
%    real R, the radius of the sphere.
%
%  Output:
%
%    real DIST, the great circle distance between
%    the points, measured in the same units as R.
%
  s = ( sin ( ( lat1 - lat2 ) / 2.0 ) ).^2 ...
    + cos ( lat1 ) * cos ( lat2 ) * ( sin ( ( lon1 - lon2 ) / 2.0 ) ).^2;

  s = sqrt ( s );

  dist = 2.0 * r * asin ( s );

  return
end
