Skip to content
Snippets Groups Projects
Commit 90ae0ca7 authored by Benjamin Berkels's avatar Benjamin Berkels
Browse files

improved the IPython notebook for Example 2.2.4

parent 4e9d6f32
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### IPython notebook for Example 2.2.4 from the lecture ### IPython notebook for Example 2.2.4 from the lecture
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
%matplotlib inline %matplotlib inline
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from IPython.display import set_matplotlib_formats, display, Math from matplotlib_inline.backend_inline import set_matplotlib_formats
set_matplotlib_formats('svg') from IPython.display import display, Math
n = 40 set_matplotlib_formats("svg")
h = 1/n
display(Math(r'''A = h \begin{pmatrix} 1 & & & \\ 1 & 1 & &0 \\ \vdots &\vdots & \ddots & \\ 1 & 1& \dots & 1 \end{pmatrix}''')) n = 40
A = h*np.tril(np.ones((n, n), dtype=int), 0) h = 1 / n
display(Math(r'''y= \frac{1}{n}(1,1,\ldots,1)^T''')) display(Math(r"\text{Setting from Section 1.2:}"))
y = np.ones(n)/n display(Math(r"A = h \begin{pmatrix} 1 & & & \\ 1 & 1 & &0 \\ \vdots &\vdots & \ddots & \\ 1 & 1& \dots & 1 \end{pmatrix}\text{ (discrete integration matrix)}"))
display(Math(r'''\delta y = \frac{1}{n}(1,-1,1,-1, \ldots)^T''')) A = h * np.tril(np.ones((n, n), dtype=int), 0)
display(Math(r"y= \frac{1}{n}(1,1,\ldots,1)^T\text{ (low frequency input)}"))
y = np.ones(n) / n
display(Math(r"\delta y = \frac{1}{n}(1,-1,1,-1, \ldots)^T\text{ (high frequency pertubation)}"))
delta_y = y.copy() delta_y = y.copy()
delta_y[1::2] = -1/n delta_y[1::2] = -1 / n
display(Math(r'''\Rightarrow \|y\|_2= \|\delta y\|_2= \frac{1}{\sqrt{n}}''')) display(Math(r"\Rightarrow \|y\|_2= \|\delta y\|_2= \frac{1}{\sqrt{n}}"))
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
display(Math(r'U^TAV=\Sigma')) display(Math(r"U^TAV=\Sigma"))
U, sigma, VT = np.linalg.svd(A) U, sigma, VT = np.linalg.svd(A)
display(Math(r'\hat y=U^Ty')) display(Math(r"\hat y=U^Ty"))
y_hat = np.matmul(U.T, y) y_hat = np.matmul(U.T, y)
display(Math(r'\hat{\delta y}:= U^T \delta y')) display(Math(r"\hat{\delta y}:= U^T \delta y"))
delta_y_hat = np.matmul(U.T, delta_y) delta_y_hat = np.matmul(U.T, delta_y)
sigma_inv = 1/sigma sigma_inv = 1 / sigma
j = np.arange(1, 41) j = np.arange(1, 41)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
plt.yscale('log') plt.yscale("log")
plt.plot(j, np.abs(y_hat), '.', label=r'$|(U^Ty)_j|$') plt.plot(j, np.abs(y_hat), ".", label=r"$|(U^Ty)_j|$")
plt.plot(j, np.abs(delta_y_hat), '+', label=r'$|(U^T \delta y)_j|$') plt.plot(j, np.abs(delta_y_hat), "+", label=r"$|(U^T \delta y)_j|$")
plt.legend() plt.legend()
plt.show() plt.show()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
plt.close() plt.close()
plt.yscale('log') plt.yscale("log")
plt.plot(j, sigma_inv, '*', label=r'$\sigma_j^{-1}$') plt.plot(j, sigma_inv, "*", label=r"$\sigma_j^{-1}$")
plt.legend() plt.legend()
plt.show() plt.show()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
plt.close() plt.close()
plt.yscale('log') plt.yscale("log")
plt.plot(j, sigma_inv*np.abs(y_hat), '.', label=r'$\sigma_j^{-1}|(U^Ty)_j|$') plt.plot(j, sigma_inv * np.abs(y_hat), ".", label=r"$\sigma_j^{-1}|(U^Ty)_j|$")
plt.plot(j, sigma_inv*np.abs(delta_y_hat), '+', label=r'$\sigma_j^{-1}|(U^T \delta y)_j|$') plt.plot(j, sigma_inv * np.abs(delta_y_hat), "+", label=r"$\sigma_j^{-1}|(U^T \delta y)_j|$")
plt.legend() plt.legend()
plt.show() plt.show()
display(Math(r'''\Rightarrow\ \|A^{-1} y\|_2 = \|\Sigma^{-1} \hat{y}\|_2 \ll \|A^{-1} \delta y\|_2= \|\Sigma^{-1} \hat{\delta y}\|_2\ \Rightarrow\ Q(y,\delta y) \gg 1''')) display(Math(r"\Rightarrow\ \|A^{-1} y\|_2 = \|\Sigma^{-1} \hat{y}\|_2 \ll \|A^{-1} \delta y\|_2= \|\Sigma^{-1} \hat{\delta y}\|_2\ \Rightarrow\ Q(y,\delta y) \gg 1"))
```
%% Cell type:code id: tags:
``` python
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment