# round-off and truncation error
# 舍入和截断误差
1. Round-off errors are due to approximate representation of floating-point number
from math import pi
print(pi)
3.141592653589793
π \pi π=3.141592653589793238462643…
2. subtractive cancellation
x = 1 x=1 x=1
y = 1 + 1 0 − 15 2 y=1+10^{-15}\sqrt{2} y=1+10−152
from math import sqrt
x = 1.0
y = 1.0 + 1e-15 * sqrt(2)
dt = 1e-15 * sqrt(2)
dn = y - x
print("dt = ", dt)
print("dn = ", dn)
print("Relative error: ", (dt - dn) / dt)
dt = 1.4142135623730953e-15
dn = 1.3322676295501878e-15
Relative error: 0.05794452478973511
similar accumulation of round-off in addition(large number + small number).
3. Truncation Errors
Truncation errors are created by truncating the math.
Example: MacLaurin Series for exponential function
e x = ∑ n = 0 ∞ x n n ! e^x=\sum_{n=0}^{\infty}\frac{x^n}{n!} ex=n=0∑∞n!xn
Now, we only use three terms
e x = ∑ n = 0 ∞ x n n ! ≈ 1 + x + x 2 2 ! e^x=\sum_{n=0}^{\infty}\frac{x^n}{n!}\approx 1 + x + \frac{x^2}{2!} ex=n=0∑∞n!xn≈1+x+2!x2
4. Numerical Differentiation and numerical errors
definition of true derivative
f ′ ( x ) = lim d x → 0 f ( x + d x ) − f ( x ) d x f^{'}(x)=\lim_{dx\rightarrow 0}\frac{f(x+dx)-f(x)}{dx} f′(x)=dx→0limdxf(x+dx)−f(x)
approximation of derivative(numerical derivative)
f ′ ( x i ) = f ( x i + 1 ) − f ( x i ) x i