积分与数值积分Integration & Numerical Integration
导数问"变化有多快",积分问"累计变了多少"。速度累加成位置,加速度累加成速度,力累加成冲量。计算机里没有"无穷小",只能一小步一小步地加,所以怎么加、步子多大、误差多少,就成了机器人里里程计和惯导漂移的根源。 The derivative asks "how fast is it changing"; the integral asks "how much has it changed in total". Speed accumulates into position, acceleration into velocity, force into impulse. A computer has no "infinitesimals" and can only add small steps, so how to add, how big a step, and how much error decide how odometry and inertial navigation drift.
1为什么机器人需要它Why robots need this
机器人"知道自己在哪"的大部分办法,都是积分。轮速积分得到里程计位置,陀螺仪的角速度积分得到朝向,加速度计积分两次得到位移。轨迹规划里,给定加加速度或加速度的曲线,也要积分才能得到每一时刻的位置。
问题是积分会累积误差:每一步多一点点偏差,加起来越来越大。这一节先看清积分是什么,再看数值积分有哪些办法、各自多准,最后看一个机器人最怕的现象:漂移。
Most of the ways a robot "knows where it is" are integrals. Integrating wheel speed gives odometry, integrating a gyro's angular rate gives heading, integrating an accelerometer twice gives displacement. In trajectory planning, a given acceleration or jerk profile needs integrating to produce the position at every instant.
The catch is that integration accumulates error: a small bias at every step adds up to a large one. This lesson first makes clear what an integral is, then looks at numerical rules and how accurate each is, and finally at the phenomenon robots fear most: drift.
2直觉:曲线下的面积就是累计量Intuition: the area under the curve is the accumulated amount
一辆车以 60 km/h 开 2 小时,走了 \(60\times2=120\) km:这是"速度 × 时间",也是速度曲线下一个长方形的面积。速度变来变去时,把时间切成许多小段,每段里近似当作匀速,把这些小长方形的面积加起来,就是黎曼和。段切得越细,和越接近真实的累计量,极限就是积分。
下面选一种"取高度"的方式,调整分段数 \(n\),观察和与精确值之间的差距。
A car at 60 km/h for 2 hours covers \(60\times2=120\) km: "speed × time", also the area of one rectangle under the speed curve. When speed varies, cut time into many small slices, treat each slice as constant speed, and add up the small rectangles' areas: that is a Riemann sum. The finer the slices, the closer the sum gets to the true accumulated amount, and the limit is the integral.
Below, pick a way of "choosing the height", change the number of slices \(n\), and watch the gap to the exact value.
- 在"光滑"曲线上,用梯形法或中点法,只用不超过 8 段,把相对误差压到 0.5% 以下。On the "smooth" curve, use the trapezoid or midpoint rule with at most 8 slices to get the relative error under 0.5%.
- 用左矩形法,设一个偶数 n,看到误差比接近 2。With the left rule and an even n, see the error ratio near 2.
- 换成梯形法,看到误差比接近 4:同样加倍工作量,精度提高得多得多。Switch to the trapezoid rule and see the ratio near 4: the same doubled effort buys much more accuracy.
3动手玩:积分与导数互为逆运算Play: integration and differentiation undo each other
把右端点 \(b\) 拖来拖去,面积 \(F(b)=\int_0^b f(t)\,\mathrm dt\) 就是一个关于 \(b\) 的函数。它长什么样?右图画出了 \(F\)。仔细看琥珀色切线的斜率:它恰好等于左图在 \(b\) 处的高度 \(f(b)\)。这就是微积分基本定理。
Drag the right endpoint \(b\) around and the area \(F(b)=\int_0^b f(t)\,\mathrm dt\) becomes a function of \(b\). What does it look like? The right panel plots \(F\). Look at the slope of the amber tangent: it equals exactly the height \(f(b)\) of the left graph at \(b\). That is the fundamental theorem of calculus.
- 把 b 拖到 F 的最高点:此时 f 刚好从正变负。Drag b to the top of F: there f has just crossed from positive to negative.
- 让 F(b) 变成负数(走过了"回头路")。Make F(b) negative (the "return trip" has outweighed the way out).
哪种加法更划算?Which way of adding pays off best?
不同的规则,误差随 \(n\) 缩小的速度大不相同。下图是误差对 \(n\) 的双对数图,直线的斜率就是"收敛阶"。左矩形法是 1 阶,梯形法和中点法是 2 阶,Simpson 法是 4 阶:\(n\) 增大 10 倍,误差分别缩小 10、100、10000 倍。Simpson 的曲线最后变平,是因为碰到了计算机的舍入误差(约 \(10^{-16}\))。切换到不光滑的函数看看会发生什么。
Different rules shrink their error at very different speeds as \(n\) grows. The log–log plot below shows the error against \(n\); the slope of each line is its "order of convergence". The left rule is first order, trapezoid and midpoint are second order, and Simpson is fourth order: a tenfold increase in \(n\) cuts the error by 10, 100 and 10000. The Simpson curve flattens at the end because it hits the computer's round-off (about \(10^{-16}\)). Switch to non-smooth functions to see what happens.
- 依次查看三个函数,直到读数里每种方法的阶数都看过。Visit all three functions to see how the measured orders change.
4再看数学The math
提示:悬停在公式带底色的符号上,FIG 1 里对应的部分会高亮。Tip: hover the highlighted symbols; the matching part of FIG 1 lights up.
定积分The definite integral
\(t_i^*\) 是第 \(i\) 段里选来取高度的点:左端点、右端点、中点,极限都相同(只要 \(f\) 足够规整),差别在于有限 \(n\) 时的误差。积分是线性的:\(\int(\alpha f+g)=\alpha\int f+\int g\)。
\(t_i^*\) is the point in slice \(i\) where the height is sampled: left end, right end or midpoint all share the same limit (for a reasonably regular \(f\)); they differ in the error at finite \(n\). The integral is linear: \(\int(\alpha f+g)=\alpha\int f+\int g\).
微积分基本定理The fundamental theorem of calculus
第一式:累计量的变化率就是被累计的东西本身(FIG 2 的切线)。第二式:把变化率累加起来,得到总变化,即"位置的变化等于速度的积分"。这使得有原函数时,定积分可以精确地算出来。机器人里的积分多数没有解析原函数(速度是传感器读数),只能用数值方法。
First form: the rate of change of the accumulated amount is the integrand itself (FIG 2's tangent). Second form: summing rates of change gives the total change, i.e. "the change in position equals the integral of velocity". With an antiderivative at hand a definite integral can be computed exactly. In robotics most integrands have no closed-form antiderivative (the velocity is a sensor stream), so we use numerical rules.
常用数值规则The usual numerical rules
| 规则Rule | 每段近似Per-slice approximation | 误差阶Order |
|---|---|---|
| 左矩形Left rectangle | \(f(t_i)\,\Delta t\) | \(O(\Delta t)\) |
| 中点Midpoint | \(f(t_i+\tfrac{\Delta t}2)\,\Delta t\) | \(O(\Delta t^2)\) |
| 梯形Trapezoid | \(\tfrac12\big(f(t_i)+f(t_{i+1})\big)\Delta t\) | \(O(\Delta t^2)\) |
| Simpson | \(\tfrac{\Delta t}{6}\big(f(t_i)+4f(t_i+\tfrac{\Delta t}2)+f(t_{i+1})\big)\) | \(O(\Delta t^4)\) |
阶数的来源是泰勒展开(2.4 节):中点法的一阶误差项恰好抵消,梯形法对线性函数精确,Simpson 法对三次多项式精确。
The orders come from Taylor expansion (Lesson 2.4): the midpoint rule's first-order error term cancels, the trapezoid rule is exact for linear functions, and Simpson's rule is exact for cubic polynomials.
机器人里的"积分"常常是边走边积:\(\dot x=f(x,t)\),下一时刻的值依赖当前值。最简单的做法 \(x_{k+1}=x_k+f(x_k,t_k)\,\Delta t\) 叫欧拉法,本质是左矩形法,一阶精度。更高阶的方法见 2.5 节。
In robotics "integration" is often integrate as you go: \(\dot x=f(x,t)\), with the next value depending on the current one. The simplest recipe \(x_{k+1}=x_k+f(x_k,t_k)\,\Delta t\) is called Euler's method; it is the left rectangle rule in disguise, first order. Higher-order methods come in Lesson 2.5.
5机器人中的它:航位推算与惯导漂移In robotics: dead reckoning and inertial drift
航位推算(dead reckoning):已知前进速度 \(v\) 和转速 \(\omega\),用积分推出位姿:\(\dot x=v\cos\theta,\ \dot y=v\sin\theta,\ \dot\theta=\omega\)。下面让机器人以 \(v=1.5\) m/s、时变的 \(\omega(t)=0.9+0.4\sin 0.8t\) 行驶 20 秒。灰色是(用极小步长算出的)准确路径,其余是用步长 \(\Delta t\) 算出的结果。欧拉法每步用步初的朝向前进,可这一步里机器人一直在转,合适的朝向应该是步子中间的那个,于是每步带来约 \(\tfrac12\omega\Delta t\) 的朝向滞后,整条路径系统性地偏离。中点法用半步之后的朝向,这个滞后基本抵消。
Dead reckoning: given forward speed \(v\) and turn rate \(\omega\), integrate for the pose: \(\dot x=v\cos\theta,\ \dot y=v\sin\theta,\ \dot\theta=\omega\). Below, the robot drives for 20 s at \(v=1.5\) m/s with a time-varying \(\omega(t)=0.9+0.4\sin 0.8t\). Gray is the accurate path (computed with a tiny step); the rest use step \(\Delta t\). Euler advances with the heading at the start of each step, yet the robot keeps turning during the step, so the right heading is the one in the middle; each step thus carries a heading lag of about \(\tfrac12\omega\Delta t\) and the whole path is systematically off. The midpoint rule uses the heading half a step ahead and cancels most of that lag.
- 把步长 Δt 调到 0.4 s 以上:欧拉法的终点误差超过 0.5 m,而中点法不到它的 1/4。Raise Δt to 0.4 s or more: Euler ends more than 0.5 m off while midpoint is under a quarter of that.
- 加入 2°/s 以上的陀螺偏置:终点误差超过 1.5 m,并随时间不断增大。Add a gyro bias of 2°/s or more: the end error exceeds 1.5 m and keeps growing in time.
另一个更典型的例子是惯性导航:把加速度计读数积分两次得到位移。传感器哪怕有很小的常值偏置 \(b\),位置误差也会以 \(\tfrac12 b t^2\) 增长,时间多一倍,误差多四倍;再加上噪声,误差还会按随机游走扩散。试试看,几十秒之后会漂到多远:
The classic case is inertial navigation: integrate accelerometer readings twice to get displacement. Even a tiny constant bias \(b\) makes the position error grow like \(\tfrac12 b t^2\): double the time, quadruple the error; noise then adds a random-walk spread. See how far it drifts in a few tens of seconds:
- 让一个"静止"的机器人在 30 s 内"漂"出 10 m 以上。Make a "motionless" robot "drift" more than 10 m in 30 s.
没有人把原始积分直接拿来用:里程计要靠 IMU、视觉或 GPS 周期性地校正(卡尔曼滤波,4.8 节),误差的增长规律(线性、二次、随机游走)决定了校正必须多频繁。三维里的朝向不能直接把角速度逐分量积分,要用李群上的指数映射(5.6 节)。
Nobody uses raw integrals directly: odometry is corrected periodically by IMU, vision or GPS (Kalman filtering, Lesson 4.8), and how the error grows (linear, quadratic, random walk) dictates how often corrections are needed. In 3D, orientation cannot be obtained by integrating angular-velocity components one by one; it needs the exponential map on a Lie group (Lesson 5.6).
6常见误区Common pitfalls
步长小到一定程度,舍入误差反而主导。FIG 3 里 Simpson 曲线最后变平就是这样。另外,传感器采样率是固定的,积分的"步长"往往就是采样周期,你并不能随意选。
Below some step size, round-off dominates instead, as the flattening tail of the Simpson curve in FIG 3 shows. Also, sensor rates are fixed, and the integration "step" is often just the sampling period; you cannot always choose it.
随机噪声确实有一部分相互抵消,但偏置不会,它每一步都往同一个方向推。偏置积一次线性增长,积两次平方增长。这也是为什么零偏标定对 IMU 如此重要。
Random noise does partly cancel, but a bias never does: it pushes the same way every step. One integration turns it into linear growth, two into quadratic growth. That is why zero-bias calibration is so important for an IMU.
传感器时间戳有抖动、丢帧。把 \(\Delta t\) 当成常数会在积分里引入系统误差。应该用时间戳之差,并检查异常大的间隔。
Sensor timestamps jitter and frames drop. Treating \(\Delta t\) as constant injects a systematic error. Use timestamp differences and watch for abnormally large gaps.
高阶方法的优势只在被积函数光滑时才有。接触、碰撞、限幅、符号函数这类"拐点"会把它的收敛阶拉回到 1 或 2 阶(FIG 3 的"拐点")。这些地方要么把区间在拐点处切开,要么用事件检测。
The advantage of high-order rules requires a smooth integrand. Kinks from contact, collisions, saturation and sign functions drag the order back to 1 or 2 (the "kink" of FIG 3). Split the interval at the kink or use event detection.
7小练习Check yourself
8延伸阅读Further reading
- 3Blue1Brown, Essence of Calculus — 第 8 章:积分与导数的关系Ch. 8: integration and its relation to the derivative
- G. Strang, Calculus (MIT OpenCourseWare) — 积分与基本定理integrals and the fundamental theorem
- L. N. Trefethen, Approximation Theory and Approximation Practice — 数值积分与高阶规则numerical quadrature and high-order rules
- T. Barfoot, State Estimation for Robotics — 运动模型与里程计误差的累积motion models and how odometry error accumulates
- O. Woodman, An introduction to inertial navigation, Univ. of Cambridge Tech. Rep. UCAM-CL-TR-696 — 惯导误差的来源与增长sources and growth of inertial-navigation error