# The n-vector page

Position calculations – simple and exact solutions by means of n-vector

n-vector is used to replace latitude and longitude, making the calculations simple and non-singular. Full accuracy is achieved for any global position (and for any distance).

This web page contains solutions to ten common geographical position calculations.

Background

Written by Kenneth Gade (PhD, Principal Scientist in the Navigation Group at FFI, Norwegian Defence Research Establishment).

Gade, K. (2010). A Non-singular Horizontal Position Representation, The Journal of Navigation, Volume 63, Issue 03, pp 395-417, July 2010.

Used worldwide for critical applications:

n-vector is used worldwide, e.g. for navigation, traffic control, and surveillance, of aircraft, cars, ships and submarines/submersibles. One example is Thales, who is world-leading at air traffic management (ATM). Their newest generation of ATM system is based on n-vector (replacing previous alternatives).

Useful pages

## Solving ten common problems

We will now illustrate how ten common geographical position calculations can be solved using n-vector. The solutions are also available as Matlab code (and as C++, C#, Python, JavaScript, and other languages).

Color coding for figures and variables:

• Input (red): The initial input that is given
• Output (green): The solution to be found
• Function (blue): A function that is available from the downloadable code.

## Example 1: A and B to delta

Given two positions A and B. Find the exact vector from A to B in meters north, east and down, and find the direction (azimuth/bearing) to B, relative to north. Use WGS-84 ellipsoid.

### Problem

See vector symbols explained for more details about the mathematical notation.

Given two positions, A and B as latitudes, longitudes and depths (relative to Earth, E):
–
$la{t}_{EA}$
and
(depth = –height)
$la{t}_{EB}$
and

Find the exact vector between the two positions, given in meters north, east, and down, and find the direction (azimuth) to B, relative to north.

Details:

• Assume WGS-84 ellipsoid. The given depths are from the ellipsoid surface.
• Use position A to define north, east, and down directions. (Due to the curvature of Earth and different directions to the North Pole, the north, east, and down directions will change (relative to Earth) for different places. Position A must be outside the poles for the north and east directions to be defined.

### Solution

Note: The solutions are also available as downloadable code in several different programming languages.

1. First, the given latitudes and longitudes are converted to n-vectors:

2. When the positions are given as n-vectors (and depths), it is easy to find the delta vector decomposed in E (using the first function mentioned at the end of n-vector explained):

No ellipsoid is specified when calling the function, thus WGS-84 (default) is used.

3. We now have the delta vector from A to B, but (as seen from the superscript) the three coordinates of the vector are along the Earth coordinate frame E, while we need the coordinates to be north, east and down. To get this, we define a North-East-Down coordinate frame called N, and then we need the rotation matrix (direction cosine matrix)
to go between E and N (as explained here). In our math library, we have a simple function that calculates
from n-vector, and we use this function (using n-vector at A-position):

4. Now the delta vector is easily decomposed in N. When deciding if we should premultiply
with
or
, we use the "closest-rule" saying that the subscript that is closest to the vector should be the frame where the vector is decomposed (also explained here). Since the vector is decomposed in E, we must use
(
is the transpose of
):

5. The three components of
are the north, east and down displacements from A to B in meters. The azimuth is simply found from element 1 and 2 of the vector (the north and east components):

## Example 2: B and delta to C

Given the position of vehicle B and a bearing and distance to an object C. Find the exact position of C. Use WGS-72 ellipsoid.

### Problem

See vector symbols explained for more details about the mathematical notation.

A radar or sonar attached to a vehicle B (Body coordinate frame) measures the distance and direction to an object C. We assume that the distance and two angles measured by the sensor (typically bearing and elevation relative to B) are already converted (by converting from spherical to Cartesian coordinates) to the vector
(i.e. the vector from B to C, decomposed in B). The position of B is given as
and
, and the orientation (attitude) of B is given as
(this rotation matrix can be found from roll/pitch/yaw by using zyx2R.m).

Find the exact position of object C as n-vector and depth (
and
), assuming Earth ellipsoid with semi-major axis a and flattening f. For WGS-72, use a = 6 378 135 m and f = 1/298.26.

### Solution

Note: The solutions are also available as downloadable code in several different programming languages.

1. The delta vector is given in B. It should be decomposed in E before using it, and thus we need
. This matrix is found from the matrices
and
, and we need to find
, as in Example 1:

2. Now, we can find
by using that the closest frames cancel when multiplying two rotation matrices (i.e. N is cancelled here):

3. The delta vector is now decomposed in E (details about decomposing are found here):

4. It is now easy to find the position of C using the second function mentioned here (with custom ellipsoid overriding the default WGS-84).

Details: For simplicity, the orientation of the vehicle in this example was given relative to N. If the navigation system of the vehicle was using the non-singular coordinate frame L instead, the solution would be the same, but replacing the function n_E2R_EN.m with n_E_and_wa2R_EL.m. See Table 2 in Gade (2010) and the help text in n_E_and_wa2R_EL.m for details.

## Example 3: ECEF-vector to geodetic latitude

Given an ECEF-vector of a position. Find geodetic latitude, longitude and height (using WGS-84 ellipsoid).

### Problem

See vector symbols explained for more details about the mathematical notation.

Position B is given as an “ECEF-vector”
(i.e. a vector from E, the center of the Earth, to B, decomposed in E).

Find the geodetic latitude, longitude and height,
$la{t}_{EB}$
,
and
, assuming WGS-84 ellipsoid.

### Solution

Note: The solutions are also available as downloadable code in several different programming languages.

1. We have a function that converts p-vector to n-vector, see Appendix B in Gade (2010):

2. Find latitude, longitude and height from
and

=  –

## Example 4: Geodetic latitude to ECEF-vector

Given geodetic latitude, longitude and height. Find the ECEF-vector (using WGS-84 ellipsoid).

### Problem

See vector symbols explained for more details about the mathematical notation.

Geodetic latitude, longitude and height are given for position B as
$la{t}_{EB}$
,
and
, find the ECEF-vector for this position,
.

### Solution

Note: The solutions are also available as downloadable code in several different programming languages.

1. First, the given latitude and longitude are converted to n-vector:

2. We want
from
, and have functions that convert between these two. Now we need n_EB_E2p_EB_E.m. See also Appendix B in Gade (2010).

In the remaining examples, we assume that a spherical Earth model is sufficiently accurate, and then the position functions used above are not needed. The examples demonstrate the simplicity of these calculations when using n-vector instead of latitude and longitude. The n-vector solutions are also non-singular for all Earth-positions and work across the Date Line (180th meridian).

## Example 5: Surface distance

Given position A and B. Find the surface distance (i.e. great circle distance) and the Euclidean distance.

### Problem

See vector symbols explained for more details about the mathematical notation.

Given two positions A and B as

.

Find the surface distance
(i.e. great circle distance). The heights of A and B are not relevant (i.e. if they do not have zero height, we seek the distance between the points that are at the surface of the Earth, directly above/below A and B). The Euclidean distance (chord length)
should also be found (for
, nonzero heights can be used). Use Earth radius
.

### Solution

Note: The solutions are also available as downloadable code in several different programming languages.

The surface distance can be found in three different ways; by utilizing the dot product of the vectors, the cross product, or a combination:

The Euclidean distance is found by

If nonzero heights for A and B are wanted for the Euclidean distance, each n-vector should be multiplied with
, where
is the corresponding height of each n-vector. If ellipsoidal Earth model is wanted, use n_EA_E_and_n_EB_E2p_AB_E.m from Example 1, and take the norm of the output vector.

## Example 6: Interpolated position

Given the position of B at time
and
. Find an interpolated position at time
.

### Problem

See vector symbols explained for more details about the mathematical notation.

Given the position of B at time
and
and
.

Find an interpolated position at time
. All positions are given as n-vectors.

### Solution

Note: The solutions are also available as downloadable code in several different programming languages.

Standard interpolation can be used directly with n-vector:

## Example 7: Mean position/center

Given three positions A, B, and C. Find the mean position (center/midpoint).

### Problem 7

See vector symbols explained for more details about the mathematical notation.

Three positions AB, and C are given as n-vectors
, and
. Find the mean position, M, given as
. Note that the calculation is independent of the heights/depths of the positions.

### Solution 7

Note: The solutions are also available as downloadable code in several different programming languages.

The mean position is simply given by the mean n-vector:

### Details about the definition of the horizontal geographical mean position

Several definitions of a geographical mean/center/midpoint are possible. The solution given here can be described as the “center of gravity” of the given positions, and can be compared to the centroid of a geometrical shape. Informally, the found mean position can be described by the following: Assume a sphere that is a model of the Earth, and let the sphere roll freely on a horizontal plane (in a uniform gravitational field). Place weights at the given positions of the sphere, and the weights will pull one side of the sphere down due to the gravitation (assuming the positions are not antipodal). When the sphere is in equilibrium, the “center of gravity”-position is the tangent point between the sphere and the plane.

Another possible way to define a midpoint is the position where the sum of surface distances (great circle distances) from the original positions is at a minimum. This midpoint is undefined when only two positions are given. If three positions are given in one dimension as 0, 0, and 3, this midpoint is at 0, while the arithmetic mean is 1. Iterations are probably needed to calculate this midpoint, and code for this is not included at this web site.

## Example 8: A and azimuth/distance to B

Given position A and an azimuth/bearing and a (great circle) distance. Find the destination point B.
Problem 8
See vector symbols explained for more details about the mathematical notation.

Position A is given as n-vector
. We also have an initial direction of travel given as an azimuth (bearing) relative to north (clockwise), and finally the distance to travel along a great circle is given as
.

Find the destination point B, given as
.

In geodesy, this is known as "The first geodetic problem" or "The direct geodetic problem" for a sphere, and we see that this is similar to Example 2, but now the delta is given as an azimuth and a great circle distance. "The second/inverse geodetic problem" for a sphere is already solved in Examples 1 and 5.

### Solution 8

Note: The solutions are also available as downloadable code in several different programming languages.

The azimuth (relative to north) is a singular quantity (undefined at the Poles), but from this angle we can find a (non-singular) quantity that is more convenient when working with vector algebra: a vector
that points in the initial direction. We find this from azimuth by first finding the north and east vectors at the start point, with unit lengths (see also equations (9) and (10) in Gade (2010), and the figure below):

Here we have assumed that our coordinate frame E has its z-axis along the rotational axis of the Earth, pointing towards the North Pole. Hence, this axis is given by
.

The two vectors
and
are horizontal, orthogonal, and span the tangent plane at the initial position. A unit vector
in the direction of the azimuth is now given by:

With the initial direction given as
instead of azimuth, it is now quite simple to find
. We know that
and
are orthogonal, and they will span the plane where
will lie. Thus, we can use sin and cos in the same manner as above, with the angle travelled given by
/
:

## Example 9: Intersection of two paths / triangulation

Given path A going through
and
, and path B going through
and
. Find the intersection of the two paths.

Variant: Each of the two paths is specified by start point and azimuth instead of two points, which corresponds to triangulation.

Credits: Some readers of the paper Gade (2010) have contacted the author and reported new examples (other than those included in the paper) they have solved by means of n-vector. The following example has been reported twice, and is thus included here. Thanks to Even Børhaug at Kongsberg Maritime and Chris Veness at Movable Type Ltd, for this example. Børhaug reported that the solution he found that was based on latitude and longitude used multiple pages of code, while Veness says that such a solution is "horribly complex". Using n-vector, the intersection is very simple to find.

### Problem 9

See vector symbols explained for more details about the mathematical notation.

Define a path from two given positions (at the surface of a spherical Earth), as the great circle that goes through the two points (assuming that the two positions are not antipodal).

Path A is given by
and
, while path B is given by
and
.

Find the position C where the two paths intersect, as
.

### Solution 9

Note: The solutions are also available as downloadable code in several different programming languages.

A convenient way to represent a great circle is by its normal vector (i.e. the normal vector to the plane containing the great circle). This normal vector is simply found by taking the cross product of the two n-vectors defining the great circle (path). Having the normal vectors to both paths, the intersection is now simply found by taking the cross product of the two normal vectors:

Note that there will be two places where the great circles intersect, and thus two solutions are found. Selecting the solution that is closest to e.g.
can be achieved by selecting the solution that has a positive dot product with
(or the mean position from Example 7 could be used instead of
). In program code, this can be implemented by selecting any of the two solutions above (+ or –), called C', and calculate:

Variant / triangulation: If each of the two paths is specified by start point and azimuth instead of two points, the normal vectors to the great circles can be found by the following:
1. Find the direction vector for each path in the same manner as
in Example 8.
2. Take the cross product of the direction vector and the n-vector for the initial position.

## Example 10: Cross track distance (cross track error)

Given path A going through
and
, and a point B. Find the cross track distance/cross track error between B and the path.

Variant: The path is specified by start point and azimuth instead of two points.

(Along track distance (and the corresponding point on the path) can also be found quite easily)

Credits: Thanks to Chris Veness at Movable Type Ltd who first reported this example.

### Problem 10

See vector symbols explained for more details about the mathematical notation.

Path A is given by the two n-vectors
and
(as in the previous example), and a position B is given by
.

Find the cross track distance
between the path A (i.e. the great circle through
and
) and the position B (i.e. the shortest distance at the surface, between the great circle and B). Also, find the Euclidean distance
between B and the plane defined by the great circle. Use Earth radius
.

### Solution 10

Note: The solutions are also available as downloadable code in several different programming languages.

First, find the normal
to the great circle, with direction given by the right hand rule and the direction of travel:

We now see that the cross track distance to B is the great circle distance from

Finding the Euclidean distance is even simpler, since it is the projection of
onto
, thus simply the dot product:

For both
and
, positive answers means that B is to the right of the track.

Variant: If the path is specified by start point and azimuth instead of two points, the normal vector to the great circle can be found by the following:
1. Find the direction vector for the path in the same manner as
in Example 8.
2. Take the cross product of the direction vector and the n-vector for the initial position.

Along track distance to the closest point on path A can be found by using the four vectors in this example, and then calculating the intersection point as in Example 9 (when the n-vector for the intersection point is found, the along track distance from
is found as in Example 5). Thanks to Enrico Spinielli at EUROCONTROL for reporting this.