aboutsummaryrefslogtreecommitdiffstats
path: root/buch/papers/0f1/teil2.tex
blob: fdcb0fc6bf6228a3f73e100d6ed2ed197257f1f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
%
% teil2.tex -- Umsetzung in C Programmen
%
% (c) 2022 Fabian Dünki, Hochschule Rapperswil
%
\section{Umsetzung
\label{0f1:section:teil2}}
\rhead{Umsetzung}
Zur Umsetzung wurden drei verschiedene Ansätze gewählt, die in vollständiger Form auf Github \cite{0f1:code} zu finden sind. Dabei wurde der Schwerpunkt auf die Funktionalität und eine gute Lesbarkeit des Codes gelegt.
Die Unterprogramme wurde jeweils, wie die GNU Scientific Library, in C geschrieben. Die Zwischenresultate wurden vom Hauptprogramm in einem CSV-File gespeichert. Anschliessen wurde mit der Matplot-Library in Python die Resultate geplottet.

\subsection{Potenzreihe
\label{0f1:subsection:potenzreihe}}
Die naheliegendste Lösung ist die Programmierung der Potenzreihe

\begin{align}
    \label{0f1:umsetzung:0f1:eq}
    \mathstrut_0F_1(;c;z)
    &=
    \sum_{k=0}^\infty
    \frac{z^k}{(c)_k \cdot k!}
    &= 
    \frac{1}{c}
    +\frac{z^1}{(c+1) \cdot 1}
    + \cdots
    + \frac{z^{20}}{c(c+1)(c+2)\cdots(c+19) \cdot 2.4 \cdot 10^{18}}.
\end{align}

\lstinputlisting[style=C,float,caption={Potenzreihe.},label={0f1:listing:potenzreihe}, firstline=59]{papers/0f1/listings/potenzreihe.c}

\subsection{Kettenbruch
\label{0f1:subsection:kettenbruch}}
Eine weitere Variante zur Berechnung von $\mathstrut_0F_1(;c;z)$ ist die Umsetzung als Kettenbruch.
Der Vorteil einer Umsetzung als Kettenbruch gegenüber der Potenzreihe ist die schnellere Konvergenz.

\subsubsection{Grundlage}
Ein endlicher Kettenbruch \cite{0f1:wiki-kettenbruch} ist ein Bruch der Form
\begin{equation*}
a_0 + \cfrac{b_1}{a_1+\cfrac{b_2}{a_2+\cfrac{b_3}{a_3+\cdots}}},
\end{equation*}
in welchem $a_0, a_1,\dots,a_n$ und $b_1,b_2,\dots,b_n$ ganze Zahlen sind.

\subsubsection{Rekursionsbeziehungen und Kettenbrüche}
Wenn es eine Relation analytischer Funktion $f_i(z)$ hat, dann gibt es einen Kettenbruch für das Verhältnis $\frac{f_i(z)}{f_{i-1}(z)}$ \cite{0f1:wiki-fraction}. 
Nimmt man die Gleichung
\begin{equation*}
	f_{i-1} - f_i = k_i z f_{i+1},
\end{equation*}
wo $f_i$ analytische Funktionen sind und $i > 0$ ist, sowie $k_i$ konstant.
Ergibt sich der Zusammenhang
\begin{equation*}
	\cfrac{f_i}{f_{i-1}} = \cfrac{1}{1+k_iz\cfrac{f_{i+1}}{f_i}}.
\end{equation*}
Geht man einen Schritt weiter und nimmt für $g_i = \frac{f_i}{f_{i-1}}$ an, kommt man zur Formel
\begin{equation*}
	g_i = \cfrac{1}{1+k_izg_{i+1}}.
\end{equation*}
Setzt man dies nun für $g_1$ in den Bruch ein, ergibt sich
\begin{equation*}
	g_1 = \cfrac{f_1}{f_0} = \cfrac{1}{1+k_izg_2} = \cfrac{1}{1+\cfrac{k_1z}{1+k_2zg_3}} = \cdots
\end{equation*}
Repetiert man dies unendlich, erhält man einen Kettenbruch in der Form:
\begin{equation}
	\label{0f1:math:rekursion:eq}
	\cfrac{f_1}{f_0} = \cfrac{1}{1+\cfrac{k_1z}{1+\cfrac{k_2z}{1+\cfrac{k_3z}{\cdots}}}}.
\end{equation}

\subsubsection{Rekursion für $\mathstrut_0F_1$}
Angewendet auf die Potenzreihe
\begin{equation}
	\label{0f1:math:potenzreihe:0f1:eq}
	\mathstrut_0F_1(;c;z) = 1 + \frac{z}{c\cdot1!} + \frac{z^2}{c(c+1)\cdot2!} + \frac{z^3}{c(c+1)(c+2)\cdot3!} + \cdots
\end{equation}
kann durch Substitution bewiesen werden, dass
\begin{equation*}
	\mathstrut_0F_1(;c-1;z) - \mathstrut_0F_1(;c;z) = \frac{z}{c(c-1)} \cdot \mathstrut_0F_1(;c+1;z)
\end{equation*}
eine Relation dazu ist.
Wenn man für $f_i$ und $k_i$ die Annahme
\begin{align*}
	f_i =& \mathstrut_0F_1(;c+i;z)\\
	k_i	=& \frac{1}{(c+i)(c+i-1)}
\end{align*}
trifft und in die Formel \eqref{0f1:math:rekursion:eq} einsetzt, erhält man:
\begin{equation*}
	\cfrac{\mathstrut_0F_1(;c+1;z)}{\mathstrut_0F_1(;c;z)} = \cfrac{1}{1+\cfrac{\cfrac{z}{c(c+1)}}{1+\cfrac{\cfrac{z}{(c+1)(c+2)}}{1+\cfrac{\cfrac{z}{(c+2)(c+3)}}{\cdots}}}}.
\end{equation*}

\subsubsection{Algorithmus}
Da mit obigen Formeln nur ein Verhältnis zwischen $	\frac{\mathstrut_0F_1(;c+1;z)}{\mathstrut_0F_1(;c;z)}$ berechnet wurde, braucht es weitere Relationen um $\mathstrut_0F_1(;c;z)$ zu erhalten.
So ergeben ähnliche Relationen nach Wolfram Alpha \cite{0f1:wolfram-0f1} den Kettenbruch
\begin{equation}
	\label{0f1:math:kettenbruch:0f1:eq}
	\mathstrut_0F_1(;c;z) = 1 + \cfrac{\cfrac{z}{c}}{1+\cfrac{-\cfrac{z}{2(c+1)}}{1+\cfrac{z}{2(c+1)}+\cfrac{-\cfrac{z}{3(c+2)}}{1+\cfrac{z}{5(c+4)} + \cdots}}},
\end{equation}
der als Code (Listing \ref{0f1:listing:kettenbruchIterativ})  umgesetzt wurde. 


\lstinputlisting[style=C,float,caption={Iterativ umgesetzter Kettenbruch.},label={0f1:listing:kettenbruchIterativ},  firstline=8]{papers/0f1/listings/kettenbruchIterativ.c}

\subsection{Rekursionsformel
\label{0f1:subsection:rekursionsformel}}
Wesentlich stabiler zur Berechnung eines Kettenbruches ist die Rekursionsformel. Nachfolgend wird die verkürzte Herleitung vom Kettenbruch zur Rekursionsformel aufgezeigt. Eine vollständige Schritt für Schritt Herleitung ist im Seminarbuch Numerik, im Kapitel Kettenbrüche \cite{0f1:kettenbrueche} zu finden.

\subsubsection{Herleitung}
Ein Näherungsbruch in der Form
\begin{align*}
	\cfrac{A_k}{B_k} = a_k + \cfrac{b_{k + 1}}{a_{k + 1} + \cfrac{p}{q}}
\end{align*}
lässt sich zu
\begin{align*}
	\cfrac{A_k}{B_k} = \cfrac{b_{k+1}}{a_{k+1} + \cfrac{p}{q}} = \frac{b_{k+1} \cdot q}{a_{k+1} \cdot q + p}
\end{align*}
umformen.
Dies lässt sich auch durch die Matrizenschreibweise
\begin{equation*}
	\begin{pmatrix}
		A_k\\
		B_k
	\end{pmatrix}
	= 		\begin{pmatrix}
		b_{k+1} \cdot q\\
		a_{k+1} \cdot q + p
	\end{pmatrix}
	=\begin{pmatrix}
		0&	b_{k+1}\\
		1&	a_{k+1}
	\end{pmatrix}
	\begin{pmatrix}
		p \\
		q
	\end{pmatrix}.
	%\label{0f1:math:rekursionsformel:herleitung}
\end{equation*}
ausdrücken.
Wendet man dies nun auf den Kettenbruch in der Form
\begin{equation*}
	\frac{A_k}{B_k} = a_0 + \cfrac{b_1}{a_1+\cfrac{b_2}{a_2+\cfrac{\cdots}{\cdots+\cfrac{b_{k-1}}{a_{k-1} + \cfrac{b_k}{a_k}}}}}
\end{equation*}
an, ergibt sich die Matrixdarstellungen:

\begin{align*}
	\begin{pmatrix}
		A_k\\
		B_k
	\end{pmatrix}
	&=
	\begin{pmatrix}
		1& a_0\\
		0& 1
	\end{pmatrix}
	\begin{pmatrix}
		0& b_1\\
		1& a_1
	\end{pmatrix}
	\cdots
	\begin{pmatrix}
		0& b_{k-1}\\
		1& a_{k-1}
	\end{pmatrix}
	\begin{pmatrix}
		b_k\\
		a_k
	\end{pmatrix}.
\end{align*}
Nach vollständiger Induktion ergibt sich für den Schritt $k$, die Matrix
\begin{equation}
	\label{0f1:math:matrix:ende:eq}
	 \begin{pmatrix}
		A_{k}\\
		B_{k}			
	\end{pmatrix} 
	=
		\begin{pmatrix}
		A_{k-2}& A_{k-1}\\
		B_{k-2}& B_{k-1}			
	\end{pmatrix}
		\begin{pmatrix}
		b_k\\
		a_k
	\end{pmatrix}.
\end{equation}
Und schlussendlich kann der Näherungsbruch
\[
\frac{A_k}{B_k}
\] 
berechnet werden.


\subsubsection{Algorithmus}
Die Berechnung von $A_k, B_k$ gemäss \eqref{0f1:math:matrix:ende:eq} kann man auch ohne die Matrizenschreibweise \cite{0f1:kettenbrueche} aufschreiben:
\begin{itemize}
\item Startbedingungen:
\begin{align*}
A_{-1} &= 0		&		A_0 &= a_0 \\
B_{-1} &= 1		&		B_0 &= 1 
\end{align*}
\item Schritt $k\to k+1$:
\[
\begin{aligned}
\label{0f1:math:loesung:eq}
k &\rightarrow k + 1:
&
A_{k+1} &= A_{k-1} \cdot b_k + A_k \cdot a_k \\
&&
B_{k+1} &= B_{k-1} \cdot b_k + B_k \cdot a_k
\end{aligned}
\]
\item
Näherungsbruch: \qquad$\displaystyle\frac{A_k}{B_k}$.
\end{itemize}

Ein grosser Vorteil dieser Umsetzung als Rekursionsformel \eqref{0f1:listing:kettenbruchRekursion} ist, dass im Vergleich zum Code (Listing \ref{0f1:listing:kettenbruchIterativ}) eine Division gespart werden kann und somit weniger Rundungsfehler entstehen können.

%Code
\lstinputlisting[style=C,float,caption={Rekursionsformel für Kettenbruch.},label={0f1:listing:kettenbruchRekursion},  firstline=8]{papers/0f1/listings/kettenbruchRekursion.c}