Computer Arithmetic Examples
Example 1
Is \(y=\sqrt{1+x^2} - 1=:f(x)\) well-conditioned?
a = np.arange(-100, 100, 1)
b = ((a ** 2 + 1) ** 0.5 + 1) / ((a ** 2 + 1) ** 0.5)
plt.plot(a, b)
plt.savefig("assets/arithmetic_examples_1.jpg")
Does \(fl(\sqrt{1+x^2}-1)\) always give an accurate result
When \(x\) is small enough so that \(fl(\sqrt{1+x^2}) = 1\), then \(\hat y = 0, y \neq 0\Rightarrow \frac{\hat y - y}{y}=\infty\)
\(fl(\sqrt{1+x^2})=1\Rightarrow x^2 < \frac{\epsilon}{2}\Rightarrow |x|\leq \sqrt{\epsilon/2}\)
When \(|x|\leq \sqrt{\epsilon/2}, x\neq 0\), this operation gives an inaccurate result, and for moderately small \(x\) this is still not so accurate
Can we change \(\sqrt{1+x^2}-1\) to a mathematically equivalent that has a much smaller f.p. error.
lemma 1 \(\sqrt{1+\delta} = 1+\hat \delta, |\hat\delta| < \epsilon/2\)
lemma 2 \((1+\delta)^{-1} = 1+\hat\delta, |\hat\delta| < \frac{1.01\epsilon}{2}\)
Claim accuracy
Note that \(\delta^{**} \leq 1.01\epsilon/2\), let the product of all \((1+\delta)\)'s, i.e. \(1 + \tilde\delta \leq \frac{7(1.01)\epsilon}{2}\)
\(2.56248\times 10^4, 2.56125\times 10^4\) agrees to 3 sig-dig (to say agree to n sig dit, the exponent must match) \(relative. error = \frac{2.56248 - 2.56125}{2.56125} = \frac{.00123}{2.56125}=1.23/2.56125\times 10^{-3}\) \(p\) sig-dig agree \(\Rightarrow 10^{-p\pm 1}\) relateive error
Example 2
Let \(x = 1\times 10^{-13}, y = 1 + x - 1, z = 4 + x - 4\)
\(r_y = (y-x)/x, r_z = (z-x)/x\) will have large relative errors
x = 1.000000000000000000000e-13
y = 1 + x - 1
z = 4 + x - 4
ry = (y - x )/x
rz = (z - x)/x
print("y =", y)
#>> y = 9.992007221626409e-14
print("z =", z)
#>> z = 1.0036416142611415e-13
print("ry =", ry)
#>> ry = -0.000799277837359144
print("rz =", rz)
#>> rz = 0.003641614261141482
Consider a system of \(\beta=10, p = 5\)
will have no error at all
However, if \(\beta = 2\)
We lost the last cut of numbers, and its about 3 significant digits
Example 3
Consider the integral \(I_n = \int_0^1 x^n e^{x-1}dx\), \(n\in \mathbb N\) Note tat \(x^n e^{x-1} > 0, \forall x \in (0, 1), I_n > 0\)
Also, \(x^ne^{x-1} > x^{n+1}e^{x-1}\) therefore this is a positive monotonic decreasing sequence.
Also, note that
Therefore we can calculate this recursively
l = [1 - 1/math.e]
for n in range(1, 24):
l.append(1 - n * l[-1])
fig, axs = plt.subplots(1, 2, figsize=(12, 6))
axs[0].plot(range(1, 20), l[:19])
axs[1].plot(range(1, 25), l);
fig.savefig("assets/arithmetic_examples_2.jpg")
print("n = 10 =>", 1e-16 * math.factorial(10))
#>> n = 10 => 3.6288e-10
print("n = 18 =>", 1e-16 * math.factorial(18))
#>> n = 18 => 0.6402373705728
Consider
Note that
Also, note that \(e^{-1} \leq e^{x-1}\leq e^0 = 1\)
So that