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

Upload New File

parent 78221d61
Branches
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
### IPython notebook for Example 3.5.5 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)
```
%% Cell type:code id: tags:
``` python
display(Math(r'\tilde y= y+\delta y'))
np.random.seed(0)
y_tilde = y + 0.02*(np.random.rand(n)-0.5)
epsilon_a = np.linalg.norm(y_tilde-y)
print('epsilon_a=||y_tilde-y|| = {:.3f}'.format(epsilon_a))
```
%% Cell type:code id: tags:
``` python
discrepancy = np.zeros(100)
alphas = np.zeros(100)
xi_alphas = np.zeros((100, n))
for i in range(1, 101):
alpha = 0.005*i
A_Tikh = np.concatenate((A, np.diag(alpha*np.ones(n))), axis=0)
y_tilde_Tikh = np.concatenate((y_tilde, np.zeros(n)), axis=0)
xi_alpha = np.linalg.lstsq(A_Tikh, y_tilde_Tikh, rcond=0)[0]
xi_alphas[i-1, :] = xi_alpha
alphas[i-1] = alpha
discrepancy[i-1] = np.linalg.norm(np.matmul(A, xi_alpha)-y_tilde)
set_matplotlib_formats('svg')
plt.title("Tikhonov, discrepancy")
plt.xlabel(r'$\alpha$')
plt.ylabel(r'$||A\tilde \xi_{\alpha}- \tilde y||_2$')
plt.plot(alphas, discrepancy, 'k+')
plt.hlines(epsilon_a, xmin=alphas[0], xmax=alphas[-1])
plt.show()
plt.close()
```
%% Cell type:code id: tags:
``` python
alpha_index = np.argmin(np.abs(discrepancy-epsilon_a))
print('alpha_d = {}'.format(alphas[alpha_index]))
j = np.arange(1, n+1)
plt.title("Tikhonov, alpha_d = {}".format(alphas[alpha_index]))
plt.plot(j, xi_alphas[alpha_index, :], 'kx')
plt.show()
plt.close()
```
%% Cell type:code id: tags:
``` python
# CGNE
xi_tilde = np.zeros(n)
xi_ks = np.zeros((101, 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 = np.zeros(101)
discrepancy[0] = np.linalg.norm(np.matmul(A, xi_tilde)-y_tilde)
for k in range(1, 101):
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[k] = np.linalg.norm(np.matmul(A, xi_tilde)-y_tilde)
xi_ks[k, :] = xi_tilde
plt.title("CGNE, discrepancy")
plt.xlabel(r'$k$')
plt.ylabel(r'$||A\tilde \xi_k- \xi||_2$')
plt.yscale('log')
plt.plot(discrepancy, 'k+')
plt.hlines(epsilon_a, xmin=0, xmax=discrepancy.shape[0])
plt.show()
plt.close()
```
%% Cell type:code id: tags:
``` python
k_opt = np.argmin(np.abs(discrepancy-epsilon_a))
plt.title("CGNE, k_opt = {}".format(k_opt))
plt.plot(j, xi_ks[k_opt, :], 'kx')
plt.show()
plt.close()
```
%% 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