泰勒展开Taylor Expansion
2.1 节说,光滑函数放大后是一条直线。泰勒展开把这句话升级:放大后是直线,稍微放松一点是抛物线,再放松一点是三次曲线……每多算一阶导数,近似就能在更大的范围内贴紧函数。机器人里的"小角度近似"、线性化、Newton 法、EKF,全是截断的泰勒展开。 Lesson 2.1 said a smooth function looks like a line when zoomed in. Taylor expansion upgrades that: zoom in and you see a line, relax a little and it is a parabola, relax a little more and a cubic… each extra derivative lets the approximation hug the function over a wider range. The "small-angle approximation", linearization, Newton's method and the EKF in robotics are all truncated Taylor expansions.
1为什么机器人需要它Why robots need this
机器人学几乎每个地方都在做同一件事:把难算的非线性函数,在工作点附近换成容易算的多项式。
- 倒立摆的控制器按 \(\sin\theta\approx\theta\) 设计(线性化),在竖直附近稳稳地工作。
- 扩展卡尔曼滤波(EKF)把非线性的运动与观测模型在当前估计处展开到一阶。
- Newton 法、Gauss–Newton、SQP 每一步都在解"一阶或二阶泰勒模型"的最优点。
- 小旋转 \(R\approx I+[\boldsymbol\omega]_\times\),是李群里指数映射的一阶展开。
用得多,就更要知道:这个多项式在多大范围内可信,丢掉的那一项有多大。这是本节要讲清楚的。
Nearly everywhere in robotics the same trick appears: replace a hard nonlinear function by an easy polynomial near the operating point.
- A controller for an inverted pendulum is designed with \(\sin\theta\approx\theta\) (linearization) and works nicely near upright.
- The extended Kalman filter (EKF) expands the nonlinear motion and observation models to first order at the current estimate.
- Newton, Gauss–Newton and SQP each step solve for the optimum of a first- or second-order Taylor model.
- A small rotation \(R\approx I+[\boldsymbol\omega]_\times\) is the first-order expansion of the exponential map on a Lie group.
Used this much, it is all the more important to know how far the polynomial can be trusted and how large the dropped term is. This lesson makes that precise.
2直觉:每多一阶导数,多贴紧一圈Intuition: every extra derivative hugs one step tighter
想在 \(x=a\) 处"复制"一个函数。零阶:只对上高度,得到一条水平线。一阶:再对上斜率,得到切线。二阶:再对上弯曲程度,得到抛物线。三阶:对上弯曲程度的变化……每对上一阶,近似曲线就和真函数多"黏"在一起一段。
下面拖动阶数滑块,拖动琥珀色的展开点 \(a\),再拖紫色的探针 \(x\),观察右图:误差随离展开点的距离 \(|x-a|\) 缩小得有多快。
Say you want to "clone" a function at \(x=a\). Order 0: match the height only, giving a horizontal line. Order 1: also match the slope, giving the tangent. Order 2: also match the curvature, giving a parabola. Order 3: match how the curvature changes… every order matched makes the approximation "stick" to the true function over a longer stretch.
Below, drag the order slider, drag the amber expansion point \(a\), and drag the purple probe \(x\); watch the right panel: how fast the error shrinks with the distance \(|x-a|\) from the expansion point.
- 选 sin x,\(a=0\),阶数调到 7:把探针拖到 \(x=\pm\pi\) 附近,误差仍小于 0.1。Pick sin x, \(a=0\), order 7: drag the probe near \(x=\pm\pi\) and the error is still below 0.1.
- 选 ln(1+x),\(a=0\),阶数 6 以上,把探针拖到 \(x>1\):阶数越高误差越大(超出收敛半径)。Pick ln(1+x), \(a=0\), order 6 or more, drag the probe to \(x>1\): a higher order gives a larger error (beyond the radius of convergence).
泰勒多项式是"局部近似":在展开点附近,\(n\) 阶近似的误差是 \(O(|x-a|^{n+1})\),离得越近越准、阶数越高越准;但远离展开点,多项式可能很快跑偏,甚至越高阶越差(有些函数有"收敛半径",比如 \(\ln(1+x)\) 在 \(|x|>1\) 处)。
The Taylor polynomial is a local approximation: near the expansion point an order-\(n\) approximation has error \(O(|x-a|^{n+1})\); closer is better, higher order is better. Far from the point the polynomial can run away fast, even getting worse with higher order (some functions have a "radius of convergence", e.g. \(\ln(1+x)\) beyond \(|x|>1\)).
3动手玩:小角度、Newton 法、摆Play: small angles, Newton's method, the pendulum
小角度近似到底有多小?How small is "small angle"?
\(\sin\theta\approx\theta\)、\(\cos\theta\approx1\)、\(\cos\theta\approx1-\tfrac12\theta^2\) 是机器人里最常用的三个近似。右侧是它们的相对误差随角度变化的曲线(纵轴是以 10 为底的对数,−2 即 1%)。"小"得看你容忍多大误差。
\(\sin\theta\approx\theta\), \(\cos\theta\approx1\) and \(\cos\theta\approx1-\tfrac12\theta^2\) are the three most-used approximations in robotics. The curves show their relative error against the angle (vertical axis is log base 10, so −2 means 1%). How "small" is small depends on the error you can tolerate.
- 找到 \(\sin\theta\approx\theta\) 的误差刚好达到 1% 的角度(约 14°)。Find the angle where \(\sin\theta\approx\theta\) is off by exactly 1% (about 14°).
- 找到 \(\cos\theta\approx1\) 的误差达到 1% 的角度。它为什么比 sin 的小得多?Find the angle where \(\cos\theta\approx1\) reaches 1% error. Why is it so much smaller than sin's?
Newton 法:每一步都是一个泰勒模型Newton's method: every step is a Taylor model
求根:在当前点 \(x_k\) 用一阶泰勒(切线)代替 \(f\),解切线的零点作为下一个点:\(x_{k+1}=x_k-f(x_k)/f'(x_k)\)。求极小:用二阶泰勒(抛物线)代替 \(g\),跳到抛物线的顶点:\(x_{k+1}=x_k-g'(x_k)/g''(x_k)\)。拖动起点 \(x_0\),点"步进",看它怎么走,什么时候出问题。
Root finding: replace \(f\) at the current point \(x_k\) by its first-order Taylor model (the tangent) and take the tangent's zero as the next point: \(x_{k+1}=x_k-f(x_k)/f'(x_k)\). Minimization: replace \(g\) by its second-order model (a parabola) and jump to its vertex: \(x_{k+1}=x_k-g'(x_k)/g''(x_k)\). Drag the start \(x_0\), press "Step" and watch how it goes and when it breaks.
- 求根,函数 \(x^2-2\),\(x_0=1\):看到残差在 6 步内降到 \(10^{-12}\) 以下(二次收敛)。Root finding, \(x^2-2\), \(x_0=1\): see the residual fall below \(10^{-12}\) in 6 steps (quadratic convergence).
- 求根,函数 \(x^3-2x+2\),\(x_0=0\):迭代在 0 和 1 之间来回跳,永远到不了根。Root finding, \(x^3-2x+2\), \(x_0=0\): the iteration bounces between 0 and 1 forever and never reaches the root.
- 求极小,选"非凸",在 \(g''<0\) 的地方起步:这一步让 g 变大了。Minimization, pick "nonconvex" and start where \(g''<0\): the step makes \(g\) larger.
摆:线性化好用到哪里?The pendulum: how far does linearization go?
单摆满足 \(\ddot\theta=-\tfrac g\ell\sin\theta\)。线性化(\(\sin\theta\approx\theta\))得到简谐运动 \(\ddot\theta=-\tfrac g\ell\theta\),周期和振幅无关。真实的摆摆得越大,周期越长。下面把两者的轨迹放在一起,改变初始摆角。
A pendulum obeys \(\ddot\theta=-\tfrac g\ell\sin\theta\). Linearizing (\(\sin\theta\approx\theta\)) gives simple harmonic motion \(\ddot\theta=-\tfrac g\ell\theta\), whose period does not depend on amplitude. The real pendulum swings slower the higher it goes. Below, the two trajectories are drawn together; change the initial angle.
- 摆角 ≤ 10°:两条轨迹在 8 秒内几乎无法区分(最大差小于 0.5°)。Amplitude ≤ 10°: the two trajectories are indistinguishable over 8 s (maximum gap below 0.5°).
- 摆角 ≥ 90°:真实的摆周期比线性模型长 15% 以上。Amplitude ≥ 90°: the real pendulum's period is more than 15% longer than the linear model's.
4再看数学The math
提示:悬停在公式带底色的符号上,FIG 1 里对应的部分会高亮。Tip: hover the highlighted symbols; the matching part of FIG 1 lights up.
泰勒公式Taylor's formula
设 \(f\) 在 \(a\) 附近有 \(n+1\) 阶导数,则
If \(f\) has \(n+1\) derivatives near \(a\), then
展开到前几项:\(f(a)+f'(a)\,\delta+\tfrac12f''(a)\,\delta^2+\tfrac16f'''(a)\,\delta^3+\cdots\),\(\delta=x-a\)。系数里的 \(1/k!\) 是为了让多项式的 \(k\) 阶导数在 \(a\) 处恰好等于 \(f^{(k)}(a)\)。余项说明误差是 \(O(|\delta|^{n+1})\),系数由 \(f^{(n+1)}\) 控制:函数"弯"得越厉害,误差越大。
Written out: \(f(a)+f'(a)\,\delta+\tfrac12f''(a)\,\delta^2+\tfrac16f'''(a)\,\delta^3+\cdots\), \(\delta=x-a\). The \(1/k!\) makes the polynomial's \(k\)-th derivative at \(a\) equal \(f^{(k)}(a)\). The remainder says the error is \(O(|\delta|^{n+1})\) with a coefficient governed by \(f^{(n+1)}\): the more sharply the function bends, the larger the error.
值得记住的几个展开(在 0 处)A few expansions worth knowing (at 0)
| 函数Function | 展开Expansion | 适用范围Valid for |
|---|---|---|
| \(\sin x\) | \(x-\tfrac{x^3}{6}+\tfrac{x^5}{120}-\cdots\) | 所有 \(x\)all \(x\) |
| \(\cos x\) | \(1-\tfrac{x^2}{2}+\tfrac{x^4}{24}-\cdots\) | 所有 \(x\)all \(x\) |
| \(e^x\) | \(1+x+\tfrac{x^2}{2}+\tfrac{x^3}{6}+\cdots\) | 所有 \(x\)all \(x\) |
| \(\ln(1+x)\) | \(x-\tfrac{x^2}{2}+\tfrac{x^3}{3}-\cdots\) | \(|x|<1\) |
| \(\dfrac1{1-x}\) | \(1+x+x^2+x^3+\cdots\) | \(|x|<1\) |
多元函数Several variables
对 \(f:\mathbb R^n\to\mathbb R\),到二阶:
For \(f:\mathbb R^n\to\mathbb R\), up to second order:
一阶项里的梯度见 3.2 节,二阶项里的 Hessian 见 3.5 节。对向量函数 \(\mathbf f:\mathbb R^n\to\mathbb R^m\),一阶项是雅可比:\(\mathbf f(\mathbf x+\boldsymbol\delta)\approx\mathbf f(\mathbf x)+J\boldsymbol\delta\)(3.3 节),这就是 EKF 的线性化。
The gradient in the first-order term is Lesson 3.2 and the Hessian in the second-order term is Lesson 3.5. For vector functions \(\mathbf f:\mathbb R^n\to\mathbb R^m\) the first-order term is the Jacobian: \(\mathbf f(\mathbf x+\boldsymbol\delta)\approx\mathbf f(\mathbf x)+J\boldsymbol\delta\) (Lesson 3.3), which is exactly the EKF's linearization.
Newton 法的来历与速度Where Newton's method comes from, and its speed
令一阶模型 \(f(x_k)+f'(x_k)(x-x_k)=0\),解得 \(x_{k+1}=x_k-f(x_k)/f'(x_k)\)。把 \(f\) 展开到二阶可以证明:在单根附近 \(e_{k+1}\approx\dfrac{f''(x^\star)}{2f'(x^\star)}\,e_k^2\),误差被平方:\(10^{-2}\to10^{-4}\to10^{-8}\to10^{-16}\),这叫二次收敛。求极小就是对导数 \(g'\) 做 Newton 法,等价于每步最小化二阶泰勒模型。
Set the first-order model \(f(x_k)+f'(x_k)(x-x_k)=0\) and solve: \(x_{k+1}=x_k-f(x_k)/f'(x_k)\). Expanding \(f\) to second order shows that near a simple root \(e_{k+1}\approx\dfrac{f''(x^\star)}{2f'(x^\star)}\,e_k^2\): the error is squared, \(10^{-2}\to10^{-4}\to10^{-8}\to10^{-16}\), called quadratic convergence. Minimization is Newton's method on the derivative \(g'\), equivalent to minimizing the second-order Taylor model at each step.
5机器人中的它:线性化的有效区域In robotics: the validity region of a linearization
FIG 4 的结论可以直接搬到控制器设计里:
- 用线性化模型设计的控制器(PD、LQR)只在工作点附近有保证。摆角 10° 以内,模型误差不到 1%;超过 60°,线性模型已经完全不对了。
- EKF 在当前估计 \(\hat{\mathbf x}\) 处线性化。如果估计偏离真值太远(比如机器人"被绑架"),线性化点本身错了,滤波器会越修越偏,这叫不一致。
- 轨迹优化每一步用二阶泰勒模型,为防止"跳出可信范围",要加信赖域(6.5 节):限制步长不超过模型可信的半径。
- 数值上的警告:\(1-\cos\theta\) 在 \(\theta\) 很小时会因为两个近似相等的数相减而丢掉有效数字,应当写成 \(2\sin^2(\theta/2)\),或者直接用泰勒展开 \(\theta^2/2\)。
FIG 4's lesson carries straight into controller design:
- A controller (PD, LQR) designed on a linearized model is guaranteed only near the operating point. Within 10° the model error is under 1%; beyond 60° the linear model is simply wrong.
- The EKF linearizes at the current estimate \(\hat{\mathbf x}\). If the estimate is far from the truth (say the robot was "kidnapped"), the linearization point itself is wrong and the filter digs itself deeper: inconsistency.
- Trajectory optimization uses a second-order Taylor model each step; to stop it from "jumping out of the trusted region" one adds a trust region (Lesson 6.5) limiting the step to the radius where the model is reliable.
- A numerical warning: \(1-\cos\theta\) loses significant digits for tiny \(\theta\) because two nearly equal numbers are subtracted; write \(2\sin^2(\theta/2)\) instead, or use the Taylor form \(\theta^2/2\) directly.
旋转的最基本事实也是泰勒展开:绕轴 \(\hat{\mathbf n}\) 转很小的角 \(\phi\),\(R=\exp([\phi\hat{\mathbf n}]_\times)\approx I+[\phi\hat{\mathbf n}]_\times\)。IMU 积分、视觉里程计、姿态估计里随处可见"小旋转 \(\approx I+\)反对称矩阵",我们会在 5.6、5.7 节把它做严格。
The most basic fact about rotations is also a Taylor expansion: turning by a small angle \(\phi\) about an axis \(\hat{\mathbf n}\), \(R=\exp([\phi\hat{\mathbf n}]_\times)\approx I+[\phi\hat{\mathbf n}]_\times\). "Small rotation \(\approx I+\)skew-symmetric matrix" is everywhere in IMU integration, visual odometry and attitude estimation; Lessons 5.6 and 5.7 make it rigorous.
6常见误区Common pitfalls
只在收敛半径内成立,也只在离展开点不太远时成立。FIG 1 里 \(\ln(1+x)\) 在 \(x>1\) 处越展开越离谱。
True only within the radius of convergence and not too far from the expansion point. In FIG 1, \(\ln(1+x)\) gets worse the more terms you add for \(x>1\).
"一阶近似"误差是 \(\delta^2\),"二阶近似"误差是 \(\delta^3\)。\(\cos\theta\approx1\) 虽然是"零阶",误差却是 \(\theta^2/2\);而 \(\sin\theta\approx\theta\) 是"一阶",误差是 \(\theta^3/6\),因为 \(\sin\) 的二阶项恰好为零。
A "first-order approximation" has error \(\delta^2\), a "second-order" one \(\delta^3\). \(\cos\theta\approx1\) is "zeroth order" with error \(\theta^2/2\); \(\sin\theta\approx\theta\) is "first order" yet has error \(\theta^3/6\) because \(\sin\)'s second-order term happens to vanish.
它只保证"离解足够近时二次收敛"。起点不好,会来回跳(FIG 3),甚至发散。求极小时若 Hessian 不是正定,Newton 步会指向上坡或鞍点。实用的做法是配合线搜索或信赖域,或给 Hessian 加阻尼(Levenberg–Marquardt,6.6 节)。
It guarantees quadratic convergence only "close enough to the solution". From a bad start it can bounce (FIG 3) or diverge. In minimization, if the Hessian is not positive definite the Newton step can point uphill or toward a saddle. In practice combine it with line search or a trust region, or damp the Hessian (Levenberg–Marquardt, Lesson 6.6).
\(\sin\theta\approx\theta\) 要求 \(\theta\) 是弧度。\(\sin 10^\circ\approx10\) 是荒谬的。
\(\sin\theta\approx\theta\) requires \(\theta\) in radians. \(\sin 10^\circ\approx10\) is absurd.
7小练习Check yourself
8延伸阅读Further reading
- 3Blue1Brown, Essence of Calculus — 第 11 章:泰勒级数Ch. 11: Taylor series
- J. Nocedal & S. Wright, Numerical Optimization, Ch. 3, 6 — 信赖域与 Newton 类方法trust regions and Newton-type methods
- T. Barfoot, State Estimation for Robotics, Ch. 4 — 非线性系统的线性化与 EKFlinearizing nonlinear systems and the EKF
- S. Boyd & L. Vandenberghe, Convex Optimization, App. A.4 — 导数与二阶近似derivatives and second-order approximation