一、绪论
误差的基本概念
绝对误差
- 设 x∗x^*x∗ 为准确值,xxx 是 x∗x^*x∗ 的一个近似值,记 e(x)=x∗−x\pmb{{e(x)}=x^*-x}e(x)=x∗−xe(x)=x∗−xe(x)=x∗−x,称 e(x)e(x)e(x) 为近似值 xxx 的绝对误差
- 绝对误差不是误差的绝对值
- 若存在 ϵ>0\epsilon > 0ϵ>0,使得 ∣e(x)∣=∣x∗−x∣≤ϵ\pmb{|e(x)|=|x^*-x| \leq \epsilon}∣e(x)∣=∣x∗−x∣≤ϵ∣e(x)∣=∣x∗−x∣≤ϵ∣e(x)∣=∣x∗−x∣≤ϵ,则 ϵ\epsilonϵ 称为近似值的绝对误差限
相对误差
- 设 x∗x^*x∗ 为准确值,xxx 是 x∗x^*x∗ 的一个近似值,记 er(x)=x∗−xx∗=e(x)x∗\pmb{e_r(x)=\frac{x^*-x}{x^*}=\frac{e(x)}{x^*}}er(x)=x∗x∗−x=x∗e(x)er(x)=x∗x∗−x=x∗e(x)er(x)=x∗x∗−x=x∗e(x),则称 er(x)e_r(x)er(x) 为近似值 xxx 的相对误差
- 由于精确值难以求得,通常以 e‾r(x)=x∗−xx\pmb{\overline{e}_r(x)=\frac{x^*-x}{x}}er(x)=xx∗−xer(x)=xx∗−xer(x)=xx∗−x 作为相对误差
有效数
- 如果近似值 xxx 的绝对误差限是其某一位的半个单位,且该位直到 xxx 的第一个非零数字之间共有 n 位,则称 xxx 具有 n 位有效数字,用这 n 位有效数字表示的近似值称为有效数
- 如 π\piπ 的近似值取 x1=3.14x_1=3.14x1=3.14,则 ∣π−x1∣=0.00159...<0.005=12×10−2|\pi-x_1|=0.00159...<0.005=\frac{1}{2} \times 10^{-2}∣π−x1∣=0.00159...<0.005=21×10−2,所以 x1x_1x1 有 3 位有效数字
数据误差对函数值的影响
设 x1∗, x2∗x^*_1,\ x^*_2x1∗, x2∗ 为准确值,y∗=f(x1∗,x2∗), x1, x2y^*=f(x^*_1, x^*_2),\ x_1,\ x_2y∗=f(x1∗,x2∗), x1, x2 为对应的近似值,y=f(x1, x2)y=f(x_1,\ x_2)y=f(x1, x2),由二元函数 Taylor 展开得
e(y)=y∗−y=f(x1∗,x2∗)−f(x1,x2)≈∂f(x1,x2)∂x1(x1∗−x1)+∂f(x1,x2)∂x2(x2∗−x2)≈∂f(x1,x2)∂x1e(x1)+∂f(x1,x2)∂x2e(x2) ①er(y)=e(y)y≈∂f(x1,x2)∂x1x1f(x1,x2)er(x1)+∂f(x1,x2)∂x2x2f(x1,x2)er(x2) ②
\begin{aligned}
e(y) &= y^*-y=f(x_1^*,x_2^*)-f(x_1, x_2) \\
&\approx \frac{\partial f(x_1, x_2)}{\partial x_1}(x^*_1-x_1)+\frac{\partial f(x_1, x_2)}{\partial x_2}(x^*_2-x_2) \\
&\approx \frac{\partial f(x_1, x_2)}{\partial x_1}e(x_1)+\frac{\partial f(x_1, x_2)}{\partial x_2}e(x_2) \ ①\\
\\
e_r(y) &= \frac{e(y)}{y} \approx \frac{\partial f(x_1, x_2)}{\partial x_1}
\frac{x_1}{f(x_1, x_2)}e_r(x_1)+\frac{\partial f(x_1, x_2)}{\partial x_2}\frac{x_2}{f(x_1, x_2)}e_r(x_2)\ ②
\end{aligned}
e(y)er(y)=y∗−y=f(x1∗,x2∗)−f(x1,x2)≈∂x1∂f(x1,x2)(x1∗−x1)+∂x2∂f(x1,x2)(x2∗−x2)≈∂x1∂f(x1,x2)e(x1)+∂x2∂f(x1,x2)e(x2) ①=ye(y)≈∂x1∂f(x1,x2)f(x1,x2)x1er(x1)+∂x2∂f(x1,x2)f(x1,x2)x2er(x2) ②
利用 ① 和 ② 可得
e(x1+x2)=e(x1)+e(x2)e(x1−x2)=e(x1)−e(x2)e(x1x2)≈x2e(x1)+x1e(x2)e(x1x2)≈1x2e(x1)−x1x22e(x2)er(x1+x2)≈x1x1+x2er(x1)+x2x1+x2er(x2)er(x1−x2)≈x1x1−x2er(x1)−x2x1−x2er(x2)er(x1x2)≈er(x1)+er(x2)er(x1x2)≈er(x1)−er(x2)
\begin{aligned}
&e(x_1+x_2)=e(x_1)+e(x_2) \\
&e(x_1-x_2) = e(x_1)-e(x_2) \\
&e(x_1x_2) \approx x_2e(x_1)+x_1e(x_2) \\
&e(\frac{x_1}{x_2}) \approx \frac{1}{x_2}e(x_1)-\frac{x_1}{x_2^2}e(x_2) \\
&e_r(x_1+x_2) \approx \frac{x_1}{x_1+x_2}e_r(x_1)+\frac{x_2}{x_1+x_2}e_r(x_2) \\
&e_r(x_1-x_2) \approx \frac{x_1}{x_1-x_2}e_r(x_1)-\frac{x_2}{x_1-x_2}e_r(x_2) \\
&e_r(x_1x_2) \approx e_r(x_1)+e_r(x_2) \\
&e_r(\frac{x_1}{x_2}) \approx e_r(x_1)-e_r(x_2)
\end{aligned}
e(x1+x2)=e(x1)+e(x2)e(x1−x2)=e(x1)−e(x2)e(x1x2)≈x2e(x1)+x1e(x2)e(x2x1)≈x21e(x1)−x22x1e(x2)er(x1+x2)≈x1+x2x1er(x1)+x1+x2x2er(x2)er(x1−x2)≈x1−x2x1er(x1)−x1−x2x2er(x2)er(x1x2)≈er(x1)+er(x2)er(x2x1)≈er(x1)−er(x2)
例子
设 x=0.1230, y=1.234x=0.1230,\ y=1.234x=0.1230, y=1.234 均为有效数字,试分析 (x−y)(x-y)(x−y) 和 x2cos(y)x^2 \cos(y)x2cos(y) 得绝对误差限、相对误差限和有效数字
解:由条件有 ∣e(x)∣≤12×10−4, ∣e(y)∣≤12×10−3|e(x)| \leq \frac{1}{2} \times 10^{-4},\ |e(y)| \leq \frac{1}{2} \times 10^{-3}∣e(x)∣≤21×10−4, ∣e(y)∣≤21×10−3,则
∣e(x−y)∣=∣e(x)−e(y)∣≤∣e(x)∣+∣e(y)∣≤0.55×10−3∣er(x−y)∣=∣e(x−y)x−y∣≤∣0.55×10−30.123−1.234∣=0.4950×10−3∣e(x2cosy)∣≈∣2xcosye(x)−x2sinye(y)∣≤2x∣cosy∣∣e(x)∣+x2∣siny∣∣e(y)∣≤0.1120×10−4∣er(x2cosy)∣=∣e(x2cosy)x2cosy∣≤0.2240×10−2
\begin{aligned}
|e(x-y)| &= |e(x)-e(y)| \leq |e(x)|+|e(y)| \leq 0.55 \times 10^{-3} \\
|e_r(x-y)| &= |\frac{e(x-y)}{x-y}| \leq |\frac{0.55 \times 10^{-3}}{0.123-1.234}|=0.4950 \times 10^{-3} \\
|e(x^2\cos{y})| &\approx |2x\cos{y}e(x)-x^2\sin{y}e(y)| \\
&\leq 2x|\cos{y}||e(x)|+x^2|\sin{y}||e(y)| \leq 0.1120 \times 10^{-4} \\
|e_r(x^2\cos{y})| &= |\frac{e(x^2\cos{y})}{x^2\cos{y}}| \leq 0.2240 \times 10^{-2}
\end{aligned}
∣e(x−y)∣∣er(x−y)∣∣e(x2cosy)∣∣er(x2cosy)∣=∣e(x)−e(y)∣≤∣e(x)∣+∣e(y)∣≤0.55×10−3=∣x−ye(x−y)∣≤∣0.123−1.2340.55×10−3∣=0.4950×10−3≈∣2xcosye(x)−x2sinye(y)∣≤2x∣cosy∣∣e(x)∣+x2∣siny∣∣e(y)∣≤0.1120×10−4=∣x2cosye(x2cosy)∣≤0.2240×10−2
由上可知,
∣e(x−y)∣≤0.55×10−3<12×10−2, x−y=−1.111|e(x-y)|\leq0.55 \times10^{-3} \lt \frac{1}{2} \times 10^{-2}, \ x-y=-1.111∣e(x−y)∣≤0.55×10−3<21×10−2, x−y=−1.111,所以 (x−y)(x-y)(x−y) 具有 3 位有效数字
∣e(x2cosy)∣<12×10−4, x2cosy=0.0049996|e(x^2\cos{y})| \lt \frac{1}{2} \times 10^{-4},\ x^2\cos{y}=0.0049996∣e(x2cosy)∣<21×10−4, x2cosy=0.0049996,所以 x2cosyx^2\cos{y}x2cosy 具有 2 位有效数字
数值稳定性
- 对于某一算法,如果初始数据很小的误差仅使最终结果产生较小的误差,则称该算法是数值稳定的,否则称为数值不稳定的
- 例:建立计算积分 ∫01xnx+5dx, n=0,1,2,...,10\int_0^1\frac{x^n}{x+5}dx,\ n=0,1,2,...,10∫01x+5xndx, n=0,1,2,...,10 的递推公式,并研究其误差传播
In=∫01xn+5xn−1−5xn−1x+5dx=∫01xn−1dx−5∫01xn−1x+5dx=1n−5In−1→不稳定若上式转换为:In−1=15(1n−In), (n=10,9,...,2,1)→稳定 \begin{aligned} I_n &= \int_0^1\frac{x^n+5x^{n-1}-5x^{n-1}}{x+5}dx \\ &= \int_0^1x^{n-1}dx - 5\int_0^1\frac{x^{n-1}}{x+5}dx \\ &= \frac{1}{n} -5I_{n-1} \rightarrow 不稳定 \\ 若上式转换为:I_{n-1} &= \frac{1}{5}(\frac{1}{n}-I_n),\ (n=10,9,...,2,1) \rightarrow 稳定 \end{aligned} In若上式转换为:In−1=∫01x+5xn+5xn−1−5xn−1dx=∫01xn−1dx−5∫01x+5xn−1dx=n1−5In−1→不稳定=51(n1−In), (n=10,9,...,2,1)→稳定
- 如果输入数据有微小的误差,引起输出数据只有微小的改变,称这类问题为良态的,否则称为病态的
实际计算中应注意的一些问题
- 尽量避免除数绝对值远远小于被除数绝对值
- 尽量避免两个相近的数相减
- 防止大数 “吃” 小数
- 简化计算步骤,减少运算次数
秦九韶算法
将多项式 f(x)=a0xn+a1xn−1+...+an−1x+anf(x)=a_0x^n+a_1x^{n-1}+...+a_{n-1}x+a_nf(x)=a0xn+a1xn−1+...+an−1x+an 转换为如下嵌套形式:
f(x)=(...((a0x+a1)x+a2)x+...+an−1)x+an
f(x) = (...((a_0x+a_1)x+a_2)x+...+a_{n-1})x+a_n
f(x)=(...((a0x+a1)x+a2)x+...+an−1)x+an
改成表格形式如下:
a0a_0a0 | a1a_1a1 | a2a_2a2 | … | an−1a_{n-1}an−1 | ana_nan | |
---|---|---|---|---|---|---|
x=x0x=x_0x=x0 | b0x0b_0x_0b0x0 | b1x0b_1x_0b1x0 | … | bn−2x0b_{n-2}x_0bn−2x0 | bn−1x0b_{n-1}x_0bn−1x0 | |
b0b_0b0 | b1b_1b1 | b2b_2b2 | … | bn−1b_{n-1}bn−1 | bn=f(x0)b_n=f(x_0)bn=f(x0) |
例:已知 f(x)=8x5+4x3−9x+1f(x)=8x^5+4x^3-9x+1f(x)=8x5+4x3−9x+1 用秦九韶法求 f(3)f(3)f(3)
解:使用表格可得
888 | 000 | 444 | 000 | −9-9−9 | 111 | |
---|---|---|---|---|---|---|
x0=3x_0=3x0=3 | 3×8=243\times 8 = 243×8=24 | 24×3=7224 \times 3 = 7224×3=72 | 76×3=22876 \times 3 = 22876×3=228 | 228×3=684228 \times 3 = 684228×3=684 | 675×3=2025675 \times 3=2025675×3=2025 | |
888 | 24+0=2424+0=2424+0=24 | 72+4=7672+4=7672+4=76 | 228+0=228228+0=228228+0=228 | 684+(−9)=675684+(-9)=675684+(−9)=675 | 2025+1=f(3)2025+1=f(3)2025+1=f(3) |
故 f(3)=2026f(3)=2026f(3)=2026