LLM + RoboticsModule 22 · Vector fields

Gradient, divergence and curl: three ways a field of arrows can move, and how a robot steers with them

Uphill, outwards and round and round. The same three ideas train neural networks, describe flows, and drive a robot around a sofa without a map.

Source: Matheorems, “Gradient, Divergence and Curl”, 8 Nov 2026 (X post with an animation) · X post. The post is a short animation with three worked examples; the robot experiment on this page is ours.

Start here (no maths needed)

Imagine an arrow painted on every spot of the floor. Drop a marble anywhere and it rolls the way the arrows point. That floor of arrows is a "vector field".

Think of it like this

How a robot uses it

  1. Make the goal a plughole: every arrow points towards it.
  2. Make obstacles push arrows away, like a magnet pushing another magnet.
  3. The robot just follows the arrow where it's standing. No map, no search.
  4. Problem: right in front of a wall, "go to the goal" and "get away from the wall" cancel out. The robot freezes. Fix: add a little whirlpool around obstacles so it slides around them.

Why robots care

This is how many simple robots, drones and game characters dodge things in real time. And "gradient" is the same word as in gradient descent, the way every neural network (including the ones in this course) learns.

Try it

In the demo below, pick "Robot: the sofa", press "Drive" without swirl and watch the robots get stuck. Then tick "swirl" and drive again.

Words to know

Vector field
An arrow at every point.
Source / sink
Where arrows spread out / where they all point in.
Local minimum
A spot where every direction looks worse, even though it isn't the goal.

Going deeper: the three formulas from the post, checked numerically; a potential-field robot in Python with measured results; and where these ideas show up in robotics and in machine learning.

In this module
  1. Summary
  2. Why this matters for robotics
  3. Deep dive: gradient
  4. Deep dive: divergence
  5. Deep dive: curl
  6. Steering Robo-Helper with a field (interactive)
  7. Playbook
  8. Self-check
  9. Glossary & further reading

1. Summary

TL;DR

The post animates three 2D vector fields. Gradient: for φ = 1 − x² − 2y², ∇φ = (−2x, −4y): tracers flow uphill, cross every contour at a right angle, and the arrow shrinks to zero at the peak. Divergence: F = (x, y) spreads out everywhere with div F = 2 (a source); F = (−x, −y) has div F = −2 (a sink). Curl: F = (−y, x) rotates around the centre with curl F = 2 (out of the screen); reversed, −2. We check all six numbers in Python, then use the ideas to steer a robot: following a gradient field alone gets stuck behind a sofa from 9 of 11 starts; adding a swirl (curl) around obstacles reaches the goal from all 11.

2. Why this matters for robotics

Track: LLM + Robotics. The gradient is the heart of machine learning: every model in this course, from Robo-Helper's neuron to a Transformer, learns by following a gradient downhill. Divergence and curl are the language of flows, which robots meet in navigation, in fluids (drones, underwater robots) and in the newest robot brains.

3. Deep dive: gradient

Take a height map φ(x, y). The gradient ∇φ = (∂φ/∂x, ∂φ/∂y) is the vector of how fast φ changes as you move a tiny bit in x and in y. It points in the steepest uphill direction, and its length is the steepness.

For the post's hill φ = 1 − x² − 2y²: ∂φ/∂x = −2x and ∂φ/∂y = −4y, so ∇φ = (−2x, −4y). The y slope is twice as steep, because of the 2 in front of y². Every arrow crosses the contour lines (lines of equal height) at a right angle: walking along a contour you neither climb nor fall, so the uphill direction must be straight across it. At the top (0, 0) both parts are zero, which is why the post's red probe slows to a stop there.

Machine learning walks the other way: a model's error is a height map over its weights, and training takes small steps against the gradient, downhill. That's every w -= lr * dw in Robo-Helper.

4. Deep dive: divergence

div F = ∂Fx/∂x + ∂Fy/∂y measures how much more flows out of a tiny box than flows in. For F = (x, y): 1 + 1 = 2, the same everywhere, so every point is a little source and the flux out through any circle is positive. Flip it to (−x, −y) and it's −2: a sink, and the flux through the circle turns around, exactly as the post animates.

For a robot, the "go to the goal" field (−(x − gx), −(y − gy)) has divergence −2 everywhere: everything drains towards the goal like water into a plughole.

5. Deep dive: curl

In 2D, curl F = ∂Fy/∂x − ∂Fx/∂y. For F = (−y, x): 1 − (−1) = 2. Positive means anticlockwise, which in 3D is an arrow pointing out of the screen (right-hand rule). The post's paddle wheel, carried around the centre, spins exactly once per orbit. Reverse the field to (y, −x) and the curl is −2: clockwise, into the screen.

The key fact for robots: the gradient of anything has zero curl. If you only ever follow "downhill", you can never circle round something. To go around an obstacle you need a field that swirls.

python3 bonus_vector_fields.py    (Robo-Helper project, solutions or homework folder)

1) THE POST'S THREE FIELDS, checked numerically at a few points:
   grad phi at (0.5, 0.5) = (-1.000, -2.000)   formula (-2x, -4y) = (-1, -2)
   grad phi at the top (0, 0) = (0.000, 0.000)   zero: a peak is flat
   div (x, y)    = 2.000   (source)
   div (-x, -y)  = -2.000   (sink)
   curl (-y, x)  = 2.000   (anticlockwise, out of the screen)
   curl (y, -x)  = -2.000   (clockwise, into the screen)

The derivatives are worked out numerically (a tiny step either side), so the check doesn't rely on the formulas it's checking.

6. Steering Robo-Helper with a field

A different way to drive than week 4's A*. The robot is a point in a 10 m × 10 m room with the goal at (9, 5). Three fields add up:

2) THE ROBOT'S FIELD: goal at (9, 5), a box of radius 1.2 at (5, 5).
   div of the pull towards the goal = -2.00  (negative everywhere: the goal is a sink)
   curl of the pull = 0.00  (a pure gradient field never swirls)
   curl of the swirl just above the box, at (5, 7) = -28.71  (not zero: it really swirls)

3) DRIVING: 11 starts on the left, follow the arrows to the goal. (Straight line: 8.0 m.)
   one box:
     gradient only    reached the goal from 10/11 starts, average path 9.1 m
     gradient + swirl reached the goal from 11/11 starts, average path 10.0 m
   a sofa (3 circles in a wall):
     gradient only    reached the goal from  2/11 starts, average path 9.8 m
     gradient + swirl reached the goal from 11/11 starts, average path 11.2 m

With one round box, only the start exactly in line with the goal gets stuck: there, "go right" and "get pushed left" cancel perfectly. A wall is much worse. Behind the sofa, 9 of 11 starts slide into the dip in front of it and stop. The swirl fixes every one, at the cost of longer paths (it always goes round the same way, even when the other way is shorter).

Interactive: the fields, and a robot that follows them

Pick a field to see its arrows. The robot scenes use exactly the Python code's fields and driving rule (checked in node: same 10/11, 11/11, 2/11 and 11/11). ■ reached the goal   ■ got stuck

Reached goal
–
Average path
–
div / curl at a point
–

Challenge: in bonus_vector_fields.py, make the swirl choose its direction: turn left or right depending on which side of the obstacle the goal is. Does the sofa's average path get shorter than 11.2 m, and does it still reach 11/11?

Challenge 2: put the cat back. Make it an obstacle that moves 0.3 m/s up and down in front of the sofa. Potential fields react instantly; A* would have to replan. Which copes better?

7. Playbook

  1. To feel a field, draw arrows on a grid of points (the demo, or a few lines of Python).
  2. To check a formula, compare it with a numerical derivative (a tiny step either side). That's how Robo-Helper's attention stretch checked its backprop too.
  3. To steer a robot reactively: attract to the goal, repel from obstacles, cap the speed, follow the field.
  4. Test from many starts, not one. Count how often it gets stuck. One run tells you nothing.
  5. If it gets stuck, add rotation (curl) around obstacles, or fall back to a planner like A*. Measure the cost in path length.

8. Self-check

For φ = 1 − x² − 2y², why is the arrow at (0, 1) twice as long as the one at (1, 0)?

∇φ(0, 1) = (0, −4), length 4. ∇φ(1, 0) = (−2, 0), length 2. The hill is steeper in y because of the 2 in front of y².

F = (x, y) has divergence 2 everywhere, even far from the centre. Why isn't it only a source at the centre?

Divergence is local: around any tiny box, a little more flows out than in, because the arrows get longer as you move outwards. Every point adds a bit of outflow.

Why can't "follow the gradient" ever circle around an obstacle?

A gradient field has zero curl, and walking downhill you can never return to the same height, so you can't loop. Around a wall, downhill can lead into a dip that isn't the goal (a local minimum), and you stay there.

The swirl reached the goal every time but made paths longer. Why?

It always turns the same way around obstacles, even when the other way would be shorter. A smarter swirl picks its direction from where the goal is (that's the first challenge).

Where do vector fields show up in robot brains like π0?

Their action heads use flow matching: a network learns a velocity field that carries random noise to a realistic arm motion, then the robot follows it in a few steps, a learned field instead of a hand-made one.

9. Glossary & further reading

TermMeaning
Vector fieldAn arrow (a vector) at every point of a space.
PotentialA height map whose gradient gives the field.
Gradient ∇φ(∂φ/∂x, ∂φ/∂y): steepest uphill direction; length = steepness.
Divergence∂Fx/∂x + ∂Fy/∂y: outflow per area. + source, − sink.
Curl (2D)∂Fy/∂x − ∂Fx/∂y: rotation. + anticlockwise.
ContourA line of equal height. The gradient crosses it at a right angle.
Local minimumA dip that isn't the lowest point: following the gradient gets stuck there.
Potential-field plannerDrive by following attract + repel fields, without a map or search.