# distance

Distance between points on sphere or ellipsoid

## Syntax

## Description

`[`

computes the lengths, `arclen`

,`az`

] = distance(`lat1`

,`lon1`

,`lat2`

,`lon2`

)`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).

## Examples

### Find Difference in Distance Along Two Tracks

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
method.

```
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 tradeoff 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

## Input Arguments

## Output Arguments

## Tips

The size of nonscalar latitude and longitude coordinates,

`lat1`

,`lon1`

,`lat2`

, and`lon2`

, must be consistent. When given a combination of scalar and array inputs, the`distance`

function automatically expands scalar inputs to match the size of the arrays.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`

.

## 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.”

## 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(...);

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

## Version History

**Introduced before R2006a**