[NumCS] ++ code examples

This commit is contained in:
RobinB27
2025-10-04 21:28:16 +02:00
parent ebe4181caf
commit 8bd1ad8876
2 changed files with 30 additions and 10 deletions

Binary file not shown.

View File

@@ -95,7 +95,7 @@ Falls $x_j = x_0 + \underbrace{j \cdot h}_{:= \Delta^j}$ gilt vereinfacht sich e
\begin{code}{python} \begin{code}{python}
# Slow matrix approach # Slow matrix approach
def divdiff(x,y): def divdiff_slow(x,y):
n = y.size n = y.size
T = np.zeros((n,n)) T = np.zeros((n,n))
T[:,0] = y T[:,0] = y
@@ -104,6 +104,8 @@ Falls $x_j = x_0 + \underbrace{j \cdot h}_{:= \Delta^j}$ gilt vereinfacht sich e
for i in range(n-l): for i in range(n-l):
T[i, l] = (T[i+1,l-1] - T[i, l-1]) T[i, l] = (T[i+1,l-1] - T[i, l-1])
T[i, l] /= (x[i+l] - x[i]) T[i, l] /= (x[i+l] - x[i])
return T[0,:]
\end{code} \end{code}
\newcolumn \newcolumn
@@ -113,16 +115,15 @@ Falls $x_j = x_0 + \underbrace{j \cdot h}_{:= \Delta^j}$ gilt vereinfacht sich e
Vektorisierter Ansatz in $\mathcal{O}(n^2)$, Speicher $\mathcal{O}(n)$ Vektorisierter Ansatz in $\mathcal{O}(n^2)$, Speicher $\mathcal{O}(n)$
\begin{code}{python} \begin{code}{python}
# NOT THE CORRECT CODE YET # Fast vectorized approach
def divdiff(x,y): def divdiff_fast(x,y):
n = y.size n = y.shape[0]
T = np.zeros((n,n))
T[:,0] = y
for l in range(1,n): for k in range(1, n):
for i in range(n-l): y[k:] = (y[k:] - y[(k-1):n-1])
T[i, l] = (T[i+1,l-1]-T[i, l-1]) y[k:] /= (x[k:] - x[0:n-k])
T[i, l] /= (x[i+l] - x[i])
return y
\end{code} \end{code}
\end{multicols} \end{multicols}
@@ -130,6 +131,9 @@ Falls $x_j = x_0 + \underbrace{j \cdot h}_{:= \Delta^j}$ gilt vereinfacht sich e
\subsubsection{Auswertung} \subsubsection{Auswertung}
Auswertung eines Newton-Polynoms funktioniert in $\mathcal{O}(n)$ durch ein modifiziertes Horner-Schema: Auswertung eines Newton-Polynoms funktioniert in $\mathcal{O}(n)$ durch ein modifiziertes Horner-Schema:
\begin{multicols}{2}
\begin{align*} \begin{align*}
p_0 &:= \beta_n \\ p_0 &:= \beta_n \\
p_1 &:= (x - x_{n-1})p_0 + \beta_{n-1} \\ p_1 &:= (x - x_{n-1})p_0 + \beta_{n-1} \\
@@ -138,6 +142,22 @@ Auswertung eines Newton-Polynoms funktioniert in $\mathcal{O}(n)$ durch ein modi
p_n &= p(x) p_n &= p(x)
\end{align*} \end{align*}
\newcolumn
\begin{code}{python}
def evalNewton(x_data, beta, x):
p = np.zeros(x.shape[0])
p += beta[beta.shape[0]-1]
for i in range(1, n+1):
p = (x - x_data[n-i])*p + beta[n-i]
return p
\end{code}
\end{multicols}
\subsubsection{Fehler} \subsubsection{Fehler}
\setcounter{all}{11} \setcounter{all}{11}