# n-vector explained

## What is n-vector?

n-vector is just the 3D normal vector to the Earth ellipsoid (or sphere, if spherical Earth model is used). It can replace latitude and longitude (and other alternatives) to represent horizontal position, giving several advantages. This page gives a brief introduction, see Gade (2010) for a more detailed explanation. The n-vector is the normal vector to the Earth model (reference ellipsoid).

## Why is n-vector needed?

Performing global position calculations often involves one or more of the following concerns:
1. Complex implementations (many, and often complex lines of program code needed)
2. Approximations, e.g.
a. distortion in map projections
b. assuming spherical Earth when ellipsoidal should be used
Errors often increase with increasing distances
3. Equation/code not valid/accurate for all Earth positions, e.g.
a. Latitude/longitude:
i. Singularities at Poles
ii. Discontinuity at International Date Line (±180° meridian)
b. Map projections: usually valid for a limited area, e.g. UTM
4. Iterations (need to iterate to achieve needed accuracy)

By using n-vector and the solutions provided here, these concerns can be avoided for a majority of position calculations.

### What can go wrong if using latitude and longitude?

Even the simple discontinuity of longitude at the International Date Line (±180° meridian) can give serious consequences if not handled correctly in all the code. An example illustrating this was the computer crash experienced by 12 fighter aircrafts (F-22 Raptors) when they crossed this meridian in 2007.

The discontinuity can be quite easily handled by adding specific code, while the singularities at the Poles are much more challenging to deal with. Examples of how the singularities can affect calculations with latitude and longitude are found in Section 6 in Gade (2010). The discontinuity of longitude caused serious problems for the F-22 Raptors when the longitude instantly changed from -180° to +180° (when crossing the International Date Line). Photo: U.S. Air Force/Master Sgt. Kevin J. Gruenwald / Wikimedia Commons public domain

## n-vector

As discussed above, using latitude and longitude in calculations may lead to several problems due to the singularities at the Poles and complex behavior near the Poles, and due to the discontinuity at the Date Line (±180° meridian).

Instead of latitude and longitude, we represent position with an “n-vector”, which is the normal vector to the Earth model (the same reference ellipsoid that is used for latitude and longitude). When using n-vector, all Earth-positions are treated equally, and there is no need to worry about singularities or discontinuities. An additional benefit with using n-vector is that many position calculations can be solved with simple vector algebra (e.g. dot product and cross product).

In documents n-vector is written
${n}^{E}$
, while in program code we use n_E. E denotes an Earth-fixed coordinate frame, and it indicates that the three components of n-vector are along the three axes of E. More details about the notation used are found here. The position of the North Pole given as n-vector is
${n}^{E}=\left[\begin{array}{c}0\\ 0\\ 1\end{array}\right]$
(assuming the coordinate frame E has its z-axis pointing to the North Pole). For all the details about n-vector, see the reference Gade (2010)

### Converting between n-vector and lat/long

Calculating n-vector from latitude/longitude is done using

${n}^{E}=\left[\begin{array}{c}\mathrm{cos}\left(lat\right)\mathrm{cos}\left(long\right)\\ \mathrm{cos}\left(lat\right)\mathrm{sin}\left(long\right)\\ \mathrm{sin}\left(lat\right)\end{array}\right]$

This can be seen from the geometry and the axes of E. The relation is true both for ellipsoidal or spherical Earth model.

To find latitude from n-vector we have

$lat=\mathrm{asin}\left({n}_{z}^{E}\right)=\text{atan2}\left({n}_{z}^{E},\sqrt{{\left({n}_{x}^{E}\right)}^{2}+{\left({n}_{y}^{E}\right)}^{2}}\right)$

where the x, y and z subscripts indicate elements 1, 2 and 3 of the n-vector. Note that the atan2-variant has better numerical properties, and is thus recommended for implementation.

Longitude is found from

$long=\text{atan2}\left({n}_{y}^{E},{n}_{x}^{E}\right)$

In the downloadable code, two functions lat_long2n_E and n_E2lat_long are used for these conversions.

## Many position calculations can be solved with only two functions

It turns out that very often, the problem to be solved is one of these:

1. A and B => delta: Given two positions, A and B (e.g. as latitudes/longitudes/heights), find the (delta) vector from A to B in meters. See also Example 1
2. A and delta => B: Given position A (e.g. as lat/long/height) and a vector in meters from A to B, find position B. See also Example 2.
Two functions performing these calculations are available for download, using n-vectors for position A and B as input/output. The n-vectors for position A and B are written
${n}_{EA}^{E}$
and
${n}_{EB}^{E}$
, while in program code we use n_EA_E and n_EB_E. The (delta) position vector from A to B is written
${p}_{AB}^{E}$
(p_AB_E). More information about the notation is found here

Based on the above variable names, the two above functions are called:
1. n_EA_E_and_n_EB_E2p_AB_E
2. n_EA_E_and_p_AB_E2n_EB_E

By default, these functions use the WGS-84 ellipsoid, but any custom ellipsoid (or sphere) may be specified.