english improved
This commit is contained in:
@@ -16,13 +16,15 @@ using InteractiveUtils
|
||||
flush(stdout)
|
||||
```
|
||||
|
||||
# A Case Study in Floating-Point Arithmetic Stability
|
||||
# A Case Study on Floating-Point Arithmetic Stability
|
||||
|
||||
This chapter is inspired by the example in *Christoph Überhuber*, "Computer-Numerik" Vol. 1, Springer 1995, Chap. 2.3.
|
||||
|
||||
## Calculation of $\pi$ According to Archimedes
|
||||
|
||||
A lower bound for $2\pi$—the circumference of the unit circle—is obtained by summing
|
||||
A lower bound for $2\pi$ -- the circumference of the unit circle -- is obtained by summing
|
||||
the side lengths of a regular $n$-gon inscribed in the unit circle.
|
||||
The figure on the left shows how one can iteratively double the number of vertices starting from a square with side length $$s_4=\sqrt{2}$$.
|
||||
The figure on the left illustrates the iterative doubling of the number of vertices starting from a square with side length $$s_4=\sqrt{2}$$.
|
||||
|
||||
:::{.narrow}
|
||||
|
||||
@@ -39,7 +41,7 @@ With
|
||||
$|\overline{AC}|= s_{n},\quad |\overline{AB}|= |\overline{BC}|= s_{2n},\quad |\overline{MN}| =a, \quad |\overline{NB}| =1-a,$ the Pythagorean theorem applied to triangles $MNA$ and
|
||||
$NBA$ respectively yields
|
||||
$$
|
||||
a^2 + \left(\frac{s_{n}}{2}\right)^2 = 1\qquad\text{resp.} \qquad
|
||||
a^2 + \left(\frac{s_{n}}{2}\right)^2 = 1\qquad\text{and } \qquad
|
||||
(1-a)^2 + \left(\frac{s_{n}}{2}\right)^2 = s_{2n}^2
|
||||
$$
|
||||
Elimination of $a$ gives the recursion
|
||||
@@ -47,8 +49,7 @@ $$
|
||||
s_{2n} = \sqrt{2-\sqrt{4-s_n^2}} \qquad\text{with initial value}\qquad
|
||||
s_4=\sqrt{2}
|
||||
$$
|
||||
for the length $s_n$ of one side of the inscribed regular
|
||||
$n$-gon.
|
||||
for the length $s_n$ of one side of the inscribed regular $n$-gon.
|
||||
|
||||
The sequence $(n\cdot s_n)$ converges monotonically from below to the limit $2\pi$:
|
||||
$$
|
||||
@@ -59,7 +60,9 @@ $$
|
||||
\epsilon_n = \left| \frac{n\, s_n-2 \pi}{2\pi} \right|
|
||||
$$
|
||||
|
||||
## Two Iteration Formulas^[by Christoph Überhuber, "Computer-Numerik" Vol. 1, Springer 1995, Chap. 2.3]. The equation
|
||||
## Two Iteration Formulas
|
||||
|
||||
The equation
|
||||
$$
|
||||
s_{2n} = \sqrt{2-\sqrt{4-s_n^2}}\qquad \qquad \text{(Iteration A)}
|
||||
$$
|
||||
@@ -68,9 +71,9 @@ $$
|
||||
s_{2n} = \frac{s_n}{\sqrt{2+\sqrt{4-s_n^2}}} \qquad \qquad \text{(Iteration B)}
|
||||
$$
|
||||
|
||||
(Please verify!)
|
||||
|
||||
However, Iteration A is ill-conditioned and numerically unstable, as the following code demonstrates. The output shows the respective approximation for $\pi$.
|
||||
|
||||
However, Iteration A is ill-conditioned and numerically unstable, as the following code demonstrates. The output shows the approximation for $\pi$ for both formulae iteration.
|
||||
|
||||
```{julia}
|
||||
using Printf
|
||||
@@ -102,7 +105,7 @@ for i in 3:35
|
||||
end
|
||||
```
|
||||
|
||||
While Iteration B stabilizes at a value correct within machine precision, Iteration A becomes unstable. A plot of the relative errors $\epsilon_i$ confirms this:
|
||||
While Iteration B stabilizes to a value accurate within machine precision, Iteration A is unstable and diverges. A plot of the relative errors $\epsilon_i$ confirms this:
|
||||
|
||||
```{julia}
|
||||
using PlotlyJS
|
||||
@@ -132,8 +135,8 @@ Further iterations do not improve the result; it stabilizes at a relative error
|
||||
|
||||
Iteration A is unstable; already at $i=16$, the relative error begins to grow again.
|
||||
|
||||
The cause is a typical cancellation effect. The side lengths $s_n$ become very small, very quickly. Thus
|
||||
$a_n=\sqrt{4-s_n^2}$ is only slightly smaller than 2, and when computing $s_{2n}=\sqrt{2-a_n}$, a typical cancellation effect occurs.
|
||||
The cause is a typical numerical cancellation effect. The side lengths $s_n$ become very small very quickly. Thus
|
||||
$a_n=\sqrt{4-s_n^2}$ is only slightly smaller than 2, and computing $s_{2n}=\sqrt{2-a_n}$ leads to a catastrophic cancellation.
|
||||
|
||||
```{julia}
|
||||
setprecision(80) # precision for BigFloat
|
||||
@@ -142,7 +145,7 @@ s = sqrt(BigFloat(2))
|
||||
|
||||
@printf " a = √(4-s^2) as BigFloat and as Float64\n\n"
|
||||
|
||||
for $$i=$ 3:44$
|
||||
for i= 3:44
|
||||
s = iterationA(s)
|
||||
x = sqrt(4-s^2)
|
||||
if i > 20
|
||||
@@ -151,7 +154,8 @@ for $$i=$ 3:44$
|
||||
end
|
||||
```
|
||||
|
||||
One sees the decrease in the number of significant digits. One also sees that using `BigFloat` with a mantissa length of 80 bits only slightly delays the onset of the cancellation effect.
|
||||
This demonstrates the loss of significant digits. It also shows that using `BigFloat` with 80 bits of precission (mantissa length) only slightly delays
|
||||
the onset of the cancellation effect.
|
||||
|
||||
**Countermeasures against unstable algorithms typically require better, stable algorithms, not more precise machine numbers.**
|
||||
|
||||
|
||||
Reference in New Issue
Block a user