Vector Magnitude

Calculate the length of a vector in any dimension using the Pythagorean theorem.

What is Vector Magnitude?

The magnitude of a vector (also called its length, norm, or absolute value) is a non-negative number that represents the "size" of the vector.

The Formula

For a 2D vector \(\vec{v} = (x, y)\):

\[|\vec{v}| = \sqrt{x^2 + y^2}\]

For a 3D vector \(\vec{v} = (x, y, z)\):

\[|\vec{v}| = \sqrt{x^2 + y^2 + z^2}\]

For an n-dimensional vector \(\vec{v} = (v_1, v_2, ..., v_n)\):

\[|\vec{v}| = \sqrt{\sum_{i=1}^{n} v_i^2} = \sqrt{v_1^2 + v_2^2 + ... + v_n^2}\]

Examples

Example 1: 2D Vector (3, 4)

|v| = √(3² + 4²) = √(9 + 16) = √25 = 5

Example 2: 3D Vector (1, 2, 2)

|v| = √(1² + 2² + 2²) = √(1 + 4 + 4) = √9 = 3

Normalization

A unit vector has magnitude 1. To normalize a vector (convert it to a unit vector), divide by its magnitude:

\[\hat{v} = \frac{\vec{v}}{|\vec{v}|}\]

Properties

  • \(|\vec{v}| \geq 0\) (always non-negative)
  • \(|\vec{v}| = 0\) if and only if \(\vec{v} = \vec{0}\)
  • \(|c\vec{v}| = |c| \cdot |\vec{v}|\) (scalar multiplication)
  • \(\vec{v} \cdot \vec{v} = |\vec{v}|^2\) (dot product with itself)

Code Examples

// JavaScript
function magnitude(v) {
    return Math.sqrt(v.reduce((sum, x) => sum + x * x, 0));
}

magnitude([3, 4]);    // 5
magnitude([1, 2, 2]); // 3
# Python
import numpy as np

v = np.array([3, 4])
mag = np.linalg.norm(v)  # 5.0
💡

Vector Magnitude Explained Simply

Think of vector magnitude as simply the length of an arrow. If you draw a vector as an arrow pointing from one point to another, the magnitude tells you exactly how long that arrow is. It's the distance the vector covers, regardless of which direction it's pointing.

The formula comes directly from the Pythagorean theorem you learned in school. Imagine a vector pointing 3 units right and 4 units up. If you draw this on graph paper, you've created a right triangle where the vector is the hypotenuse. Just like finding the longest side of a right triangle (a² + b² = c²), you calculate √(3² + 4²) = √25 = 5. The vector's length is 5 units.

Here's a real-world way to think about it: imagine you're in a city with a grid of streets. You need to walk 3 blocks east and 4 blocks north. While you walked 7 blocks total, the straight-line distance (if you could fly) is only 5 blocks. That straight-line distance is the magnitude! It measures "as the crow flies," not the path you actually traveled.

This concept extends beautifully to 3D and beyond. In 3D, you're just adding another dimension to the triangle. If a drone flies 2 meters east, 2 meters north, and 1 meter up, its distance from the start is √(2² + 2² + 1²) = √9 = 3 meters. No matter how many dimensions you add, it's always about finding that straight-line distance using the same Pythagorean principle.

Frequently Asked Questions

How do you find the magnitude of a vector?
To find the magnitude of a vector, square each component, add them together, and take the square root. For a 2D vector (x, y), use the formula √(x² + y²). For example, the magnitude of vector (3, 4) is √(9 + 16) = √25 = 5.
What is the magnitude formula for a vector?
The magnitude formula depends on the dimension. For 2D: |v| = √(x² + y²). For 3D: |v| = √(x² + y² + z²). For n-dimensional vectors: |v| = √(v₁² + v₂² + ... + vₙ²). This formula is derived from the Pythagorean theorem.
Can the magnitude of a vector be negative?
No, magnitude can never be negative. Since magnitude represents length or distance, it is always zero or positive. Even if a vector has negative components like (-3, -4), the magnitude is still √(9 + 16) = 5, a positive value. The only vector with magnitude zero is the zero vector (0, 0, ...).
What is magnitude in physics?
In physics, magnitude is the size or amount of a physical quantity. For vector quantities like velocity, force, or acceleration, magnitude tells you "how much" without regard to direction. For example, speed is the magnitude of velocity—a car traveling at 60 mph north has a velocity magnitude (speed) of 60 mph.
What is the difference between magnitude and direction?
Magnitude and direction are the two components that define a vector. Magnitude tells you the size or length (how much), while direction tells you where it's pointing (which way). Together, they completely describe a vector. For example, "5 meters northeast" combines magnitude (5 meters) with direction (northeast).
How do you find magnitude from components?
To find magnitude from components, apply the Pythagorean theorem. Square each component, sum the squares, and take the square root. For vector v = (a, b, c), magnitude = √(a² + b² + c²). This works for any number of dimensions—just include all component squares under the radical.
What is a unit vector and how is it related to magnitude?
A unit vector is a vector with magnitude exactly equal to 1. To create a unit vector from any vector, divide the vector by its magnitude. This process is called normalization. The unit vector points in the same direction as the original but has a standardized length of 1, making it useful for representing pure direction.
How do you calculate magnitude in Python or JavaScript?
In Python, use NumPy: np.linalg.norm([3, 4]) returns 5.0. In JavaScript: Math.sqrt(v.reduce((sum, x) => sum + x*x, 0)) or use Math.hypot(3, 4) for 2D vectors. Both approaches implement the same Pythagorean formula.
What is the magnitude of a 3D vector?
The magnitude of a 3D vector (x, y, z) is calculated as √(x² + y² + z²). For example, the vector (1, 2, 2) has magnitude √(1 + 4 + 4) = √9 = 3. This extends the 2D Pythagorean theorem into three dimensions, measuring the straight-line distance in 3D space.
Why is vector magnitude important in graphics and games?
Vector magnitude is essential in graphics and games for calculating distances between objects (collision detection), normalizing vectors for lighting calculations, determining movement speed, and scaling forces. For instance, to move a character at constant speed regardless of direction, you normalize the direction vector and multiply by the desired speed.