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

多元函数与偏导Multivariable Functions & Partial Derivatives

机器人的世界里,几乎没有只依赖一个变量的量:末端到目标的距离同时取决于每个关节角,信号强度同时取决于 \(x\) 和 \(y\),损失同时取决于几百万个参数。把其余旋钮都固定住,只拧动其中一个,输出会变多快?这个"单旋钮的灵敏度"就是偏导数。 Almost nothing in a robot's world depends on a single variable: the distance from the gripper to a target depends on every joint angle, signal strength depends on both \(x\) and \(y\), a loss depends on millions of parameters. Freeze all the knobs but one and turn it: how fast does the output change? That "one-knob sensitivity" is the partial derivative.

约 20 分钟~20 min 前置:1.1 向量 · 2.1 导数Prereqs: 1.1 Vectors · 2.1 Derivatives 4 个交互4 interactives

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

    一元函数 \(g(x)\) 的导数回答:"\(x\) 动一点,\(g\) 变多少?"可机器人面对的函数几乎都有很多个输入:

    • 两连杆机械臂的末端到目标的距离 \(d(\theta_1,\theta_2)\):该先转肩关节,还是先转肘关节?
    • 地图上每个位置的代价或信号强度(WiFi、气味、距离场)\(c(x,y)\):往东走还是往北走,代价降得更快?
    • 神经网络策略的损失 \(\mathcal L(w_1,\dots,w_N)\):\(N\) 个参数里,每一个对损失的影响有多大?

    三个问题的答案都是同一种东西:固定其他变量,只看一个变量时的变化率,这就是偏导数。把所有偏导数排成一列,得到下一节的梯度;对向量值函数把它们排成一张表,得到 3.3 节的雅可比。所以这一节是整个第 3 章的地基。

    The derivative of a one-variable function \(g(x)\) answers: "nudge \(x\) a little, how much does \(g\) change?" But the functions a robot faces almost always have many inputs:

    • The distance from the gripper to a target, \(d(\theta_1,\theta_2)\), of a two-link arm: should you turn the shoulder or the elbow first?
    • The cost or signal strength over a map (WiFi, odor, a distance field), \(c(x,y)\): walk east or north to bring the cost down faster?
    • The loss of a neural policy, \(\mathcal L(w_1,\dots,w_N)\): among \(N\) parameters, how strongly does each one move the loss?

    All three have the same kind of answer: the rate of change with respect to one variable while the others are held fixed. That is the partial derivative. Line all of them up in a column and you get the gradient of the next lesson; for a vector-valued function, arrange them in a table and you get the Jacobian of Lesson 3.3. This lesson is the foundation of the whole chapter.

    2直觉:把曲面切成一片一片Intuition: slice the surface

    二元函数 \(f(x,y)\) 可以画成一张曲面:在平面上每个点 \((x,y)\) 的正上方,高度是 \(f(x,y)\)。站在曲面上的一点 \(\mathbf p\),"坡度"这个词立刻变得含糊:朝东是上坡,朝北可能是下坡。坡度取决于朝哪走。

    最简单的办法是只允许沿着坐标轴走。固定 \(y = y_0\),把曲面沿着这个平面切开,切口是一条普通的一元曲线 \(x \mapsto f(x, y_0)\),它在 \(x_0\) 处的斜率就是 \(f\) 对 \(x\) 的偏导数。同样固定 \(x = x_0\) 再切一刀,得到另一条切口,它的斜率是对 \(y\) 的偏导数。下图中红色切口对应 \(x\),绿色切口对应 \(y\),拖动滑块把点 \(\mathbf p\) 移来移去。

    A function of two variables, \(f(x,y)\), can be drawn as a surface: above each point \((x,y)\) of the plane, the height is \(f(x,y)\). Stand on the surface at a point \(\mathbf p\) and the word "slope" suddenly turns ambiguous: east may be uphill while north is downhill. The slope depends on which way you walk.

    The simplest fix is to walk only along the coordinate axes. Freeze \(y = y_0\) and cut the surface along that plane: the cut is an ordinary one-variable curve \(x \mapsto f(x, y_0)\), and its slope at \(x_0\) is the partial derivative of \(f\) with respect to \(x\). Freeze \(x = x_0\) and cut again for the other curve; its slope is the partial derivative with respect to \(y\). In the figure below the red cut belongs to \(x\), the green cut to \(y\). Drag the sliders to move \(\mathbf p\) around.

    FIG 1曲面与两刀切口A surface and its two cuts
    拖动画面可以旋转视角。琥珀色圆点是 \(\mathbf p\) 在曲面上的位置;红色曲线是 \(y = y_0\) 的切口,绿色曲线是 \(x = x_0\) 的切口;两条虚线是各自的切线,斜率分别是 \(\partial f/\partial x\) 与 \(\partial f/\partial y\)。曲面的竖直方向已按比例缩放,只看斜率的正负和大小关系即可。 Drag the picture to rotate the view. The amber dot is \(\mathbf p\) on the surface; the red curve is the cut \(y = y_0\), the green one the cut \(x = x_0\); the two dashed lines are their tangents, with slopes \(\partial f/\partial x\) and \(\partial f/\partial y\). The vertical axis is rescaled for display, so only the signs and relative sizes of the slopes matter.
    • 找到一个两个偏导数同时为 0 的点:两条切线都是水平的。在"鞍面"上试试,这个点叫什么?Find a point where both partial derivatives are 0: both tangent lines are horizontal. Try the "saddle": what would you call such a point?
    • 找一个点,让 \(\partial f/\partial x > 0\) 而 \(\partial f/\partial y < 0\):朝 \(x\) 正方向是上坡,朝 \(y\) 正方向却是下坡。Find a point with \(\partial f/\partial x > 0\) but \(\partial f/\partial y < 0\): uphill toward +\(x\), downhill toward +\(y\).
    核心洞见Key insight

    偏导数就是一元导数,只不过把其余变量当作常数。\(\partial f/\partial x\) 是"沿着 \(x\) 方向的坡度",\(\partial f/\partial y\) 是"沿着 \(y\) 方向的坡度"。一个点上有多少个输入变量,就有多少个这样的坡度。

    A partial derivative is an ordinary derivative that treats the other variables as constants. \(\partial f/\partial x\) is "the slope in the \(x\) direction" and \(\partial f/\partial y\) "the slope in the \(y\) direction". A point has as many such slopes as the function has inputs.

    3动手玩:用差分"量"出偏导Play: measuring a partial by differencing

    偏导数的定义就是一个极限:沿 \(x\) 方向迈一小步 \(h\),看 \(f\) 变了多少,再除以 \(h\)。左边是等高线图(越偏青色越低,越偏紫色越高,每条线上 \(f\) 相等),拖动黄点 \(\mathbf p\)。右边把两条切口画在同一张图里:横轴是离开 \(\mathbf p\) 的位移 \(t\),纵轴是 \(f\)。实心粗线段是"差商",即割线,虚线是真正的切线。把 \(h\) 拉小,割线就会贴上切线。

    现实里的 \(f\) 常常是传感器读数,带噪声。把"测量噪声"打开,再把 \(h\) 拉到很小,会发生什么?

    The partial derivative is defined by a limit: take a small step \(h\) in the \(x\) direction, see how much \(f\) changed, and divide by \(h\). On the left is a contour map (more cyan is lower, more purple is higher; \(f\) is constant along each line); drag the amber point \(\mathbf p\). On the right both cuts share one plot: the horizontal axis is the displacement \(t\) away from \(\mathbf p\), the vertical axis is \(f\). The thick solid segments are the "difference quotients" (secants); the dashed lines are the true tangents. Pull \(h\) down and the secants settle onto the tangents.

    In real life \(f\) is often a sensor reading, and sensors are noisy. Turn "measurement noise" on, then pull \(h\) very small. What happens?

    FIG 2偏导数实验室Partial-derivative lab
    等高线图 (x, y)CONTOUR MAP (x, y)
    两条切口:f 对位移 tTHE TWO CUTS: f vs. offset t
    红色对应 \(x\) 方向,绿色对应 \(y\) 方向。左图中黄点旁的红、绿短线是步长为 \(h\) 的两步。右图的空心圆圈是带噪声的测量值,实线割线连接的是测量值,所以噪声不为零时割线的斜率会偏离切线。 Red is the \(x\) direction, green the \(y\) direction. The short red and green segments beside the amber point on the left are the two steps of size \(h\). The hollow circles on the right are the noisy measurements and the secants join the measured values, so with noise the secant slope can stray from the tangent.
    试一试Try this
    • 噪声设为 0,把 \(h\) 缩小,直到两个差商都和解析偏导相差不到 0.01。With noise at 0, shrink \(h\) until both difference quotients are within 0.01 of the analytic partials.
    • 把噪声设为 0.05 以上、\(h\) 拉到 0.01 以下,让估计的偏导数错得离谱(误差大于 2)。然后找一个折中的 \(h\),让误差回到 0.6 以下。Set noise to 0.05 or more and \(h\) below 0.01, and make an estimate wildly wrong (error above 2). Then find a compromise \(h\) that brings the error back under 0.6.

    这里有两种误差在拔河:\(h\) 大了,割线和切线差得远(截断误差,大约正比于 \(h\));\(h\) 小了,两个几乎相等的测量值相减,噪声被除以 \(h\) 放大(大约正比于 \(\sigma/h\))。所以数值求偏导存在一个最佳步长:既不能太大,也不能太小。机器人没有解析公式、只能"试探着动一动再看传感器"的时候,这一点尤其重要。

    Two kinds of error are in a tug of war here. A large \(h\) lets the secant drift from the tangent (truncation error, roughly proportional to \(h\)); a tiny \(h\) subtracts two nearly equal measurements and divides the noise by \(h\) (roughly proportional to \(\sigma/h\)). So numerical partials have a sweet spot for the step: neither too big nor too small. This matters whenever a robot has no formula and can only "wiggle and read the sensor".

    4第二视角:偏导数会"相加"A second view: partials add up

    如果 \(x\) 和 \(y\) 同时改变呢?走一条阶梯路径:先沿 \(x\) 方向走 \(\delta_x\),再沿 \(y\) 方向走 \(\delta_y\)。每一小段都是"一元问题",变化量约等于斜率乘以步长。两段加起来,就是总变化的一阶预测:

    What if \(x\) and \(y\) change at the same time? Walk a staircase: first \(\delta_x\) along \(x\), then \(\delta_y\) along \(y\). Each leg is a one-variable problem, and the change is about slope times step. Adding the two legs gives a first-order prediction of the total change:

    $$ f(x+\delta_x,\,y+\delta_y) - f(x,y) \;\approx\; \ci{\frac{\partial f}{\partial x}}\,\delta_x \;+\; \cj{\frac{\partial f}{\partial y}}\,\delta_y $$

    拖动下图中的紫色手柄 \(\mathbf q = \mathbf p + \boldsymbol\delta\),黄点 \(\mathbf p\) 也可以拖动。右边的瀑布图把预测的变化拆成两块:红色是 \(x\) 方向贡献的 \(f_x\delta_x\),绿色接在它后面,是 \(y\) 方向贡献的 \(f_y\delta_y\);琥珀色条是 \(f\) 的真实变化,紫色竖虚线标出预测值。

    Drag the purple handle \(\mathbf q = \mathbf p + \boldsymbol\delta\) in the figure below; the amber point \(\mathbf p\) can be dragged too. The waterfall on the right splits the predicted change into two pieces: red is the \(x\) contribution \(f_x\delta_x\), green stacks onto it with the \(y\) contribution \(f_y\delta_y\); the amber bar is the true change in \(f\), and the purple dashed line marks the prediction.

    FIG 3阶梯路径与瀑布图Staircase path and waterfall
    等高线图 (x, y)CONTOUR MAP (x, y)
    f 的变化量CHANGE IN f
    阶梯路径是 \(\mathbf p\to(p_x+\delta_x,\,p_y)\to\mathbf q\),先红后绿。预测与真实变化之间的差(残差)大约正比于 \(\|\boldsymbol\delta\|^2\):点一下"δ 减半",残差会变成约原来的四分之一。 The staircase is \(\mathbf p\to(p_x+\delta_x,\,p_y)\to\mathbf q\), red first, then green. The gap between prediction and truth (the residual) scales like \(\|\boldsymbol\delta\|^2\): press "Halve δ" and it drops to about a quarter.
    • 把 \(\mathbf q\) 拖到一个位置,使红、绿两块几乎抵消(预测的总变化小于 0.02),同时 \(\|\boldsymbol\delta\|>0.3\)。这时你正沿着什么在走?(答案在下一节。)Drag \(\mathbf q\) so the red and green pieces almost cancel (predicted total change below 0.02) while \(\|\boldsymbol\delta\|>0.3\). What are you walking along? (The next lesson answers this.)
    三种常见的函数形态Three common shapes of function

    多入一出 \(f:\R^n\to\R\)(本节):有 \(n\) 个偏导数,排成一列就是梯度 \(\nabla f\)(3.2 节)。
    多入多出 \(F:\R^n\to\R^m\):每个输出各有 \(n\) 个偏导,一共 \(m\times n\) 个,排成一张表就是雅可比 \(J\)(3.3 节)。
    偏导的偏导:二阶偏导 \(\partial^2 f/\partial x_i\partial x_j\) 排成 Hessian(3.5 节),描述曲面怎么弯。

    Many in, one out, \(f:\R^n\to\R\) (this lesson): \(n\) partials; stacked in a column they form the gradient \(\nabla f\) (Lesson 3.2).
    Many in, many out, \(F:\R^n\to\R^m\): each output has its own \(n\) partials, \(m\times n\) in all; arranged in a table they form the Jacobian \(J\) (Lesson 3.3).
    Partials of partials: the second derivatives \(\partial^2 f/\partial x_i\partial x_j\) form the Hessian (Lesson 3.5), which describes how the surface bends.

    5再看数学The math

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

    定义Definition

    设 \(f:\R^n\to\R\),\(\mathbf p=(p_1,\dots,p_n)\)。\(f\) 在 \(\mathbf p\) 处对第 \(j\) 个变量的偏导数,是只让第 \(j\) 个坐标变化的一元导数:

    Let \(f:\R^n\to\R\) and \(\mathbf p=(p_1,\dots,p_n)\). The partial derivative of \(f\) at \(\mathbf p\) with respect to the \(j\)-th variable is the one-variable derivative in which only the \(j\)-th coordinate moves:

    $$ \frac{\partial f}{\partial x_j}(\mathbf p) \;=\; \lim_{h\to 0}\; \frac{f(\mathbf p + \hl{step}{h\,\mathbf e_j}) - \hl{f0}{f(\mathbf p)}}{\hl{h}{h}} \;=\; \frac{d}{dt}\, f(\mathbf p + t\,\mathbf e_j)\Big|_{t=0}. $$

    其中 \(\mathbf e_j\) 是第 \(j\) 个标准基向量(1.2 节),除了第 \(j\) 个分量为 1 外全为 0。最右边的写法说明了切口的含义:沿着穿过 \(\mathbf p\) 的坐标轴方向的直线,把 \(f\) 限制在这条线上,得到一个一元函数,再对它求导。常见的记号还有 \(f_{x_j}\)、\(\partial_j f\)、\(D_j f\),意思都一样。

    Here \(\mathbf e_j\) is the \(j\)-th standard basis vector (Lesson 1.2): 1 in slot \(j\), 0 elsewhere. The right-hand form says what a "cut" is: restrict \(f\) to the line through \(\mathbf p\) along a coordinate axis, getting a one-variable function, and differentiate that. Other notations, \(f_{x_j}\), \(\partial_j f\), \(D_j f\), all mean the same thing.

    计算:把其余变量当常数Computing: treat the others as constants

    实际计算时,求 \(\partial f/\partial x\) 就是对 \(x\) 求导,\(y\) 当成一个数字。例如 \(f(x,y) = x^2 y + \sin y\):

    In practice, to get \(\partial f/\partial x\), differentiate in \(x\) and treat \(y\) as a plain number. For example, \(f(x,y) = x^2 y + \sin y\):

    $$ \ci{\frac{\partial f}{\partial x}} = \ci{2xy}, \qquad \cj{\frac{\partial f}{\partial y}} = \cj{x^2 + \cos y}. $$

    在 \(\mathbf p=(2,0)\) 处:\(f_x = 0\),\(f_y = 4 + 1 = 5\)。沿 \(x\) 方向,函数此刻是平的(因为 \(y=0\) 把 \(x^2y\) 整个压没了),沿 \(y\) 方向的坡度是 5。

    At \(\mathbf p=(2,0)\): \(f_x = 0\) and \(f_y = 4 + 1 = 5\). Along \(x\) the function is flat right now (\(y=0\) wipes out all of \(x^2y\)), while along \(y\) the slope is 5.

    一阶近似与全微分First-order approximation and the total differential

    FIG 3 的结论可以写成一般形式。把所有偏导数收在一起,用 1.7 节的点积来写:

    The conclusion of FIG 3 has a general form. Collect all the partials and write it with the dot product of Lesson 1.7:

    $$ f(\mathbf p + \boldsymbol\delta) \;\approx\; \cv{f(\mathbf p)} + \sum_{j=1}^{n} \frac{\partial f}{\partial x_j}(\mathbf p)\,\delta_j \;=\; \cv{f(\mathbf p)} + \Big[\tfrac{\partial f}{\partial x_1}\;\cdots\;\tfrac{\partial f}{\partial x_n}\Big]\boldsymbol\delta . $$

    方括号里是一个行向量,它就是下一节的梯度 \(\nabla f(\mathbf p)^\top\)。只要各偏导数本身连续(\(C^1\)),上式就成立,余项比 \(\|\boldsymbol\delta\|\) 小得多(\(o(\|\boldsymbol\delta\|)\));若二阶偏导也连续(\(C^2\)),余项是 \(O(\|\boldsymbol\delta\|^2)\)。只有偏导数存在而不连续时,这个近似可能失败,这是第 7 节的一个误区。

    The bracket is a row vector, which is the next lesson's gradient \(\nabla f(\mathbf p)^\top\). The formula holds as long as the partials themselves are continuous (\(C^1\)), with a remainder much smaller than \(\|\boldsymbol\delta\|\) (\(o(\|\boldsymbol\delta\|)\)); if the second partials are continuous too (\(C^2\)), the remainder is \(O(\|\boldsymbol\delta\|^2)\). If partials merely exist but are discontinuous, the approximation can fail, which is one of the pitfalls in Section 7.

    二阶偏导与混合偏导Second partials and mixed partials

    偏导数本身又是 \(x,y\) 的函数,可以再求偏导。对 \(f = x^2y+\sin y\):

    A partial derivative is itself a function of \(x,y\), so it can be differentiated again. For \(f = x^2y+\sin y\):

    $$ f_{xx} = 2y,\quad f_{yy} = -\sin y,\quad f_{xy} = \frac{\partial}{\partial y}\big(f_x\big) = 2x,\quad f_{yx} = \frac{\partial}{\partial x}\big(f_y\big) = 2x. $$

    两个混合偏导数相等不是巧合。只要二阶偏导连续,求导的次序就可以交换:\(f_{xy} = f_{yx}\)(Schwarz 定理,也叫 Clairaut 定理)。这个对称性在 3.5 节里变成"Hessian 是对称矩阵"。

    The two mixed partials agreeing is no accident. As long as the second partials are continuous, the order of differentiation can be swapped: \(f_{xy} = f_{yx}\) (Schwarz's, or Clairaut's, theorem). In Lesson 3.5 this symmetry becomes "the Hessian is a symmetric matrix".

    例题:机械臂末端到目标的距离Worked example: gripper-to-target distance

    平面两连杆机械臂,末端位置 \(\mathbf x(\theta_1,\theta_2)\),目标 \(\mathbf t\),距离 \(d = \|\mathbf x - \mathbf t\|\)。用链式法则(2.2 节)对 \(\theta_i\) 求偏导。先注意 \(\partial d/\partial\mathbf x = (\mathbf x-\mathbf t)/d\) 是从目标指向末端的单位向量 \(\hat{\mathbf n}\),于是:

    A planar two-link arm has tip position \(\mathbf x(\theta_1,\theta_2)\), a target \(\mathbf t\), and distance \(d = \|\mathbf x - \mathbf t\|\). Differentiate in \(\theta_i\) with the chain rule (Lesson 2.2). First, \(\partial d/\partial\mathbf x = (\mathbf x-\mathbf t)/d\) is the unit vector \(\hat{\mathbf n}\) from the target to the tip, so:

    $$ \frac{\partial d}{\partial \theta_i} \;=\; \hat{\mathbf n}\cdot\frac{\partial \mathbf x}{\partial \theta_i}, \qquad \frac{\partial \mathbf x}{\partial \theta_1} = \begin{bmatrix} -l_1 s_1 - l_2 s_{12} \\ \;\;l_1 c_1 + l_2 c_{12}\end{bmatrix},\quad \frac{\partial \mathbf x}{\partial \theta_2} = \begin{bmatrix} -l_2 s_{12} \\ \;\;l_2 c_{12}\end{bmatrix}. $$

    读法:\(\partial\mathbf x/\partial\theta_i\) 是只转第 \(i\) 个关节时末端跑的方向(3.3 节中雅可比的第 \(i\) 列),\(\partial d/\partial\theta_i\) 是这个方向在"远离目标"方向上的投影(1.9 节)。如果这一列几乎垂直于 \(\hat{\mathbf n}\),转这个关节对距离几乎没有一阶影响。

    Reading: \(\partial\mathbf x/\partial\theta_i\) is the direction the tip runs when only joint \(i\) turns (column \(i\) of the Jacobian in Lesson 3.3), and \(\partial d/\partial\theta_i\) is that direction's component along "away from the target" (Lesson 1.9). If the column is almost perpendicular to \(\hat{\mathbf n}\), turning that joint barely changes the distance to first order.

    6机器人中的它:先转哪个关节?In robotics: which joint first?

    让机械臂末端靠近目标(黄色菱形),但这次只允许一次转一个关节。每个关节对距离的灵敏度正是偏导数 \(\partial d/\partial\theta_1\)、\(\partial d/\partial\theta_2\):右侧的条形图向左表示"正转会让距离变小",向右表示"正转会让距离变大"。点按钮,机器人会选灵敏度绝对值较大的那个关节,朝让距离变小的方向转一小步。这就是坐标下降(coordinate descent):每次只沿一根坐标轴优化。

    Bring the arm's tip close to the target (amber diamond), but this time you may turn one joint at a time. Each joint's sensitivity is exactly the partial \(\partial d/\partial\theta_1\), \(\partial d/\partial\theta_2\): in the bar chart on the right, a bar pointing left means "turning this joint positively shrinks the distance", right means "it grows the distance". Press the button and the robot picks the joint with the larger absolute sensitivity and turns it a small step in the distance-reducing direction. That is coordinate descent: optimize along one coordinate axis at a time.

    FIG 4每个关节对距离的灵敏度Each joint's sensitivity of the distance
    ∂d/∂θ₁
    ∂d/∂θ₂
    红箭头是 \(\partial\mathbf x/\partial\theta_1\),绿箭头是 \(\partial\mathbf x/\partial\theta_2\)(按 0.5 倍长度绘制)。虚线圆是以目标为圆心、当前距离为半径的等距线:箭头在 \(\hat{\mathbf n}\) 方向上的投影越大,对应关节越"有效"。可以拖动目标,也可以用滑块手动转关节。 The red arrow is \(\partial\mathbf x/\partial\theta_1\), the green one \(\partial\mathbf x/\partial\theta_2\) (drawn at 0.5× length). The dashed circle is the set of points at the current distance from the target: the larger an arrow's projection on \(\hat{\mathbf n}\), the more "effective" that joint. Drag the target, or turn the joints by hand with the sliders.
    • 用"走一步"或滑块让末端到目标的距离小于 0.05。Use "One step" or the sliders to bring the tip within 0.05 of the target.
    • 拖动目标,让两个关节的灵敏度几乎相同,条形图两根一样长。此时两个关节哪一个更值得转?Drag the target so both joints have almost the same sensitivity and the two bars are equally long. Which joint is worth turning now?
    真实系统里In real systems

    数值逆运动学:把每个关节的偏导数一次性算出来,沿所有关节一起走,比一次只转一个快得多,这就是下一节梯度和 3.4 节雅可比求逆的内容。灵敏度分析:标定时关心"哪个参数的误差对末端精度影响最大",答案就是偏导数。无模型机器人:当 \(f\) 是黑盒(比如硬件测得的力或气味浓度)时,机器人用 FIG 2 的办法,对每个自由度各"晃一下",用差商估计偏导;每多一个自由度就要多测一次,代价随维度线性增长,噪声大时还要多次平均。

    Numerical inverse kinematics: compute all the joint partials at once and move every joint together, which is far faster than one at a time; that is the next lesson's gradient and Lesson 3.4's Jacobian inversion. Sensitivity analysis: in calibration you ask "which parameter's error hurts end-point accuracy most?", and the answer is a partial derivative. Model-free robots: when \(f\) is a black box (a force measured on hardware, an odor concentration), the robot uses the trick of FIG 2: wiggle each degree of freedom and estimate partials from difference quotients. Each extra degree of freedom costs one more measurement, so the cost grows linearly with dimension, and noisy sensors need repeated averaging.

    7常见误区Common pitfalls

    误区 1:偏导数全为 0 就是极值点Pitfall 1: all partials zero means an extremum

    不一定。FIG 1 里的鞍面在原点两个偏导都是 0,但沿 \(x\) 是谷底、沿 \(y\) 是山顶。要区分极小、极大、鞍点,需要二阶信息(3.5 节的 Hessian)。

    Not necessarily. The saddle in FIG 1 has both partials 0 at the origin, yet it is a valley floor along \(x\) and a peak along \(y\). Telling a minimum from a maximum from a saddle takes second-order information (the Hessian of Lesson 3.5).

    误区 2:偏导数都存在,函数就"可微"Pitfall 2: if all partials exist, the function is differentiable

    偏导数只检查了沿坐标轴的两个方向。函数 \(f(x,y) = xy/(x^2+y^2)\)(\(f(0,0)=0\))在原点处两个偏导数都是 0,但沿对角线方向 \(f\) 恒等于 \(\tfrac12\),函数在原点甚至不连续,FIG 3 的一阶近似当然失败。放心:只要偏导数连续,函数就是可微的,机器人里遇到的光滑模型基本都满足。

    Partials only look along the two coordinate axes. The function \(f(x,y) = xy/(x^2+y^2)\) (with \(f(0,0)=0\)) has both partials equal to 0 at the origin, yet along the diagonal \(f\) is constantly \(\tfrac12\), so the function is not even continuous there, and the first-order approximation of FIG 3 fails. No need to panic: if the partials are continuous, the function is differentiable, and the smooth models in robotics almost always satisfy this.

    误区 3:"固定其余变量"取决于你选了什么变量Pitfall 3: "hold the others fixed" depends on which variables you chose

    \(\partial f/\partial x\) 的含义依赖于同时使用的其他坐标。以 \(r=\sqrt{x^2+y^2}\) 为例:用坐标 \((x,y)\) 时"固定 \(y\)、让 \(x\) 变",得 \(\partial r/\partial x = x/r\);改用坐标 \((x,\theta)\) 时"固定 \(\theta\)、让 \(x\) 变",由 \(x=r\cos\theta\) 得 \(\partial r/\partial x = 1/\cos\theta = r/x\)。同样写作 \(\partial r/\partial x\),两者却不同。机器人里要明确:偏导是对关节角、对笛卡尔坐标,还是对李代数里的扰动。

    The meaning of \(\partial f/\partial x\) depends on which other coordinates come with it. Take \(r=\sqrt{x^2+y^2}\): in coordinates \((x,y)\), "hold \(y\) fixed and vary \(x\)" gives \(\partial r/\partial x = x/r\); in coordinates \((x,\theta)\), "hold \(\theta\) fixed and vary \(x\)" uses \(x=r\cos\theta\) and gives \(\partial r/\partial x = 1/\cos\theta = r/x\). Same symbol \(\partial r/\partial x\), different numbers. In robotics be explicit: partials with respect to joint angles, Cartesian coordinates, or a perturbation in a Lie algebra.

    误区 4:忘了各变量的单位不同Pitfall 4: forgetting that the variables have different units

    FIG 4 中的 \(\partial d/\partial\theta_i\) 单位是"米/弧度"。把弧度偏导和米偏导直接比大小,等于把苹果和橘子比。同样是"转 0.1",转的是 0.1 弧度还是 0.1 米,影响天差地别。后面做优化时,变量的尺度不一致会让问题变得病态(6.3 节)。

    In FIG 4 the \(\partial d/\partial\theta_i\) have units of "metres per radian". Comparing a per-radian partial directly with a per-metre one compares apples with oranges. "Turn by 0.1" means something quite different for 0.1 rad than for 0.1 m. In optimization later on, mismatched variable scales make a problem ill-conditioned (Lesson 6.3).

    8小练习Check yourself

    1. \(f(x,y) = x^2\sin y\)。\(\partial f/\partial y\) 在 \((1,\pi/2)\) 处是?1. \(f(x,y) = x^2\sin y\). What is \(\partial f/\partial y\) at \((1,\pi/2)\)?
    把 \(x\) 当常数:\(f_y = x^2\cos y\)。代入得 \(1\cdot\cos(\pi/2) = 0\)。选项 A(\(2\))是 \(f_x = 2x\sin y\) 在这一点的值,求错了变量。Treat \(x\) as a constant: \(f_y = x^2\cos y\), which at this point is \(1\cdot\cos(\pi/2) = 0\). Option A (\(2\)) is \(f_x = 2x\sin y\) at that point: the wrong variable.
    2. 已知 \(f(1,2)=5\),\(f_x(1,2)=3\),\(f_y(1,2)=-1\)。用一阶近似估计 \(f(1.1,\,1.9)\)。2. Given \(f(1,2)=5\), \(f_x(1,2)=3\), \(f_y(1,2)=-1\), estimate \(f(1.1,\,1.9)\) to first order.
    \(\delta_x = 0.1\),\(\delta_y = -0.1\)。变化量约 \(3\times0.1 + (-1)\times(-0.1) = 0.3+0.1 = 0.4\),所以 \(f\approx 5.4\)。注意 \(y\) 减小了,而 \(f_y<0\),两个负号相乘,这一项反而是正的。\(\delta_x = 0.1\), \(\delta_y = -0.1\). The change is about \(3\times0.1 + (-1)\times(-0.1) = 0.3+0.1 = 0.4\), so \(f\approx 5.4\). Note that \(y\) decreased while \(f_y<0\): two minus signs make this term positive.
    3. 用差商 \((f(\mathbf p+h\mathbf e_x)-f(\mathbf p))/h\) 估计偏导,传感器有噪声。把 \(h\) 一直缩小,通常会怎样?3. You estimate a partial with \((f(\mathbf p+h\mathbf e_x)-f(\mathbf p))/h\) from a noisy sensor. As you keep shrinking \(h\), what usually happens?
    差商里的噪声贡献约 \(\sqrt2\,\sigma/h\),随 \(h\) 变小而增大;截断误差约正比于 \(h\),随 \(h\) 变小而减小。两者之和在某个中间的 \(h\) 处最小(FIG 2)。The noise contribution to the quotient is about \(\sqrt2\,\sigma/h\), growing as \(h\) shrinks; the truncation error is roughly proportional to \(h\), shrinking with it. The sum is smallest at some intermediate \(h\) (FIG 2).
    4. 两连杆臂当前 \(\partial d/\partial\theta_1 = -0.8\),\(\partial d/\partial\theta_2 = +0.3\)(单位:米/弧度)。只转一个关节、转 0.1 rad,想让距离减小得最多,应该?4. A two-link arm currently has \(\partial d/\partial\theta_1 = -0.8\) and \(\partial d/\partial\theta_2 = +0.3\) (metres per radian). Turning only one joint by 0.1 rad, how do you shrink the distance the most?
    变化量约 \(\frac{\partial d}{\partial\theta_i}\Delta\theta_i\)。关节 1 的灵敏度绝对值更大,且符号为负,正转 \(+0.1\) 使 \(d\) 变化 \(-0.08\) m。关节 2 反转虽然也能让 \(d\) 减小,但只有 \(0.03\) m。最后一项把单位和数量级弄错了。The change is about \(\frac{\partial d}{\partial\theta_i}\Delta\theta_i\). Joint 1 has the larger magnitude and a negative sign, so \(+0.1\) rad changes \(d\) by \(-0.08\) m. Reversing joint 2 also reduces \(d\), but only by 0.03 m. The last option gets the scale wrong.

    9延伸阅读Further reading

    • Khan Academy, Multivariable Calculus — Grant Sanderson(3Blue1Brown)主讲的多元函数、偏导数与"切片"直觉Grant Sanderson's (3Blue1Brown) lessons on multivariable functions, partial derivatives and the slicing intuition
    • G. Strang, Calculus (MIT OpenCourseWare) — 多元函数、偏导数与线性近似Functions of several variables, partial derivatives and linear approximation
    • S. Boyd & L. Vandenberghe, Convex Optimization, Appendix A.4 — 导数、梯度与链式法则的简明复习A concise review of derivatives, gradients and the chain rule
    • J. Nocedal & S. Wright, Numerical Optimization, Ch. 8 — 有限差分近似导数及其步长选择Finite-difference derivative approximations and how to choose the step