english translation started
This commit is contained in:
108
chapters/Pi2.qmd
108
chapters/Pi2.qmd
@@ -9,20 +9,26 @@ engine: julia
|
||||
using InteractiveUtils
|
||||
```
|
||||
|
||||
```{julia}
|
||||
#| error: false
|
||||
#| echo: false
|
||||
#| output: false
|
||||
flush(stdout)
|
||||
```
|
||||
|
||||
# Ein Beispiel zur Stabilität von Gleitkommaarithmetik
|
||||
# Example of Floating-Point Arithmetic Stability
|
||||
|
||||
|
||||
|
||||
## Berechnung von $\pi$ nach Archimedes
|
||||
## Calculation of $\pi$ according to Archimedes
|
||||
|
||||
Eine untere Schranke für $2\pi$, den Umfang des Einheitskreises, erhält man durch die
|
||||
Summe der Seitenlängen eines dem Einheitskreis eingeschriebenen regelmäßigen $n$-Ecks.
|
||||
Die Abbildung links zeigt, wie man beginnend mit einem Viereck der Seitenlänge $s_4=\sqrt{2}$ die Eckenzahl iterativ verdoppelt.
|
||||
A lower bound for $2\pi$, the circumference of the unit circle, is obtained by the
|
||||
sum of 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}$.
|
||||
|
||||
:::{.narrow}
|
||||
|
||||
| Abb. 1 | Abb.2 |
|
||||
| Fig. 1 | Fig. 2 |
|
||||
| :-: | :-: |
|
||||
|  |  |
|
||||
: {tbl-colwidths="[57,43]"}
|
||||
@@ -30,51 +36,49 @@ Summe der Seitenlängen eines dem Einheitskreis eingeschriebenen regelmäßigen
|
||||
:::
|
||||
|
||||
|
||||
Die zweite Abbildung zeigt die Geometrie der Eckenverdoppelung.
|
||||
The second figure shows the geometry of the vertex doubling.
|
||||
|
||||
Mit
|
||||
$|\overline{AC}|= s_{n},\quad |\overline{AB}|= |\overline{BC}|= s_{2n},\quad |\overline{MN}| =a, \quad |\overline{NB}| =1-a,$ liefert Pythagoras für die Dreiecke $MNA$ und
|
||||
$NBA$ jeweils
|
||||
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{bzw.} \qquad
|
||||
a^2 + \left(\frac{s_{n}}{2}\right)^2 = 1\qquad\text{resp.} \qquad
|
||||
(1-a)^2 + \left(\frac{s_{n}}{2}\right)^2 = s_{2n}^2
|
||||
$$
|
||||
Elimination von $a$ liefert die Rekursion
|
||||
Elimination of $a$ gives the recursion
|
||||
$$
|
||||
s_{2n} = \sqrt{2-\sqrt{4-s_n^2}} \qquad\text{mit Startwert}\qquad
|
||||
s_4=\sqrt{2}
|
||||
s_{2n} = \sqrt{2-\sqrt{4-s_n^2}} \qquad\text{with initial value}\qquad
|
||||
s_4=\sqrt{2}
|
||||
$$
|
||||
für die Länge $s_n$ **einer** Seite des eingeschriebenen regelmäßigen
|
||||
$n$-Ecks.
|
||||
for the length $s_n$ **of one** side of the inscribed regular
|
||||
$n$-gon.
|
||||
|
||||
|
||||
Die Folge $(n\cdot s_n)$
|
||||
konvergiert monoton von unten gegen den
|
||||
Grenzwert $2\pi$:
|
||||
The sequence $(n\cdot s_n)$
|
||||
converges monotonically from below to the
|
||||
limit $2\pi$:
|
||||
$$
|
||||
n\, s_n \rightarrow 2\pi % \qquad\text{und} \qquad {n c_n}\downarrow 2\pi
|
||||
n\, s_n \rightarrow 2\pi % \qquad\text{and} \qquad {n c_n}\downarrow 2\pi
|
||||
$$
|
||||
Der relative Fehler der Approximation von 2π durch ein $n$-Eck ist:
|
||||
The relative error of the approximation of 2π by an $n$-gon is:
|
||||
$$
|
||||
\epsilon_n = \left| \frac{n\, s_n-2 \pi}{2\pi} \right|
|
||||
$$
|
||||
|
||||
|
||||
## Zwei Iterationsvorschriften^[nach: Christoph Überhuber, „Computer-Numerik“ Bd. 1, Springer 1995, Kap. 2.3]
|
||||
Die Gleichung
|
||||
## Two Iteration Formulas^[by: Christoph Überhuber, "Computer-Numerik" Vol. 1, Springer 1995, Chap. 2.3]
|
||||
The equation
|
||||
$$
|
||||
s_{2n} = \sqrt{2-\sqrt{4-s_n^2}}\qquad \qquad \textrm{(Iteration A)}
|
||||
s_{2n} = \sqrt{2-\sqrt{4-s_n^2}}\qquad \qquad \text{(Iteration A)}
|
||||
$$
|
||||
ist mathematisch äquivalent zu
|
||||
is mathematically equivalent to
|
||||
$$
|
||||
s_{2n} = \frac{s_n}{\sqrt{2+\sqrt{4-s_n^2}}} \qquad \qquad \textrm{(Iteration B)}
|
||||
s_{2n} = \frac{s_n}{\sqrt{2+\sqrt{4-s_n^2}}} \qquad \qquad \text{(Iteration B)}
|
||||
$$
|
||||
|
||||
(Bitte nachrechnen!)
|
||||
(Please verify!)
|
||||
|
||||
|
||||
|
||||
Allerdings ist Iteration A schlecht konditioniert und numerisch instabil, wie der folgende Code zeigt. Ausgegeben wird die jeweils berechnete Näherung für π.
|
||||
However, Iteration A is ill-conditioned and numerically unstable, as the following code demonstrates. The output shows the respective approximation for π.
|
||||
|
||||
```{julia}
|
||||
using Printf
|
||||
@@ -82,16 +86,16 @@ using Printf
|
||||
iterationA(s) = sqrt(2 - sqrt(4 - s^2))
|
||||
iterationB(s) = s / sqrt(2 + sqrt(4 - s^2))
|
||||
|
||||
s_B = s_A = sqrt(2) # Startwert
|
||||
s_B = s_A = sqrt(2) # initial value
|
||||
|
||||
ϵ(x) = abs(x - 2π)/2π # rel. Fehler
|
||||
ϵ(x) = abs(x - 2π)/2π # rel. error
|
||||
|
||||
ϵ_A = Float64[] # Vektoren für den Plot
|
||||
ϵ_A = Float64[] # vectors for the plot
|
||||
ϵ_B = Float64[]
|
||||
is = Float64[]
|
||||
|
||||
@printf """ approx. Wert von π
|
||||
n-Eck Iteration A Iteration B
|
||||
@printf """ approx. value of π
|
||||
n-gon Iteration A Iteration B
|
||||
"""
|
||||
|
||||
for i in 3:35
|
||||
@@ -106,13 +110,13 @@ for i in 3:35
|
||||
end
|
||||
```
|
||||
|
||||
Während Iteration B sich stabilisiert bei einem innerhalb der Maschinengenauigkeit korrekten Wert für π, wird Iteration A schnell instabil. Ein Plot der relativen Fehler $\epsilon_i$ bestätigt das:
|
||||
While Iteration B stabilizes at a value correct within machine precision, Iteration A quickly becomes unstable. A plot of the relative errors $\epsilon_i$ confirms this:
|
||||
|
||||
|
||||
```{julia}
|
||||
using PlotlyJS
|
||||
|
||||
layout = Layout(xaxis_title="Iterationsschritte", yaxis_title="rel. Fehler",
|
||||
layout = Layout(xaxis_title="Iteration steps", yaxis_title="rel. error",
|
||||
yaxis_type="log", yaxis_exponentformat="power",
|
||||
xaxis_tick0=2, xaxis_dtick=2)
|
||||
|
||||
@@ -120,19 +124,17 @@ plot([scatter(x=is, y=ϵ_A, mode="markers+lines", name="Iteration A", yscale=:lo
|
||||
scatter(x=is, y=ϵ_B, mode="markers+lines", name="Iteration B", yscale=:log10)],
|
||||
layout)
|
||||
```
|
||||
|
||||
## Stability and Cancellation
|
||||
|
||||
|
||||
|
||||
## Stabilität und Auslöschung
|
||||
|
||||
Bei $i=26$ erreicht Iteration B einen relativen Fehler in der Größe des Maschinenepsilons:
|
||||
At $i=26$, Iteration B reaches a relative error on the order of machine epsilon:
|
||||
|
||||
|
||||
```{julia}
|
||||
ϵ_B[22:28]
|
||||
```
|
||||
|
||||
Weitere Iterationen verbessern das Ergebnis nicht mehr. Sie stabilisieren sich bei einem relativen Fehler von etwa 2.5 Maschinenepsilon:
|
||||
Further iterations do not improve the result. It stabilizes at a relative error of approximately 2.5 machine epsilons:
|
||||
|
||||
|
||||
```{julia}
|
||||
@@ -140,18 +142,18 @@ Weitere Iterationen verbessern das Ergebnis nicht mehr. Sie stabilisieren sich b
|
||||
```
|
||||
|
||||
|
||||
Die Form Iteration A ist instabil. Bereits bei $i=16$ beginnt der relative Fehler wieder zu wachsen.
|
||||
Iteration A is unstable. Already at $i=16$ the relative error begins to grow again.
|
||||
|
||||
Ursache ist eine typische Auslöschung. Die Seitenlängen $s_n$ werden sehr schnell klein. Damit ist
|
||||
$a_n=\sqrt{4-s_n^2}$ nur noch wenig kleiner als 2 und bei der Berechnung von $s_{2n}=\sqrt{2-a_n}$ tritt ein typischer Auslöschungseffekt auf.
|
||||
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.
|
||||
|
||||
|
||||
```{julia}
|
||||
setprecision(80) # precision für BigFloat
|
||||
setprecision(80) # precision for BigFloat
|
||||
|
||||
s = sqrt(BigFloat(2))
|
||||
|
||||
@printf " a = √(4-s^2) als BigFloat und als Float64\n\n"
|
||||
@printf " a = √(4-s^2) as BigFloat and as Float64\n\n"
|
||||
|
||||
for i = 3:44
|
||||
s = iterationA(s)
|
||||
@@ -164,15 +166,13 @@ end
|
||||
|
||||
```
|
||||
|
||||
Man sieht die Abnahme der Zahl der signifikanten Ziffern. Man sieht auch, dass eine Verwendung von `BigFloat` mit einer Mantissenlänge von hier 80 Bit das Einsetzen des Auslöschungseffekts nur etwas hinaussschieben kann.
|
||||
One sees the decrease in the number of significant digits. One also sees that using `BigFloat` with a mantissa length of 80 bits here only slightly delays the onset of the cancellation effect.
|
||||
|
||||
**Gegen instabile Algorithmen helfen in der Regel nur bessere, stabile Algorithmen und nicht genauere Maschinenzahlen!**
|
||||
**Countermeasures against unstable algorithms usually require better, stable algorithms, not more precise machine numbers!**
|
||||
|
||||
:::{.content-hidden unless-format="xxx"}
|
||||
|
||||
Offensichtlich tritt bei der Berechnung von $2-a_n$ bereits relativ früh
|
||||
eine Abnahme der Anzahl der signifikanten Ziffern (Auslöschung) auf,
|
||||
bevor schließlich bei der Berechnung von $a_n=\sqrt{4-s_n^2}$
|
||||
selbst schon Auslöschung zu einem unbrauchbaren Ergebnis führt.
|
||||
Clearly, the computation of $2-a_n$ already shows a decrease in the number of significant digits (cancellation) relatively early,
|
||||
before the computation of $a_n=\sqrt{4-s_n^2}$ itself leads to an unusable result due to cancellation.
|
||||
|
||||
:::
|
||||
Reference in New Issue
Block a user