JUMP TO TOPIC

# Euclidean Distance Calculator + Online Solver With Free Steps

The **Euclidean Distance Calculator** finds the Euclidean distance between any two real or complex n-dimensional vectors. Both vectors must have equal dimensions (number of components).

The calculator supports **any-dimensional** vectors. That is, **n** can be any positive integer, and the input vector can exceed 3-dimensions. However, such high-dimensional vectors are not visualizable.

**Variable entries** within a vector are also supported. That is, you may enter a vector $\vec{p}$ = (x, 2) and $\vec{q}$ = (y, 3), in which case the calculator will return three results.

## What Is the Euclidean Distance Calculator?

**The Euclidean Distance Calculator is an online tool that calculates the Euclidean distance between two n-dimensional vectors $\vec{p}$ and $\vec{q}$ given the components of both the vectors at the input. **

The **calculator interface** consists of two vertically stacked input text boxes. Each text box corresponds to a single vector of n-dimensions.

Both the vectors must be in **Euclidean or complex space**, and $\mathbf{n}$ should be some positive integer and must be equal for both vectors. Mathematically, the calculator evaluates:

\[ d ( \, \vec{p}, \, \vec{q} \, ) = \left \| \, \vec{q}-\vec{p} \, \right \| \]

Where $d ( \, \vec{p}, \, \vec{q} \, )$ represents the desired Euclidean distance and $\|$ indicates the** L2 norm**. Note that if one of the vectors is a zero vector (that is, all of its components are zero), the result is the L2 norm (length or magnitude) of the non-zero vector.

## How To Use the Euclidean Distance Calculator

You can use the **Euclidean Distance Calculator** to find the Euclidean distance between any two vectors $\vec{p}$ and $\vec{q}$ using the following guidelines.

For example, let us assume we want to find the euclidean distance between the two vectors:

**$\vec{p}$ = (5, 3, 4) and $\vec{q}$ = (4, 1, 2) **

### Step 1

Ensure both vectors have equal dimensions (number of components).

### Step 2

Enter the first vector’s components into either the first or second text box as “5, 3, 4” without commas.

### Step 3

Enter the second vector’s components into the other text box as “4, 1, 2” without commas.

### Step 4

Press the **Submit **button to get the resulting Euclidean distance:

\[ d ( \, \vec{p}, \, \vec{q} \, ) = 3 \]

The order in which you enter the vectors does not matter because the Euclidean distance involves the **square of the difference** between corresponding vector components. This automatically removes any negative signs so $\| \, \vec{q}-\vec{p} \, \| = \| \, \vec{p}-\vec{q} \, \|$.

### Entering Complex Vectors

If any one component of an n-dimensional vector is complex, that vector is said to be defined in the complex space $\mathbb{C}^n$. To enter iota $i = \sqrt{-1}$ in such components, type “i” after the coefficient of the imaginary part.

For example, in $\vec{p} = (1+2i, \, 3)$ we have p1 = 1+2i where 2i is the imaginary part. To enter p1, type “1+2i” without commas into the text box. Note that entering “1+2i, 3” is the same as entering “1+2i, 3+0i”.

### Results

#### Non-variable Inputs

If all the components are defined, constant values belonging to $\mathbb{C}$ or $\mathbb{R}$, the calculator outputs a single value in the same set.

#### Variable Inputs

If the input contains any characters other than “i” (treated as iota i) or a combination of letters corresponding to a mathematical constant such as “pi” (treated as $\pi$), it is considered a variable. You can enter any number of variables, and they may be in either one or both of the input vectors.

For example, let us say we want to enter $\vec{p}$ = (7u, 8v, 9). To do so, we would type “7u, 8v, 9.” For such an input on any one of the vectors, the calculator will show **three results**:

- The first result is the most general form and has the modulus operator on all variable terms.
- The second result assumes that the variables are complex and performs the modulus operation on each difference component before squaring.
- The third result assumes that the variables are real and contain the square of the difference of variable terms with other components.

#### Plots

If a **minimum of one and a maximum of two variables** are present in the input, the calculator will also plot some graphs.

In the case of one variable, it plots the 2D graph with distance along the y-axis and variable value along the x-axis. In the case of two variables, it plots the 3D graph and its equivalent contour plot.

## How Does the Euclidean Distance Calculator Work?

The calculator works by using the **generalized distance formula**. Given any two vectors:

\[ \vec{p} = (p_1, \, p_2, \, \ldots, \, p_n) \quad \text{and} \quad \vec{q} = (q_1, \, q_2, \, \ldots, \, q_n) \in \mathbb{R}^n \tag*{$n = 1, \, 2, \, 3, \, \ldots$} \]

The Euclidean distance is then given as:

\[ d ( \, \vec{p}, \, \vec{q} \, ) = \sqrt{(q_1-p_1)^2 + (q_2-p_2)^2+\ldots+(q_n-p_n)^2} \]

Essentially, the calculator uses the following general equation:

\[ d ( \, \vec{p}, \, \vec{q} \, ) = \sqrt{\sum_{i=1}^n \left ( q_i-p_i \right ) ^2} \]

Where pi and qi represent the i component of the vectors $\vec{p}$ and $\vec{q}$ respectively. For example, if $\vec{p}$ is 3-dimensional, then $\vec{p}$ = (x, y, z) where p1 = x, p2 = y, p3 = z.

Euclidean distance can also be thought of as the **L2 norm** of the difference vector $\vec{r}$ between the two vectors $\vec{p}$ and $\vec{q}$. That is:

\[ d \left ( \, \vec{p}, \, \vec{q} \, \right ) = \| \, \vec{q}-\vec{p} \, \| = \| \, \vec{r} \, \| \quad \text{where} \quad \vec{r} = \vec{q}-\vec{p} \]

For **complex corresponding components** a+bi in $\vec{p}$ and c+di in $\vec{q}$, the calculator squares the **modulus** of the difference between the real and imaginary parts of the vector components in the calculations (refer to Example 2). That is:

\[ d ( \, \vec{p}, \, \vec{q} \, ) = \sqrt{ \left ( \sqrt{(a-c)^2+(b-d)^2} \right ) ^2 + \text{squared differences of other components} } \]

Where $\sqrt{(a-c)^2+(b-d)^2}$ represents the modulus of the difference between the complex numbers a+bi and c+di.

## Solved Examples

### Example 1

Find the Euclidean distance between the two vectors:

**$\vec{p}$ = (2, 3) **

** $\vec{q}$ = (-6, 5) **

Show that it is equal to the L2 norm of the difference vector $\vec{r} = \vec{q}-\vec{p}$.

### Solution

\[ d ( \, \vec{p}, \, \vec{q} \, ) = \sqrt{ (-6-2)^2 + (5-3)^2 } = \sqrt{68 } = 8.2462 \]

\[ \vec{r} = \left( \begin{array}{c} -6 \\ 5 \end{array} \right) – \left( \begin{array}{c} 2 \\ 3 \end{array} \right) = \left( \begin{array}{c} -8 \\ 2 \end{array} \right) \]

The L2 norm of $\vec{r}$ is given as:

\[ \| \, \vec{r} \, \| = \sqrt{(-8)^2 + (2)^2} = \sqrt{68} = 8.24621\]

Thus, if $\vec{r} = \vec{q} – \vec{p}$, then $d ( \, \vec{p}, \, \vec{q} \, ) = \| \, \vec{r} \, \|$ as proved.

### Example 2

Consider the two complex vectors:

**$\vec{p}$ = (1+2i, 7) **

** $\vec{q}$ = (3-i, 7+4i)**

Calculate the distance between them.

### Solution

Since we have complex vectors, we must use the square of the **modulus **(indicated by|a|) of each component’s difference.

\[ d ( \, \vec{p}, \, \vec{q} \, ) = \sqrt{ \left| \, 3-i -(1+2i) \, \right|^2 + \left| \, (7+4i-7) \, \right|^2 } \]

\[ d ( \, \vec{p}, \, \vec{q} \, ) = \sqrt{ \left| \, 2-3i \, \right|^2 + \left| \, 4i \, \right|^2 } \]

The modulus is simply the square root of squared sum of the real and imaginary parts so:

\[ |z| = \sqrt{\text{Re}(z)^2 + \text{Im}(z)^2} \]

\[ \Rightarrow |2-3i| = \sqrt{2^2 + (-3)^2} = \sqrt{13} \]

\[ \Rightarrow |4i| = \sqrt{0^2 + 4^2} = 4 \]

Which gets us:

\[ d ( \, \vec{p}, \, \vec{q} \, ) = \sqrt{ \left( \sqrt{13} \right)^2 + 4^2 } = \sqrt{29} = 5.38516 \]

### Example 3

Find the Euclidean distance between the following high-dimensional vectors with variable components:

\[ \vec{p} = \left( \begin{array}{c} 3 \\ 9 \\ x+2 \\ 5 \end{array} \right) \quad \text{and} \quad \vec{q} = \left( \begin{array}{c} -7 \\ 1 \\ y-1 \\ 6 \end{array} \right) \]

### Solution

We have two variables x and y. The Euclidean distance is given as:

\[ d ( \, \vec{p}, \, \vec{q} \, ) = \sqrt{ (-7-3)^2 + (1-9)^2 + (y-1-x-2)^2 + (6-5)^2 } \]

\[ d ( \, \vec{p}, \, \vec{q} \, ) = \sqrt{ 100 + 64 + (y-x-3)^2 + 1 } = \sqrt{ (y-x-3)^2 + 165} \]

Since the variables may be complex, the **general result** is given by the calculator as:

\[ d ( \, \vec{p}, \, \vec{q} \, ) = \sqrt{ \left| \, y-x-3 \, \right|^2 + 165} \]

The **second result** assumes variables are complex and gives:

\[ x = \text{Re}(x) + \text{Im}(x) \quad \text{and} \quad y = \text{Re}(y) + \text{Im}(y) \]

\[ d ( \, \vec{p}, \, \vec{q} \, ) = \sqrt{ \left| \, \text{Re}(y)-\text{Re}(x)-3+\text{Im}(x)-\text{Im}(y) \, \right|^2 + 165} \]

Let z be a complex number such that:

\[ z = \text{Re}(y)-\text{Re}(x)-3+\text{Im}(x)-\text{Im}(y) \]

\[ \Rightarrow \text{Re}(z) = \text{Re}(y)-\text{Re}(x)-3 \quad \text{and} \quad \text{Im}(z) = \text{Im}(x)-\text{Im}(y)\]

Thus, our expression for Euclidean distance becomes:

\[ d ( \, \vec{p}, \, \vec{q} \, ) = \sqrt{ \left| z \right|^2 + 165} \]

Applying modulus:

\[ d ( \, \vec{p}, \, \vec{q} \, ) = \sqrt{ \left( \sqrt{\text{Re(z)}^2 + \text{Im}(z)^2} \right)^2+ 165} \]

\[ d ( \, \vec{p}, \, \vec{q} \, ) = \sqrt{ (\text{Re}(y)-\text{Re}(x)-3)^2 +(\text{Im}(x)-\text{Im}(y))^2+ 165} \]

The** third result** assumes the variables are real, and replaces the modulus operator with parentheses:

\[ d ( \, \vec{p}, \, \vec{q} \, ) = \sqrt{ (y-x-3)^2 + 165} \]

The graph (in orange) of the Euclidean distance (blue axis) above as a function of x (red axis) and y (green axis) is given below:

*All images/plots were created using GeoGebra.*