TExcelTAD
Home › Calculus in Depth › Vector Fields

Vector Fields

A vector field attaches an arrow to every point of a region: a wind velocity at each spot on a map, a force at each point in space, a steering direction at each tile of a game level. This lesson gives the precise definition, shows how to draw and compute fields in Python, introduces gradient (conservative) fields and their potential functions, and builds a small potential-field steering demo of the kind game AI and robots use.

Definition

Let DD be a set in R2\mathbb{R}^2. A vector field on DD is a function F\mathbf{F} that assigns to each point (x,y)(x, y) in DD a two-dimensional vector F(x,y)\mathbf{F}(x, y). In components,

F(x,y)=P(x,y) i+Q(x,y) j=⟨P(x,y),Q(x,y)⟩\mathbf{F}(x, y) = P(x, y)\,\mathbf{i} + Q(x, y)\,\mathbf{j} = \langle P(x, y), Q(x, y) \rangle

where PP and QQ are ordinary scalar functions, called the component functions. A vector field on a set EE in R3\mathbb{R}^3 is the same idea with three components: F(x,y,z)=⟨P,Q,R⟩\mathbf{F}(x, y, z) = \langle P, Q, R \rangle. We call F\mathbf{F} continuous (or differentiable, or C1C^1) when its component functions are.

A useful mental picture: at a sample of points you draw the arrow F(x,y)\mathbf{F}(x, y) with its tail at (x,y)(x, y). The arrows are usually scaled down so they do not overlap; only their relative lengths and directions matter for the picture.

Field Formula What it looks like
Uniform flow ⟨2,1⟩\langle 2, 1 \rangle identical arrows everywhere
Rotation (counter-clockwise) ⟨−y,x⟩\langle -y, x \rangle circles around the origin, speed grows with radius
Source ⟨x,y⟩\langle x, y \rangle arrows point straight out from the origin
Inverse-square (gravity toward origin) −⟨x,y,z⟩/(x2+y2+z2)3/2-\langle x, y, z \rangle / (x^2 + y^2 + z^2)^{3/2} arrows point in, length 1/r21/r^2
Shear ⟨y,0⟩\langle y, 0 \rangle horizontal arrows, faster higher up

The rotation field is perpendicular to the position vector, since ⟨−y,x⟩⋅⟨x,y⟩=0\langle -y, x \rangle \cdot \langle x, y \rangle = 0, so every arrow is tangent to the circle through its point. Its length is x2+y2=r\sqrt{x^2 + y^2} = r.

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

def F(x, y):                      # a rotation plus a weak outward push
    return -y + 0.3 * x, x + 0.3 * y

for p in [(1, 0), (0, 2), (-1, -1)]:
    P, Q = F(*p)
    print(f"F{p} = ({P:+.1f}, {Q:+.1f})")

x, y = np.meshgrid(np.linspace(-2, 2, 13), np.linspace(-2, 2, 13))
P, Q = F(x, y)
plt.figure(figsize=(5, 5))
plt.quiver(x, y, P, Q, np.hypot(P, Q), cmap="viridis")
plt.gca().set_aspect("equal")
plt.title("F = ⟨−y + 0.3x, x + 0.3y⟩: a spiral source")
F(1, 0) = (+0.3, +1.0)
F(0, 2) = (-2.0, +0.6)
F(-1, -1) = (+0.7, -1.3)

The arrows swirl counter-clockwise while drifting outward, so a particle carried by this flow spirals away from the origin.

Gradient fields

If ff is a differentiable function of two or three variables, its gradient is a vector field:

∇f(x,y,z)=⟨fx,fy,fz⟩\nabla f(x, y, z) = \langle f_x, f_y, f_z \rangle

Two facts from that lesson give the picture: ∇f\nabla f points in the direction in which ff increases fastest, and it is perpendicular to the level curves (or level surfaces) of ff. So a gradient field crosses the contour lines of a map at right angles, pointing uphill.

A vector field F\mathbf{F} is called conservative if it is the gradient of some scalar function, F=∇f\mathbf{F} = \nabla f. Then ff is called a potential function for F\mathbf{F}. (Physicists usually write F=−∇U\mathbf{F} = -\nabla U with a potential energy UU; the minus sign only flips "uphill" to "downhill".) Not every field is conservative: the next lessons give a test, and show why conservative fields are special for line integrals.

python · try it yourself
import sympy as sp

x, y, z = sp.symbols('x y z')
f = x**2 * y - sp.exp(z) * y + 3 * z
grad = [sp.diff(f, v) for v in (x, y, z)]
print("grad f =", grad)
print("at (1, 2, 0):", [g.subs({x: 1, y: 2, z: 0}) for g in grad])

# a quick check: is <2xy, x**2 - e^z, -y e^z + 3> really this gradient?
F = [2*x*y, x**2 - sp.exp(z), -y*sp.exp(z) + 3]
print("matches:", all(sp.simplify(a - b) == 0 for a, b in zip(F, grad)))
grad f = [2*x*y, x**2 - exp(z), -y*exp(z) + 3]
at (1, 2, 0): [4, 0, 1]
matches: True

SymPy prints x**2 for x2x^2 and exp(z) for eze^z. At (1,2,0)(1, 2, 0) the gradient ⟨4,0,1⟩\langle 4, 0, 1 \rangle says that ff grows fastest in a direction mostly along xx and slightly up in zz.

Flow lines

A flow line (streamline) of F\mathbf{F} is a curve r(t)\mathbf{r}(t) whose velocity is the field: r′(t)=F(r(t))\mathbf{r}'(t) = \mathbf{F}(\mathbf{r}(t)). For the pure rotation ⟨−y,x⟩\langle -y, x \rangle the flow lines are circles r(t)=⟨Rcos⁡t,Rsin⁡t⟩\mathbf{r}(t) = \langle R \cos t, R \sin t \rangle, since r′=⟨−Rsin⁡t,Rcos⁡t⟩=⟨−y,x⟩\mathbf{r}' = \langle -R \sin t, R \cos t \rangle = \langle -y, x \rangle. For a gradient field the flow lines are paths of steepest ascent. Tracing flow lines numerically is just solving an ODE, the same Euler step you have seen for direction fields.

In software

  • Potential-field steering. Game AI and mobile robots often steer by a potential U(x,y)=(attraction to the goal)+(repulsion from obstacles)U(x, y) = (\text{attraction to the goal}) + (\text{repulsion from obstacles}), moving a little along −∇U-\nabla U every frame. The agent slides downhill to the goal and around obstacles without any path search. The weakness is local minima, places where the forces cancel short of the goal, which is why games combine it with A* paths.
  • Flow fields for crowds. Strategy games precompute one vector per grid cell pointing along the shortest path to a target; thousands of units then just read the arrow under their feet.
  • Fluids, wind and particles. A fluid solver stores a velocity field on a grid; particles, smoke and foliage are advected by sampling it. See the Curl and Divergence lesson for what makes such a field physical.
  • ML. Gradient descent follows the flow lines of −∇L-\nabla L, the negative gradient field of the loss.
python · try it yourself
import numpy as np

goal = np.array([8.0, 0.0])
obstacle, radius = np.array([4.0, 0.3]), 1.0

def grad_U(p):
    g = 2 * (p - goal) * 0.05                         # attraction: U = 0.05·|p − goal|²
    d = p - obstacle
    dist = np.linalg.norm(d)
    if dist < 3 * radius:                             # repulsion only near the obstacle
        g -= 4.0 * d / dist**4                        # U = 2/|d|², ∇U = −4·d/|d|⁴
    return g

p = np.array([0.0, 0.0])
for step in range(200):
    p = p - 0.5 * grad_U(p)                           # move downhill along −∇U
    if step % 40 == 39:
        print(f"step {step + 1:3d}: ({p[0]:.2f}, {p[1]:.2f})  "
              f"distance to obstacle {np.linalg.norm(p - obstacle):.2f}")
step  40: (5.94, -2.03)  distance to obstacle 3.03
step  80: (7.73, -0.26)  distance to obstacle 3.78
step 120: (7.97, -0.03)  distance to obstacle 3.98
step 160: (8.00, -0.00)  distance to obstacle 4.01
step 200: (8.00, -0.00)  distance to obstacle 4.01

The agent starts heading straight for the goal, gets pushed below the obstacle (its yy coordinate goes negative) and then settles on the goal at (8,0)(8, 0).

Exercises

Exercise 1: Read the arrows

For F(x,y)=⟨y,−x⟩\mathbf{F}(x, y) = \langle y, -x \rangle, compute F\mathbf{F} at (1,0)(1, 0), (0,1)(0, 1) and (−2,0)(-2, 0). Which way do the arrows circulate, and how long is F(x,y)\mathbf{F}(x, y) in terms of r=x2+y2r = \sqrt{x^2 + y^2}?

Answer

⟨0,−1⟩\langle 0, -1 \rangle, ⟨1,0⟩\langle 1, 0 \rangle, ⟨0,2⟩\langle 0, 2 \rangle; clockwise; ∣F∣=r\lvert \mathbf{F} \rvert = r.

Solution
F(1,0)=⟨0,−1⟩(at the right of the circle, pointing down)F(0,1)=⟨1,0⟩(at the top, pointing right)F(−2,0)=⟨0,2⟩(at the left, pointing up)\begin{aligned} \mathbf{F}(1, 0) &= \langle 0, -1 \rangle && \text{(at the right of the circle, pointing down)} \\ \mathbf{F}(0, 1) &= \langle 1, 0 \rangle && \text{(at the top, pointing right)} \\ \mathbf{F}(-2, 0) &= \langle 0, 2 \rangle && \text{(at the left, pointing up)} \end{aligned}

Right → down, top → right, left → up: that is clockwise. ∣F∣=y2+x2=r\lvert \mathbf{F} \rvert = \sqrt{y^2 + x^2} = r, and F⋅⟨x,y⟩=xy−yx=0\mathbf{F} \cdot \langle x, y \rangle = xy - yx = 0, so the arrows are tangent to circles about the origin.

Exercise 2: A gradient field

Find the gradient field of f(x,y)=x⋅exyf(x, y) = x \cdot e^{xy} and evaluate it at (1,0)(1, 0).

Answer

∇f=⟨(1+xy)exy,x2exy⟩\nabla f = \langle (1 + xy)e^{xy}, x^2 e^{xy} \rangle; ∇f(1,0)=⟨1,1⟩\nabla f(1, 0) = \langle 1, 1 \rangle.

Solution
fx=exy+x⋅yexy=(1+xy) exy(product rule)fy=x⋅xexy=x2exyat (1,0): e0=1, so ∇f=⟨1+0,1⟩=⟨1,1⟩\begin{aligned} f_x &= e^{xy} + x \cdot y e^{xy} = (1 + xy)\,e^{xy} && \text{(product rule)} \\ f_y &= x \cdot x e^{xy} = x^2 e^{xy} \\ &\text{at } (1, 0)\text{: } e^0 = 1, \text{ so } \nabla f = \langle 1 + 0, 1 \rangle = \langle 1, 1 \rangle \end{aligned}

Exercise 3: Find a potential

Find a function ff with ∇f=⟨2x+3y,3x−4y⟩\nabla f = \langle 2x + 3y, 3x - 4y \rangle.

Answer

f(x,y)=x2+3xy−2y2+Cf(x, y) = x^2 + 3xy - 2y^2 + C.

Solution
fx=2x+3y  ⟹  f=x2+3xy+g(y)fy=3x+g′(y) must equal 3x−4y  ⟹  g′(y)=−4y  ⟹  g(y)=−2y2+Cf=x2+3xy−2y2+C\begin{aligned} &f_x = 2x + 3y \implies f = x^2 + 3xy + g(y) \\ &f_y = 3x + g'(y) \text{ must equal } 3x - 4y \implies g'(y) = -4y \implies g(y) = -2y^2 + C \\ &f = x^2 + 3xy - 2y^2 + C \end{aligned}

Check: fx=2x+3yf_x = 2x + 3y and fy=3x−4yf_y = 3x - 4y.

Exercise 4: Inverse-square field (code)

For the gravity-like field F=−⟨x,y,z⟩/(x2+y2+z2)3/2\mathbf{F} = -\langle x, y, z \rangle / (x^2 + y^2 + z^2)^{3/2}, compute ∣F∣\lvert \mathbf{F} \rvert at (0,3,4)(0, 3, 4) by hand, then write code that evaluates the field there and prints its length.

Answer

∣F∣=125=0.04\lvert \mathbf{F} \rvert = \frac{1}{25} = 0.04, and F(0,3,4)=⟨0,−0.024,−0.032⟩\mathbf{F}(0, 3, 4) = \langle 0, -0.024, -0.032 \rangle.

Solution

At (0,3,4)(0, 3, 4) the distance is r=5r = 5, and ∣F∣=r/r3=1/r2=125\lvert \mathbf{F} \rvert = r / r^3 = 1/r^2 = \frac{1}{25}. The direction is −⟨0,3,4⟩/5-\langle 0, 3, 4 \rangle / 5, so F=125⋅⟨0,−0.6,−0.8⟩=⟨0,−0.024,−0.032⟩\mathbf{F} = \frac{1}{25} \cdot \langle 0, -0.6, -0.8 \rangle = \langle 0, -0.024, -0.032 \rangle.

python · try it yourself
import numpy as np

def F(p):
    p = np.asarray(p, dtype=float)
    return -p / np.linalg.norm(p)**3

v = F([0, 3, 4])
print([round(float(c), 6) for c in v], round(float(np.linalg.norm(v)), 6))
[-0.0, -0.024, -0.032] 0.04

The -0.0 is a negative floating-point zero (−0/125-0/125), equal to 0.

Test yourself

A vector field F\mathbf{F} on R2\mathbb{R}^2 is…

  1. a function that assigns a vector to each point of the plane
  2. a single vector in the plane
  3. a function that assigns a number to each vector
  4. a curve whose tangent is a vector

F\mathbf{F} is called conservative when…

  1. ∣F∣\lvert \mathbf{F} \rvert is constant
  2. F\mathbf{F} is perpendicular to the position vector
  3. F=∇f\mathbf{F} = \nabla f for some scalar function ff
  4. F\mathbf{F} has no zeros

In potential-field steering an agent moves along −∇U-\nabla U each frame. What can stop it short of the goal?

  1. Nothing, it always arrives
  2. A local minimum of UU where the attractive and repulsive forces cancel
  3. The gradient being too large
  4. UU being a polynomial