Part I · 第 3 章 多元与向量微积分 · 3.2Part I · Ch. 3 Multivariable & Vector Calculus · 3.2

梯度与方向导数Gradient & Directional Derivative

站在山坡上,朝东走是上坡,朝北走是下坡,朝东北走又是另一种坡度:坡度取决于方向。方向导数回答"朝这个方向走,坡度是多少",梯度把所有方向的答案压缩成一个向量:它指向最陡的上坡,长度就是那里的最大坡度,并且总是垂直于等高线。从优化、势场导航到神经网络训练,机器人学里"往哪边走最划算"的问题,答案几乎都是梯度。 Stand on a hillside: east is uphill, north is downhill, northeast is yet another slope, so the slope depends on the direction. The directional derivative answers "how steep is it if I walk this way?", and the gradient packs every answer into one vector: it points up the steepest slope, its length is the maximum slope there, and it is always perpendicular to the contour line. From optimization to potential-field navigation to neural-network training, the robotics question "which way pays off most?" is answered by the gradient.

约 20 分钟~20 min 前置:1.7 点积 · 3.1 多元函数与偏导Prereqs: 1.7 Dot Product · 3.1 Partial Derivatives 4 个交互4 interactives

    1为什么机器人需要它Why robots need this

    上一节的偏导数只回答了"沿 \(x\) 轴"和"沿 \(y\) 轴"两个方向。真实的机器人想知道的是任意方向:

    • 势场导航:地图上每一点有一个"代价",终点最低、障碍物附近很高。机器人朝代价下降最快的方向挪一步,就是沿着负梯度走。
    • 优化与学习:逆运动学、标定、轨迹优化、训练神经网络,都是"调整参数让代价变小",每一步沿着负梯度(6.3 节)。
    • 感知:图像梯度给出边缘的方向;SDF(有符号距离场)的梯度指向离最近障碍物最远的方向,是避障的常用信息。
    • 灵敏度:灵敏度分析问"参数朝哪个方向变化,对输出影响最大",答案还是梯度。

    这一节把"每个坐标方向的坡度"升级成"任意方向的坡度",并说明为什么梯度这个小小的向量能回答所有这些问题。

    The partial derivatives of the last lesson answered only two directions, "along \(x\)" and "along \(y\)". A real robot wants to know about any direction:

    • Potential-field navigation: every map point has a "cost", lowest at the goal and high near obstacles. The robot steps in the direction of fastest cost decrease, which is along the negative gradient.
    • Optimization and learning: inverse kinematics, calibration, trajectory optimization and neural-network training all "adjust parameters to shrink a cost", each step along the negative gradient (Lesson 6.3).
    • Perception: the image gradient gives edge directions; the gradient of an SDF (signed distance field) points away from the nearest obstacle, a staple of collision avoidance.
    • Sensitivity: "in which direction does changing the parameters affect the output most?" is, again, the gradient.

    This lesson upgrades "slope along each coordinate axis" to "slope along any direction", and explains why one small vector can answer all these questions.

    2直觉:绕着脚下转一圈Intuition: turn around on the spot

    想象站在山坡上的点 \(\mathbf p\),原地转一圈,每个朝向 \(\mathbf u\)(单位向量)都有一个脚下的坡度,叫做方向导数 \(D_{\mathbf u}f(\mathbf p)\):沿着 \(\mathbf p + t\mathbf u\) 走,\(f\) 对 \(t\) 的变化率。转一圈的过程中,坡度有一个最大值(最陡上坡的方向)、一个最小值(最陡下坡,恰好是反方向),以及两个正好为 0 的朝向:沿着等高线走,高度不变。

    这条坡度曲线形状非常规整:它是一条余弦曲线。峰值的位置就是梯度的方向,峰值的高度就是梯度的长度。下面亲手验证。

    Imagine standing at a point \(\mathbf p\) on a hillside and turning around on the spot. Each heading \(\mathbf u\) (a unit vector) has its own slope underfoot, the directional derivative \(D_{\mathbf u}f(\mathbf p)\): the rate at which \(f\) changes with \(t\) as you walk along \(\mathbf p + t\mathbf u\). During the turn the slope has a maximum (the steepest uphill heading), a minimum (steepest downhill, exactly opposite), and two headings where it is exactly 0: walking along the contour line, the height does not change.

    The shape of this slope-versus-heading curve is strikingly regular: it is a cosine. The peak's position is the direction of the gradient, and its height is the gradient's length. Check it for yourself.

    3动手玩:方向导数罗盘Play: the directional-derivative compass

    左边是等高线图(越偏青色越低,越偏紫色越高)。拖动黄点 \(\mathbf p\),再拖动紫色手柄设定朝向 \(\mathbf u\)。琥珀色大箭头是梯度 \(\nabla f(\mathbf p)\),青色线是穿过 \(\mathbf p\) 的等高线切线。右边把方向导数 \(D_{\mathbf u}f\) 画成朝向角 \(\varphi\) 的函数,竖线标出当前的 \(\varphi\)。

    On the left is a contour map (more cyan is lower, more purple is higher). Drag the amber point \(\mathbf p\), then drag the purple handle to set the heading \(\mathbf u\). The big amber arrow is the gradient \(\nabla f(\mathbf p)\) and the cyan line is the tangent to the contour through \(\mathbf p\). On the right, the directional derivative \(D_{\mathbf u}f\) is plotted against the heading angle \(\varphi\), with a vertical line at the current \(\varphi\).

    FIG 1方向导数罗盘Directional-derivative compass
    等高线图 (x, y)CONTOUR MAP (x, y)
    D_u f 随朝向角 φ 变化D_u f vs. heading angle φ
    紫色虚线圆是朝向 \(\mathbf u\) 的所有取值(单位圆,半径画成 0.9),手柄在圆上。梯度箭头按 0.6 倍长度画出。读数中的"有限差分"是 \((f(\mathbf p+\varepsilon\mathbf u)-f(\mathbf p))/\varepsilon\)(\(\varepsilon=10^{-6}\))的直接数值,用来核对 \(\nabla f\cdot\mathbf u\)。 The purple dashed circle contains every possible heading \(\mathbf u\) (the unit circle, drawn with radius 0.9), and the handle sits on it. The gradient arrow is drawn at 0.6× length. The "finite difference" in the readout is the direct number \((f(\mathbf p+\varepsilon\mathbf u)-f(\mathbf p))/\varepsilon\) with \(\varepsilon=10^{-6}\), a check on \(\nabla f\cdot\mathbf u\).
    试一试Try this
    • 转动 \(\mathbf u\),让方向导数最大(右图曲线的峰顶)。此时 \(\mathbf u\) 和梯度箭头是什么关系?这个最大值等于什么?Turn \(\mathbf u\) until the directional derivative is maximal (the peak of the curve). How do \(\mathbf u\) and the gradient arrow relate? What does the maximum equal?
    • 让 \(D_{\mathbf u}f\approx 0\),但梯度并不为零(\(\|\nabla f\|>0.3\))。此时 \(\mathbf u\) 沿着什么线?Make \(D_{\mathbf u}f\approx 0\) while the gradient is nonzero (\(\|\nabla f\|>0.3\)). What kind of line is \(\mathbf u\) along?
    • 让方向导数取最小值。这时 \(\mathbf u\) 与梯度成多大的角?Make the directional derivative take its minimum. What angle does \(\mathbf u\) make with the gradient?
    核心洞见Key insight

    任何方向的坡度都是梯度在这个方向上的投影:\(D_{\mathbf u}f = \nabla f\cdot\mathbf u = \|\nabla f\|\cos\varphi\)(点积,1.7 节)。所以:梯度方向最陡(\(\varphi=0\),坡度 \(\|\nabla f\|\));反方向最陡下降(\(\varphi=\pi\));垂直方向(\(\varphi=\pm\pi/2\))坡度为 0,那就是等高线的方向。

    The slope in any direction is the projection of the gradient onto that direction: \(D_{\mathbf u}f = \nabla f\cdot\mathbf u = \|\nabla f\|\cos\varphi\) (the dot product, Lesson 1.7). So: along the gradient the slope is steepest (\(\varphi=0\), slope \(\|\nabla f\|\)); opposite to it is the steepest descent (\(\varphi=\pi\)); perpendicular to it (\(\varphi=\pm\pi/2\)) the slope is 0, which is the direction of the contour line.

    4第二视角:梯度场与沿梯度的流线A second view: the gradient field and its flow lines

    在每个点都画一支梯度箭头,就得到一个梯度场。下图把它画在等高线上:箭头处处垂直于等高线,指向函数增大的一侧,越陡越长。点击地图任意位置放下一个"种子":青色线是从那里一路往下走的最速下降线,琥珀色线是一路往上走的最速上升线。你会看到这些线像水流一样,垂直地穿过每一圈等高线。

    Draw one gradient arrow at every point and you get the gradient field. The figure below draws it over the contours: arrows are everywhere perpendicular to the contours, point toward increasing \(f\), and are longer where the terrain is steeper. Click anywhere on the map to drop a "seed": the cyan line is the steepest-descent curve that keeps walking downhill from there, the amber one is the steepest-ascent curve. These curves cross every contour at a right angle, like flowing water.

    FIG 2梯度场与流线Gradient field and flow lines
    移动鼠标(手机上点击)查看那一点的梯度;点击放下种子,最多保留 8 条。在"双峰"上,下降线会走向哪里?在"鞍面"上呢? Move the mouse (tap on a phone) to read the gradient at that point; click to drop a seed, up to 8 are kept. Where do the descent curves end on "Hills"? And on the "Saddle"?
    • 放下至少 3 个种子,检验:流线与每一圈等高线都垂直相交。Drop at least 3 seeds and check that each flow line meets every contour at a right angle.
    • 切到"鞍面",在 \(x\) 轴附近(\(|y|<0.4\)、\(|x|>1.5\))放一个种子:下降线先接近原点,然后拐弯离开。梯度在原点为 0,原点是什么点?Switch to "Saddle" and drop a seed near the \(x\)-axis (\(|y|<0.4\), \(|x|>1.5\)): the descent curve approaches the origin and then veers away. The gradient vanishes at the origin: what kind of point is it?

    最后看一步"沿着负梯度走"。从 \(\mathbf p\) 出发,走到 \(\mathbf p' = \mathbf p - \eta\nabla f(\mathbf p)\),\(\eta\) 是步长。右图画出 \(f\) 沿这条射线的切片 \(\phi(\eta)=f(\mathbf p-\eta\nabla f)\),以及它在 \(\eta=0\) 处的切线(斜率恰好是 \(-\|\nabla f\|^2\))。步子够小,\(f\) 一定下降;步子太大,会冲过谷底,甚至更高。

    Finally, one step "along the negative gradient": from \(\mathbf p\) to \(\mathbf p' = \mathbf p - \eta\nabla f(\mathbf p)\), with step size \(\eta\). The right panel plots the slice \(\phi(\eta)=f(\mathbf p-\eta\nabla f)\) of \(f\) along this ray, together with its tangent at \(\eta=0\) (whose slope is exactly \(-\|\nabla f\|^2\)). If the step is small enough, \(f\) always goes down; if it is too big, you overshoot the valley floor and may end up higher.

    FIG 3沿负梯度走一步One step along the negative gradient
    等高线图 (x, y)CONTOUR MAP (x, y)
    f 沿 −∇f 方向的切片f ALONG −∇f
    左图:黄点是 \(\mathbf p\),紫点是走一步之后的 \(\mathbf p'\),青色箭头是 \(-\eta\nabla f\)。右图:实线是真实的切片 \(\phi(\eta)\),虚线是切线(一阶预测 \(f-\eta\|\nabla f\|^2\)),圆点是当前的 \(\eta\)。"斜碗"的陡峭程度由 \(a\) 控制:\(f=\tfrac12(x^2+a\,y^2)\)。 Left: the amber dot is \(\mathbf p\), the purple dot is \(\mathbf p'\) after the step, and the cyan arrow is \(-\eta\nabla f\). Right: the solid curve is the true slice \(\phi(\eta)\), the dashed line its tangent (the first-order prediction \(f-\eta\|\nabla f\|^2\)), and the dot marks the current \(\eta\). The steepness of the "Stretched bowl" is controlled by \(a\): \(f=\tfrac12(x^2+a\,y^2)\).
    • 选一个小步长,让 \(f(\mathbf p')\) 比 \(f(\mathbf p)\) 小,并且与一阶预测几乎重合。Choose a small step so that \(f(\mathbf p')\) is lower than \(f(\mathbf p)\) and almost equals the first-order prediction.
    • 把步长加大,直到 \(f(\mathbf p')\) 反而比 \(f(\mathbf p)\) 更大。方向明明是下降方向,为什么?(提示:曲率,3.5 节会回答。)Increase the step until \(f(\mathbf p')\) is higher than \(f(\mathbf p)\). The direction is a descent direction, so why? (Hint: curvature; Lesson 3.5 answers it.)

    5再看数学The math

    提示:把鼠标悬停在公式中带底色的符号上,FIG 1 中对应的元素会被高亮。Tip: hover the highlighted symbols in the formulas; the matching element in FIG 1 lights up.

    梯度与方向导数Gradient and directional derivative

    设 \(f:\R^n\to\R\) 可微。梯度是把全部偏导数(3.1 节)排成的列向量:

    Let \(f:\R^n\to\R\) be differentiable. The gradient is the column vector of all the partial derivatives (Lesson 3.1):

    $$ \hl{grad}{\cv{\nabla f(\mathbf p)}} = \begin{bmatrix} \partial f/\partial x_1 \\ \vdots \\ \partial f/\partial x_n \end{bmatrix}_{\mathbf p}, \qquad D_{\mathbf u} f(\mathbf p) \;=\; \lim_{h\to0}\frac{f(\mathbf p + h\,\hl{u}{\cw{\mathbf u}}) - f(\mathbf p)}{h} \;=\; \frac{d}{dt}f(\mathbf p + t\mathbf u)\Big|_{t=0}. $$

    其中 \(\|\mathbf u\|=1\)。用 3.1 节的一阶近似 \(f(\mathbf p+h\mathbf u)\approx f(\mathbf p)+\nabla f\cdot(h\mathbf u)\) 代入,立刻得到

    with \(\|\mathbf u\|=1\). Substituting the first-order approximation of Lesson 3.1, \(f(\mathbf p+h\mathbf u)\approx f(\mathbf p)+\nabla f\cdot(h\mathbf u)\), gives immediately

    $$ D_{\mathbf u} f(\mathbf p) \;=\; \cv{\nabla f(\mathbf p)}\cdot\cw{\mathbf u} \;=\; \hl{cos}{\|\nabla f(\mathbf p)\|\cos\varphi}, \qquad -\|\nabla f\| \le D_{\mathbf u}f \le \|\nabla f\|. $$

    不等式就是柯西–施瓦茨不等式(点积的绝对值不超过长度之积)。等号在 \(\mathbf u\) 与梯度同向或反向时成立,所以:梯度方向是最陡上升方向,最大坡度 \(=\|\nabla f\|\);负梯度方向是最陡下降方向。注意 \(\mathbf u\) 必须是单位向量,否则方向导数要先除以 \(\|\mathbf u\|\)。特别地,取 \(\mathbf u=\mathbf e_j\) 得 \(D_{\mathbf e_j}f=\partial f/\partial x_j\):偏导数是方向导数的特例。

    The inequality is Cauchy–Schwarz (the absolute dot product is at most the product of lengths). Equality holds when \(\mathbf u\) is parallel or antiparallel to the gradient, so: the gradient direction is the direction of steepest ascent, with maximum slope \(=\|\nabla f\|\); the negative gradient is steepest descent. Note that \(\mathbf u\) must be a unit vector; otherwise divide by \(\|\mathbf u\|\) first. In particular \(\mathbf u=\mathbf e_j\) gives \(D_{\mathbf e_j}f=\partial f/\partial x_j\): partial derivatives are special directional derivatives.

    梯度垂直于等高线The gradient is perpendicular to level sets

    设 \(\mathbf c(t)\) 是一条等高线(水平集 \(f=c\))上的光滑曲线,则 \(f(\mathbf c(t))\equiv c\)。两边对 \(t\) 求导,用链式法则(2.2 节):

    Let \(\mathbf c(t)\) be a smooth curve lying on a contour (the level set \(f=c\)), so \(f(\mathbf c(t))\equiv c\). Differentiate both sides in \(t\) with the chain rule (Lesson 2.2):

    $$ \frac{d}{dt} f(\mathbf c(t)) = \nabla f(\mathbf c(t))\cdot\mathbf c'(t) = 0 \quad\Longrightarrow\quad \nabla f \perp \mathbf c'(t). $$

    曲线的速度 \(\mathbf c'\) 就是等高线的切向,所以梯度垂直于等高线(在 \(\nabla f\neq\mathbf 0\) 的地方)。等高线越密,说明 \(\|\nabla f\|\) 越大。

    The velocity \(\mathbf c'\) is the tangent of the contour, so the gradient is perpendicular to the contour (wherever \(\nabla f\neq\mathbf 0\)). Denser contours mean a larger \(\|\nabla f\|\).

    沿负梯度走一步One step along the negative gradient

    $$ f(\mathbf p-\eta\nabla f) \;=\; f(\mathbf p) \;-\; \eta\,\|\nabla f(\mathbf p)\|^2 \;+\; O(\eta^2). $$

    只要 \(\nabla f\neq\mathbf 0\),对足够小的 \(\eta\),\(f\) 一定下降。余项 \(O(\eta^2)\) 由曲率决定(Hessian,3.5 节),它决定"足够小"到底是多小。这就是 6.3 节梯度下降的全部起点。

    As long as \(\nabla f\neq\mathbf 0\), \(f\) goes down for small enough \(\eta\). The \(O(\eta^2)\) remainder is set by curvature (the Hessian, Lesson 3.5), and it decides how small "small enough" really is. This is the whole starting point of gradient descent in Lesson 6.3.

    与雅可比、链式法则的关系Relation to the Jacobian and the chain rule

    对标量函数,雅可比(3.3 节)只有一行:\(J_f=\nabla f^\top\)。梯度是列,雅可比是行,只差一个转置,但它们在"什么空间里"的意义不同:梯度是输入空间里的向量,雅可比是把输入变化映到输出变化的线性映射。链式法则也因此有梯度版本。若 \(c(\mathbf q)=g(F(\mathbf q))\),\(F:\R^n\to\R^m\)、\(g:\R^m\to\R\),则

    For a scalar function the Jacobian (Lesson 3.3) has a single row: \(J_f=\nabla f^\top\). The gradient is a column and the Jacobian a row, differing by a transpose, but they live in different roles: the gradient is a vector in input space, the Jacobian a linear map from input changes to output changes. The chain rule has a gradient version too. If \(c(\mathbf q)=g(F(\mathbf q))\) with \(F:\R^n\to\R^m\) and \(g:\R^m\to\R\), then

    $$ \nabla c(\mathbf q) \;=\; J_F(\mathbf q)^\top\,\nabla g\big(F(\mathbf q)\big). $$

    例如机械臂 IK 的代价 \(c(\boldsymbol\theta)=\tfrac12\|F(\boldsymbol\theta)-\mathbf t\|^2\)(\(F\) 是正运动学),\(\nabla g=F-\mathbf t\),于是 \(\nabla c=J^\top(F-\mathbf t)\)。沿负梯度走一步就是 \(\alpha J^\top(\mathbf t-F)\),也就是 3.4 节"转置法"的更新 \(\alpha J^\top\mathbf e\)(那里误差定义为 \(\mathbf e=\mathbf t-F\))。

    For example, a robot-arm IK cost \(c(\boldsymbol\theta)=\tfrac12\|F(\boldsymbol\theta)-\mathbf t\|^2\) (\(F\) is forward kinematics) has \(\nabla g=F-\mathbf t\), so \(\nabla c=J^\top(F-\mathbf t)\). A step along the negative gradient is \(\alpha J^\top(\mathbf t-F)\), exactly the "transpose method" update \(\alpha J^\top\mathbf e\) of Lesson 3.4 (where the error is defined as \(\mathbf e=\mathbf t-F\)).

    例题Worked example

    \(f(x,y)=x^2y\),点 \(\mathbf p=(1,2)\),方向 \(\mathbf u=(3,4)/5\)。\(\nabla f=(2xy,\;x^2)=(4,\;1)\),\(\|\nabla f\|=\sqrt{17}\approx4.12\)。方向导数 \(D_{\mathbf u}f=(4\cdot3+1\cdot4)/5=3.2\),小于最大值 \(4.12\)。最陡上升方向是 \((4,1)/\sqrt{17}\),沿等高线的方向是 \((1,-4)/\sqrt{17}\)(与梯度的点积 \(4-4=0\))。

    \(f(x,y)=x^2y\), point \(\mathbf p=(1,2)\), direction \(\mathbf u=(3,4)/5\). \(\nabla f=(2xy,\;x^2)=(4,\;1)\), \(\|\nabla f\|=\sqrt{17}\approx4.12\). The directional derivative is \(D_{\mathbf u}f=(4\cdot3+1\cdot4)/5=3.2\), below the maximum \(4.12\). The steepest-ascent direction is \((4,1)/\sqrt{17}\), and the direction along the contour is \((1,-4)/\sqrt{17}\) (dot product with the gradient \(4-4=0\)).

    6机器人中的它:人工势场导航In robotics: artificial potential-field navigation

    经典的局部避障方法(Khatib, 1986)给环境造一个标量"势能" \(U(\mathbf x)\):目标处是山谷(吸引势),障碍物周围是陡峭的山丘(排斥势)。机器人只做一件事:沿着负梯度走,\(\dot{\mathbf x}=-\nabla U(\mathbf x)\),就像一颗小球滚下山。

    $$ U(\mathbf x) = \tfrac12 k_a\|\mathbf x-\mathbf g\|^2 + \sum_{i}\tfrac12 k_r\Big(\tfrac1{d_i}-\tfrac1{d_0}\Big)^2\ \ (d_i<d_0),\qquad d_i = \|\mathbf x-\mathbf c_i\|-r_i. $$

    下图用的就是这个势场。拖动绿色目标、蓝色起点和红色障碍物,机器人的轨迹由真实的 \(-\nabla U\) 积分得到。只改排斥强度 \(k_r\):太弱会撞上障碍物,太强会把狭窄通道也堵死。

    A classic local obstacle-avoidance method (Khatib, 1986) builds a scalar "potential" \(U(\mathbf x)\) over the environment: a valley at the goal (attractive potential) and steep hills around obstacles (repulsive potential). The robot does one thing only: walk along the negative gradient, \(\dot{\mathbf x}=-\nabla U(\mathbf x)\), like a ball rolling downhill.

    $$ U(\mathbf x) = \tfrac12 k_a\|\mathbf x-\mathbf g\|^2 + \sum_{i}\tfrac12 k_r\Big(\tfrac1{d_i}-\tfrac1{d_0}\Big)^2\ \ (d_i<d_0),\qquad d_i = \|\mathbf x-\mathbf c_i\|-r_i. $$

    The figure uses exactly this field. Drag the green goal, the blue start and the red obstacles; the trajectory comes from integrating the real \(-\nabla U\). Vary only the repulsion strength \(k_r\): too weak and the robot hits the obstacle, too strong and even a narrow corridor gets blocked.

    FIG 4人工势场:沿 −∇U 走Potential field: following −∇U
    越偏青色势能越低,越偏紫色越高(对数着色)。细箭头是 \(-\nabla U\) 的方向场;紫色粗线是机器人的轨迹(步长固定,速度限幅);轨迹终点的圆点显示它是否到达目标。红色实心圆是障碍物,虚线圆是排斥势的作用范围 \(d_0\)。 More cyan means lower potential, more purple higher (log shading). The thin arrows are the direction field of \(-\nabla U\); the thick purple line is the robot's trajectory (fixed time step, speed-limited); the dot at its end shows whether it reached the goal. The solid red discs are obstacles and the dashed circles mark the repulsion range \(d_0\).
    • 让机器人绕过障碍物,到达目标且不进入任何障碍物。Get the robot around the obstacles and to the goal without entering any obstacle.
    • 把两个障碍物并排放在起点与目标的连线上,中间只留一条窄缝,让机器人卡在局部极小(\(\nabla U=0\) 但没到目标)。怎样才能让它脱困?Place two obstacles side by side across the line between start and goal, leaving only a narrow gap, so that the robot gets stuck in a local minimum (\(\nabla U=0\) yet the goal not reached). How could it escape?
    真实系统里In real systems

    局部极小是势场法的老毛病:梯度为零不等于到了目标。实际系统常把势场与全局规划器配合(全局路径给出"航点",局部势场负责避障),或者使用没有局部极小的导航函数、距离场(SDF)梯度。轨迹优化(CHOMP 等)把障碍代价写成沿轨迹的积分,也靠 SDF 的梯度把轨迹从障碍物里"推"出来。操作空间里,末端受到的"虚拟力"\(-\nabla U\) 再用 3.4 节的 \(\boldsymbol\tau=J^\top\mathbf F\) 变成关节力矩,就是经典的势场控制。

    Local minima are the old weakness of potential fields: a zero gradient does not mean the goal was reached. Real systems pair the field with a global planner (the global path supplies waypoints, the local field handles avoidance), or use navigation functions, which have no spurious minima, or SDF gradients. Trajectory optimization (CHOMP and others) writes the obstacle cost as an integral along the trajectory and uses SDF gradients to "push" the trajectory out of obstacles. In operational space, the "virtual force" \(-\nabla U\) on the end effector is turned into joint torques with \(\boldsymbol\tau=J^\top\mathbf F\) from Lesson 3.4: classic potential-field control.

    7常见误区Common pitfalls

    误区 1:梯度指向最小值(或最大值)Pitfall 1: the gradient points at the minimum (or maximum)

    梯度只是局部最陡的方向。在狭长的山谷里,梯度几乎垂直于谷底方向,指向对面谷壁而不是最低点(FIG 3 把 \(a\) 调大就能看到)。这正是梯度下降会"之"字形前进的原因(6.3 节)。

    The gradient is the locally steepest direction, nothing more. In a long narrow valley it is nearly perpendicular to the valley floor and points at the opposite wall rather than the lowest point (raise \(a\) in FIG 3 to see it). This is exactly why gradient descent zigzags (Lesson 6.3).

    误区 2:忘了方向向量要单位化Pitfall 2: forgetting to normalize the direction

    \(D_{\mathbf u}f=\nabla f\cdot\mathbf u\) 只对 \(\|\mathbf u\|=1\) 成立。如果直接拿 \(\mathbf u=(1,1)\) 点乘,会得到 \(\sqrt2\) 倍的答案,那是"沿 \((1,1)\) 走一个单位的参数时的变化率",而不是"每走一米的变化率"。

    \(D_{\mathbf u}f=\nabla f\cdot\mathbf u\) holds only for \(\|\mathbf u\|=1\). Dotting with \(\mathbf u=(1,1)\) directly gives an answer \(\sqrt2\) too big: the rate per unit of parameter along \((1,1)\), not per metre travelled.

    误区 3:梯度与坐标的单位、缩放有关Pitfall 3: the gradient depends on units and scaling of the coordinates

    "最陡"依赖于你怎么量距离。如果状态向量里一个分量是米、另一个是弧度,或者把某个轴的单位从米改成毫米,梯度的方向就变了(因为"走一步"的含义变了)。机器人里把位置和角度放进同一个优化变量时,要么先缩放变量,要么使用考虑尺度的度量(预条件)。

    "Steepest" depends on how you measure distance. If one component of the state is in metres and another in radians, or you change one axis from metres to millimetres, the gradient direction changes (because what "one step" means has changed). When positions and angles share one optimization variable in robotics, rescale the variables first or use a scale-aware metric (preconditioning).

    误区 4:梯度为零就是最低点Pitfall 4: a zero gradient means the lowest point

    \(\nabla f=\mathbf 0\) 的点叫驻点,可能是极小、极大,也可能是鞍点(FIG 2 的"鞍面")。FIG 4 中的局部极小同样是梯度为零。区分它们要看二阶信息:Hessian(3.5 节)。

    Points with \(\nabla f=\mathbf 0\) are stationary points; they may be minima, maxima, or saddles (the "Saddle" of FIG 2). The local minimum in FIG 4 has a zero gradient too. Telling them apart takes second-order information: the Hessian (Lesson 3.5).

    8小练习Check yourself

    1. \(f(x,y)=x^2y+y^3\),在点 \((1,1)\) 处的梯度是?1. For \(f(x,y)=x^2y+y^3\), what is the gradient at \((1,1)\)?
    \(f_x=2xy=2\),\(f_y=x^2+3y^2=4\)。选项 A 漏了 \(y^3\) 的贡献,选项 C 把 \(f_x\) 求成了 \(x^2\),选项 D 在 \(f_y\) 里漏掉了 \(x^2\) 项。\(f_x=2xy=2\) and \(f_y=x^2+3y^2=4\). Option A dropped the \(y^3\) term, option C took \(f_x=x^2\), and option D dropped the \(x^2\) term from \(f_y\).
    2. \(f(x,y)=xy\),在 \((2,1)\) 处沿 \(\mathbf u=(1,1)/\sqrt2\) 的方向导数是?2. For \(f(x,y)=xy\), what is the directional derivative at \((2,1)\) along \(\mathbf u=(1,1)/\sqrt2\)?
    \(\nabla f=(y,x)=(1,2)\),\(D_{\mathbf u}f=(1+2)/\sqrt2=3/\sqrt2\approx2.12\)。\(3\) 是忘了单位化;\(\sqrt5\) 是 \(\|\nabla f\|\),即所有方向中的最大值,这里 \(\mathbf u\) 不是梯度方向,所以小于它。\(\nabla f=(y,x)=(1,2)\), so \(D_{\mathbf u}f=(1+2)/\sqrt2=3/\sqrt2\approx2.12\). The value \(3\) forgets to normalize; \(\sqrt5\) is \(\|\nabla f\|\), the maximum over all directions, which this \(\mathbf u\) does not reach since it is not along the gradient.
    3. 某点 \(\nabla f=(3,-4)\)。最陡下降的方向与下降的速率(每单位距离)分别是?3. At some point \(\nabla f=(3,-4)\). What are the direction of steepest descent and its rate (per unit distance)?
    下降方向是 \(-\nabla f/\|\nabla f\|=(-3,4)/5\),速率是 \(\|\nabla f\|=5\)。选项 D \((4,3)/5\) 与梯度垂直,沿它走高度不变(速率 0),那是等高线方向,不是下降方向。The descent direction is \(-\nabla f/\|\nabla f\|=(-3,4)/5\), at rate \(\|\nabla f\|=5\). Option D, \((4,3)/5\), is perpendicular to the gradient: walking along it keeps the height constant (rate 0), the contour direction rather than a descent direction.
    4. 在势场导航中,机器人沿 \(-\nabla U\) 走,停在了一个不是目标的点。最合理的解释是?4. In potential-field navigation a robot following \(-\nabla U\) stops at a point that is not the goal. What is the most plausible explanation?
    沿负梯度走只能保证势能下降,到驻点就停。吸引势和排斥势叠加后可能产生目标之外的局部极小(如 FIG 4 中两个障碍物并排挡在连线上)。这是势场法需要配合全局规划的原因。Following the negative gradient only guarantees that the potential goes down, and it stops at a stationary point. Attractive and repulsive terms can combine into local minima away from the goal (two obstacles side by side across the straight line in FIG 4). This is why potential fields need a global planner alongside.

    9延伸阅读Further reading

    • Khan Academy, Multivariable Calculus — Grant Sanderson(3Blue1Brown)主讲的梯度与方向导数视频Grant Sanderson's (3Blue1Brown) videos on the gradient and the directional derivative
    • G. Strang, Calculus (MIT OpenCourseWare) — 梯度、方向导数与等高线The gradient, directional derivatives and level curves
    • S. Boyd & L. Vandenberghe, Convex Optimization, Appendix A.4 & Ch. 9 — 梯度的定义,以及梯度下降的收敛性The definition of the gradient and the convergence of gradient descent
    • O. Khatib, Real-Time Obstacle Avoidance for Manipulators and Mobile Robots, Int. J. Robotics Research 5(1), 1986 — 人工势场法的经典论文The classic paper on artificial potential fields
    • S. LaValle, Planning Algorithms, Ch. 5 — 势场、导航函数与局部极小Potential fields, navigation functions and local minima