Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 68 ยท Multivariable Calculus

Vectors in 2D and 3D

Learn vectors in two and three dimensions: dot and cross products, projections, lines and planes, with numpy and sympy examples you can run and change.

Single-variable calculus follows one number along a line. The real world has more directions: forces push sideways, velocities point somewhere, heat flows downhill in three dimensions. A vector is the tool for this. It carries both a size (its magnitude) and a direction, and it is written as a list of components such as (3, 4) or (1, 2, 2).

Every later idea in this chapter, from partial derivatives to the gradient to surface integrals, is built from the handful of operations in this lesson. You will learn the two products of vectors (the dot product and the cross product), how to project one vector onto another, and how to describe lines and planes. Each idea comes with numpy code, and sympy handles the exact versions.

flowchart TD
    V["Vector a, b"] --> D["Dot product<br/>a . b = number"]
    V --> C["Cross product<br/>a x b = vector (3D only)"]
    D --> A["Angle, projection,<br/>work, test for perpendicular"]
    C --> N["Normal vector, area,<br/>torque, plane equation"]

The dot product returns a number that measures alignment; the cross product returns a vector perpendicular to both inputs.

Vectors, length and direction

A vector in 3D has three components, a = (a1, a2, a3). You add vectors component by component and scale them by multiplying every component. Geometrically, adding vectors places them tip to tail.

The magnitude (length) comes from Pythagoras: |a| = sqrt(a1**2 + a2**2 + a3**2). A vector with magnitude 1 is a unit vector, and dividing any non-zero vector by its length gives a unit vector with the same direction.

Python Vector arithmetic with numpy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The vector (1, 2, 2) has length exactly 3, because 1 + 4 + 4 = 9. That is a handy example to remember.

The dot product

The dot product multiplies matching components and adds them:

a . b = a1*b1 + a2*b2 + a3*b3.

The result is a single number, and it has a second, geometric formula:

a . b = |a| |b| cos(theta),

where theta is the angle between the vectors. Combining the two formulas lets you find any angle: cos(theta) = (a . b) / (|a| |b|).

Three facts are worth memorizing.

  • If a . b = 0 and neither vector is zero, the vectors are perpendicular (orthogonal).
  • If a . b > 0, the angle is less than 90 degrees; if a . b < 0, it is greater.
  • a . a = |a|**2, so the dot product also gives lengths.

In physics, the work done by a constant force F over a displacement d is F . d. Only the part of the force along the motion does any work.

Python Angle between two vectors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Here a . b = 4, |a| = 3 and |b| = sqrt(5), so cos(theta) = 4 / (3 sqrt(5)), about 0.5963, and the angle is about 53.4 degrees.

Projections

Shine a light straight down on a and let it cast a shadow on the line through b. That shadow is the projection of a onto b:

proj_b(a) = ((a . b) / (b . b)) * b.

The leftover part, a - proj_b(a), is perpendicular to b. So every vector splits into a piece parallel to b and a piece perpendicular to b. This decomposition is used everywhere: resolving a force along a ramp, removing the component of motion along a wall, and fitting lines with least squares.

Python Split a vector into parallel and perpendicular parts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The dashed line meets the line of b at a right angle, which is the picture behind the formula.

The cross product

In three dimensions there is a second product. The cross product a x b takes two vectors and returns a third vector that is perpendicular to both:

a x b = (a2*b3 - a3*b2, a3*b1 - a1*b3, a1*b2 - a2*b1).

Its direction follows the right-hand rule: curl the fingers of your right hand from a toward b, and your thumb points along a x b. Its magnitude is |a| |b| sin(theta), which equals the area of the parallelogram spanned by the two vectors.

Because of the right-hand rule, order matters: b x a = -(a x b). Parallel vectors give the zero vector, since sin(0) = 0. Unlike the dot product, the cross product exists only in 3D (2D vectors can be treated as having a third component of 0).

Python Cross product, area and perpendicularity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The result (2, 3, -4) is perpendicular to both inputs, as the two zero dot products confirm. Its length is sqrt(29), about 5.385, the area of the parallelogram.

The cross product is also how you compute torque: tau = r x F, where r is the position of the point where the force F is applied. A force pushing straight along r gives zero torque.

Lines in space

A line needs a point and a direction. If it passes through r0 with direction d, then every point on it is

r(t) = r0 + t*d,

where the parameter t runs over all real numbers. This is a different style from y = mx + b, and it works in any dimension. In 3D there is no single equation for a line, so the parametric form is the standard one.

Planes

A plane is fixed by one point P and one normal vector n that is perpendicular to the plane. A point X is on the plane exactly when the vector from P to X is perpendicular to n:

n . (X - P) = 0, or in components n1*x + n2*y + n3*z = d, with d = n . P.

If you only know three points P, Q, R on the plane, build two edge vectors Q - P and R - P, and take their cross product to get a normal.

The distance from a point X to the plane is |n . (X - P)| / |n|: project the vector X - P onto the unit normal.

Python Plane through three points, distance, line intersection

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The intersection step is the key trick: substitute r(t) into the plane equation, n . (r0 + t*d) = d_plane, and solve the one linear equation for t. Here 11 t = 6, so t = 6/11.

Exact vectors with sympy

numpy works with floating-point numbers. When you want exact results, or vectors with symbols in them, sympy.Matrix has .dot(), .cross() and .norm() methods.

Python Symbolic vectors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Using a symbol turns a geometry question ("when are these perpendicular?") into an equation you can solve. You will use the same pattern when you look for critical points in Optimization in Several Variables.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Two non-zero vectors have a dot product of 0. What does this tell you?

    Since a . b = |a||b|cos(theta), a zero dot product with non-zero vectors forces cos(theta) = 0, so the angle is 90 degrees.
  2. 2. What kind of object is a x b for vectors in 3D?

    The cross product returns a vector whose direction is given by the right-hand rule and which is perpendicular to both inputs. The dot product is the one that returns a number.
  3. 3. If a = (1, 2, 2), what is |a|?

    |a| = sqrt(1 + 4 + 4) = sqrt(9) = 3. The value 9 is the squared length.
  4. 4. What does the magnitude |a x b| equal geometrically?

    |a x b| = |a||b| sin(theta), which is base times height of the parallelogram built from the two vectors.
  5. 5. How do you get a normal vector for a plane through three points P, Q, R?

    Q - P and R - P lie in the plane, so their cross product is perpendicular to the plane. The dot product would only give a number.

Summary

  • A vector has magnitude and direction; its length is sqrt of the sum of squared components.
  • The dot product a . b is a number: |a||b|cos(theta). Zero means perpendicular. It gives angles and projections.
  • proj_b(a) = ((a . b)/(b . b)) b; the remainder a - proj_b(a) is perpendicular to b.
  • The cross product a x b is a vector perpendicular to both; its length is the parallelogram area, and b x a = -(a x b).
  • A line is r0 + t*d; a plane is n . (X - P) = 0, with n found from a cross product.
  • numpy handles numeric vectors; sympy.Matrix gives exact and symbolic results.

Next: Partial Derivatives.