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

Upload New File

parent 68eae05c
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
### IPython notebook for Example 3.5.7 from the lecture
%% Cell type:code id: tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import set_matplotlib_formats, display, Math
def k(s, t, d):
return d / np.power(d**2 + (s-t)**2, 3/2)
d = 0.1
n = 80
h = 1/n
display(Math(r'\text{Create }A\text{ from Example 1.7.1, }\xi_j=\begin{cases}2&1\leq j\leq40\\1&41\leq j\leq80\end{cases},\ y=A\xi'))
A = np.zeros((n, n))
for i in range(n):
for j in range(n):
A[i, j] = h * k((i+0.5)*h, (j+0.5)*h, d)
xi = np.ones(n)
xi[:n//2] = 2
y = np.matmul(A, xi)
display(Math(r'\tilde y= y+\delta y'))
np.random.seed(0)
y_tilde = y + 0.02*(np.random.rand(n)-0.5)
```
%% Cell type:code id: tags:
``` python
discrepancy_tilde = np.zeros(100)
xi_tilde_norm = np.zeros(100)
discrepancy = np.zeros(100)
xi_norm = np.zeros(100)
for i in range(1, 101):
alpha = 0.005*i
A_Tikh = np.concatenate((A, alpha*np.eye(n)), axis=0)
y_tilde_Tikh = np.concatenate((y_tilde, np.zeros(n)), axis=0)
xi_tilde_alpha = np.linalg.lstsq(A_Tikh, y_tilde_Tikh, rcond=0)[0]
discrepancy_tilde[i-1] = np.linalg.norm(np.matmul(A, xi_tilde_alpha)-y_tilde)
xi_tilde_norm[i-1] = np.linalg.norm(xi_tilde_alpha)
y_Tikh = np.concatenate((y, np.zeros(n)), axis=0)
xi_alpha = np.linalg.lstsq(A_Tikh, y_Tikh, rcond=0)[0]
discrepancy[i-1] = np.linalg.norm(np.matmul(A, xi_alpha)-y)
xi_norm[i-1] = np.linalg.norm(xi_alpha)
set_matplotlib_formats('svg')
plt.title("Tikhonov L-curve")
plt.xlabel(r'$||A\tilde \xi_{\alpha}- \tilde y||_2^2$')
plt.ylabel(r'$||\xi_{\alpha}||_2^2$')
plt.plot(discrepancy_tilde**2, xi_tilde_norm**2, 'k+')
plt.show()
plt.close()
```
%% Cell type:code id: tags:
``` python
plt.title("Tikhonov L-curves, zoom")
plt.plot(discrepancy_tilde[:50]**2, xi_tilde_norm[:50]**2, 'k+', label=r'$(\|A \tilde \xi_\alpha - \tilde y\|_2^2, \|\tilde \xi_\alpha\|_2^2)$')
plt.plot(discrepancy[:50]**2, xi_norm[:50]**2, 'k.', label=r'$(\|A \xi_\alpha - y\|_2^2, \| \xi_\alpha\|_2^2)$')
plt.legend()
plt.show()
plt.close()
```
%% Cell type:code id: tags:
``` python
plt.title("Tikhonov L-curves, zoom")
plt.plot(discrepancy_tilde[:50], xi_tilde_norm[:50], 'k+', label=r'$(\|A \tilde \xi_\alpha - \tilde y\|_2, \|\tilde \xi_\alpha\|_2)$')
plt.plot(discrepancy[:50], xi_norm[:50], 'k.', label=r'$(\|A \xi_\alpha - y\|_2, \| \xi_\alpha\|_2)$')
plt.legend()
plt.show()
plt.close()
```
%% Cell type:code id: tags:
``` python
def CGNE(A, y_tilde):
xi_tilde = np.zeros(n)
# xi_tilde is zero, so the initial residuum is the input data.
r = y_tilde.copy()
d = np.matmul(A.T, r)
p = d.copy()
p_norm = np.linalg.norm(p)
discrepancy_tilde = np.zeros(201)
discrepancy_tilde[0] = np.linalg.norm(np.matmul(A, xi_tilde)-y_tilde)
xi_tilde_norm = np.zeros(201)
for k in range(1, 201):
q = np.matmul(A, d)
beta = (np.linalg.norm(p)/np.linalg.norm(q))**2
xi_tilde += beta * d
r += -beta*q
p = np.matmul(A.T, r)
p_norm_new = np.linalg.norm(p)
gamma = (p_norm_new/p_norm)**2
p_norm = p_norm_new
d = p + gamma*d
discrepancy_tilde[k] = np.linalg.norm(np.matmul(A, xi_tilde)-y_tilde)
xi_tilde_norm[k] = np.linalg.norm(xi_tilde)
return discrepancy_tilde, xi_tilde_norm
discrepancy_tilde, xi_tilde_norm = CGNE(A, y_tilde)
plt.title("CGNE L-curve")
plt.xlabel(r'$||A\tilde \xi_{k}- \tilde y||_2$')
plt.ylabel(r'$||\xi_{k}||_2$')
plt.plot(discrepancy_tilde[2:], xi_tilde_norm[2:], 'k+')
plt.show()
plt.close()
```
%% Cell type:code id: tags:
``` python
plt.title("CGNE L-curve log-log scale")
plt.xlabel(r'$||A\tilde \xi_{k}- \tilde y||_2$')
plt.ylabel(r'$||\xi_{k}||_2$')
plt.loglog(discrepancy_tilde[2:], xi_tilde_norm[2:], 'k+')
plt.show()
plt.close()
```
%% Cell type:code id: tags:
``` python
discrepancy, xi_norm = CGNE(A, y)
plt.title("CGNE L-curve log-log scale, zoom")
plt.loglog(discrepancy_tilde[10:80], xi_tilde_norm[10:80], 'k+', label=r'$(\|A \tilde \xi_\alpha - \tilde y\|_2, \|\tilde \xi_\alpha\|_2)$')
plt.loglog(discrepancy[10:80], xi_norm[10:80], 'k.', label=r'$(\|A \xi_\alpha - y\|_2, \| \xi_\alpha\|_2)$')
plt.legend()
plt.show()
plt.close()
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment