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.
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
- Gradient is a hill. The arrows point uphill, the steepest way. At the very top they shrink to nothing, because the top is flat.
- Divergence is a tap or a plughole. Around a tap, arrows spread outwards (a source). Around a plughole, they all point in (a sink).
- Curl is a whirlpool. The arrows go round and round. A little paddle wheel dropped in it would spin.
How a robot uses it
- Make the goal a plughole: every arrow points towards it.
- Make obstacles push arrows away, like a magnet pushing another magnet.
- The robot just follows the arrow where it's standing. No map, no search.
- 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.
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.
- Gradient = direction of steepest increase. Its size is how steep. Zero at peaks, valleys and flat saddles.
- Divergence = net outflow per unit area. Positive: a source. Negative: a sink. Zero: what flows in flows out.
- Curl (in 2D, one number) = how much the field rotates a tiny paddle wheel. Positive: anticlockwise.
- Two facts tie them together: a gradient field never swirls (curl of a gradient is 0), and a pure swirl never spreads (div of curl is 0). So "go downhill" can't, on its own, walk you around an obstacle.
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.
- Potential-field navigation (Khatib, 1986): the goal attracts, obstacles repel, the robot follows the combined field. Cheap and reactive, used in arms, drones and game AI. Its known weakness is local minima, which this page shows and fixes.
- Escaping local minima with rotation: adding a tangential (curl) component around obstacles is a standard fix (often called vortex or circulation fields). Planners like A* (Robo-Helper week 4) don't have this problem, but need a map and a search first.
- Gradient descent: week 2 fits the braking distance by stepping against the gradient; Module 21 adds momentum.
- Flow matching and diffusion (Module 21): π0 and GR00T's action heads learn a velocity field that carries random noise to a smooth arm motion. That's a learned vector field, and divergence appears directly in the maths of how probability flows along it.
- Drones and underwater robots: wind and water currents are vector fields; divergence and curl describe where air or water piles up and where it spins.
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)
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:
- Attract: (−(x − 9), −(y − 5)), the downhill direction of a bowl centred on the goal. Divergence −2, curl 0.
- Repel: near each obstacle, a push straight away from it that grows fast as you get close (the classic Khatib formula, active within 2 m of the edge).
- Swirl (optional): the repel push turned 90°. It slides the robot around the obstacle. It has curl, so it can do what no gradient can.
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
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
- To feel a field, draw arrows on a grid of points (the demo, or a few lines of Python).
- 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.
- To steer a robot reactively: attract to the goal, repel from obstacles, cap the speed, follow the field.
- Test from many starts, not one. Count how often it gets stuck. One run tells you nothing.
- 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
| Term | Meaning |
|---|---|
| Vector field | An arrow (a vector) at every point of a space. |
| Potential | A 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. |
| Contour | A line of equal height. The gradient crosses it at a right angle. |
| Local minimum | A dip that isn't the lowest point: following the gradient gets stuck there. |
| Potential-field planner | Drive by following attract + repel fields, without a map or search. |
- The post, with its animation
- 3Blue1Brown, Divergence and curl: the language of Maxwell's equations, fluid flow, and more
- Khan Academy, Multivariable calculus (gradient, divergence, curl units)
- O. Khatib (1986), “Real-Time Obstacle Avoidance for Manipulators and Mobile Robots”, International Journal of Robotics Research 5(1): the original potential-field paper
- Related: Module 21 (A*, Kalman, momentum, diffusion: interactive), Robo-Helper week 4 (A*), week 2 (gradient descent), Module 02 (maths drills)