Accelerating the pace of engineering and science

# Documentation Center

• Trials
• Product Updates

# distance

Distance between points on sphere or ellipsoid

## Syntax

[arclen,az] = distance(lat1,lon1,lat2,lon2)
[arclen,az] = distance(lat1,lon1,lat2,lon2,ellipsoid)
[arclen,az] = distance(lat1,lon1,lat2,lon2,units)
[arclen,az] = distance(lat1,lon1,lat2,lon2,ellipsoid,units)
[arclen,az] = distance(track,...)
[arclen,az] = distance(pt1,pt2)
[arclen,az] = distance(pt1,pt2,ellipsoid)
[arclen,az] = distance(pt1,pt,units)
[arclen,az] = distance(pt1,pt2,ellipsoid,units)
[arclen,az] = distance(track,pt1,...)

## Description

[arclen,az] = distance(lat1,lon1,lat2,lon2) computes the lengths, arclen, of the great circle arcs connecting pairs of points on the surface of a sphere. In each case, the shorter (minor) arc is assumed. The function can also compute the azimuths, az, of the second point in each pair with respect to the first (that is, the angle at which the arc crosses the meridian containing the first point). The input latitudes and longitudes, lat1, lon1, lat2, lon2, can be scalars or arrays of equal size and must be expressed in degrees. arclen is expressed in degrees of arc and will have the same size as the input arrays. az is measured clockwise from north, in units of degrees. When given a combination of scalar and array inputs, the scalar inputs are automatically expanded to match the size of the arrays.

[arclen,az] = distance(lat1,lon1,lat2,lon2,ellipsoid) computes geodesic arc length and azimuth assuming that the points lie on the reference ellipsoid defined by the input ellipsoid. ellipsoid is a referenceSphere, referenceEllipsoid, or oblateSpheroid object, or a vector of the form [semimajor_axis eccentricity]. The output, arclen, is expressed in the same length units as the semimajor axis of the ellipsoid.

[arclen,az] = distance(lat1,lon1,lat2,lon2,units) uses the input string units to define the angle unit of the outputs arclen and az and the input latitude-longitude coordinates. units may equal 'degrees' (the default value) or 'radians'.

[arclen,az] = distance(lat1,lon1,lat2,lon2,ellipsoid,units) uses the units string to specify the units of the latitude-longitude coordinates, but the output range has the same units as the semimajor axis of the ellipsoid.

[arclen,az] = distance(track,...) uses the input string track to specify either a great circle/geodesic or a rhumb line arc. If track equals 'gc' (the default value), then great circle distances are computed on a sphere and geodesic distances are computed on an ellipsoid. If track equals 'rh', then rhumb line distances are computed on either a sphere or ellipsoid.

[arclen,az] = distance(pt1,pt2) accepts N-by-2 coordinate arrays pt1 and pt2 such that pt1 = [lat1 lon1] and pt2 = [lat2 lon2], where lat1, lon1, lat2, and lon2 are column vectors. It is equivalent to arclen = distance(pt1(:,1),pt1(:,2),pt2(:,1),pt2(:,2)).

[arclen,az] = distance(pt1,pt2,ellipsoid),

[arclen,az] = distance(pt1,pt,units),

[arclen,az] = distance(pt1,pt2,ellipsoid,units), and

[arclen,az] = distance(track,pt1,...) are all valid calling forms.

## Examples

Using pt1,pt2 notation, find the distance from Norfolk, Virginia (37ºN, 76ºW), to Cape St. Vincent, Portugal (37ºN, 9ºW), just outside the Straits of Gibraltar. The distance between these two points depends upon the track string selected.

```arclen = distance('gc',[37,-76],[37,-9])

arclen =
52.3094

arclen = distance('rh',[37,-76],[37,-9])

arclen =
53.5086```

The difference between these two tracks is 1.1992 degrees, or about 72 nautical miles. This represents about 2% of the total trip distance. The trade-off is that at the cost of those 72 miles, the entire trip can be made on a rhumb line with a fixed course of 90º, due east, while in order to follow the shorter great circle path, the course must be changed continuously.

On a meridian and on the Equator, great circles and rhumb lines coincide, so the distances are the same. For example,

```% Great circle distance
arclen = distance(37,-76,67,-76)

arclen =
30.0000

% Rhumb line distance
arclen = distance('rh',37,-76,67,-76)

arclen =
30.0000```

The distances are the same, 30º, or about 1800 nautical miles. (There are about 60 nautical miles in a degree of arc length.)

## Alternatives

Distance between two points can be calculated in two ways. For great circles (on the sphere) and geodesics (on the ellipsoid), the distance is the shortest surface distance between two points. For rhumb lines, the distance is measured along the rhumb line passing through the two points, which is not, in general, the shortest surface distance between them.

When you need to compute both distance and azimuth for the same point pair(s), it is more efficient to do so with a single call to distance. That is, use

```[arclen az] = distance(...);
```

rather than the slower

```arclen = distance(...)
az = azimuth(...)```

To express the output arclen as an arc length in either degrees or radians, omit the ellipsoid argument. This is possible only on a sphere. If ellipsoid is supplied, arclen is a distance expressed in the same units as the semimajor axis of the ellipsoid. Specify ellipsoid as [R 0] to compute arclen as a distance on a sphere of radius R, with arclen having the same units as R.

## More About

expand all

### Algorithms

Distance calculations for geodesics degrade slowly with increasing distance and may break down for points that are nearly antipodal, as well as when both points are very close to the Equator. In addition, for calculations on an ellipsoid, there is a small but finite input space, consisting of pairs of locations in which both the points are nearly antipodal and both points fall close to (but not precisely on) the Equator. In this case, a warning is issued and both arclen and az are set to NaN for the "problem pairs."

## See Also

Was this topic helpful?