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

added IPython notebook for Example 1.1.1

parent a0765e66
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
### IPython notebook for Example 1.1.1 from the lecture
%% Cell type:code id: tags:
``` python
import numpy as np
from numpy.linalg import lstsq, matrix_rank, norm
from IPython.display import display, Math
from sympy import Matrix, latex
def my_latex(expr):
return latex(expr, mat_delim='(').replace('.0 &', '&').replace('.0\\', '\\')
delta_t = 0.15
n = 6
t = np.arange(1, n + 1) * delta_t
xi_hat = np.array([1.2, 0.6, 1.6, 0.9])
xi = xi_hat[:-1]
A_hat = np.stack((t, np.exp(t), t ** 3, np.sin(t)), axis=1)
A = A_hat[:, :-1]
display(Math(r'\hat A = h \begin{pmatrix} t_1 & e^{t_1} & t_1^3 & \sin(t_1) \\ t_2 & e^{t_2} & t_2^3 & \sin(t_2) \\ \vdots &\vdots & \vdots & \vdots \end{pmatrix},\quad ' + \
r'A = h \begin{pmatrix} t_1 & e^{t_1} & t_1^3 \\ t_2 & e^{t_2} & t_2^3 \\ \vdots &\vdots & \vdots \end{pmatrix},\quad ' + \
rf't = {my_latex(Matrix(t))}'))
```
%% Cell type:code id: tags:
``` python
display(Math(r"\operatorname{rank}(\hat A) = " + f"{matrix_rank(A_hat)}"))
display(Math(r"\operatorname{rank}(A) = " + f"{matrix_rank(A)}"))
```
%% Cell type:code id: tags:
``` python
display(Math(r"\hat y=\hat A\hat\xi,\quad y=A\xi, \quad " + rf"\hat\xi = {my_latex(Matrix(xi_hat))},\quad \xi = {my_latex(Matrix(xi))}"))
y_hat = np.matmul(A_hat, xi_hat)
y = np.matmul(A, xi)
delta_y1 = 1e-2 * np.array([1, 0, -1, -1, -0.5, 1])
delta_y2 = 1e-2 * np.array([-1, 1, 1, -0.5, -2, 1])
display(Math(rf"\delta y_1 = {my_latex(Matrix(delta_y1))},\quad \delta y_2 = {my_latex(Matrix(delta_y2))}"))
display(Math(f"\|\delta y_1\|_2 = {norm(delta_y1):.3f}" + r",\quad" \
f"\|\delta y_2\|_2 = {norm(delta_y2):.3f}"))
```
%% Cell type:code id: tags:
``` python
tilde_y1_hat = y_hat + delta_y1
tilde_y2_hat = y_hat + delta_y2
tilde_y1 = y + delta_y1
tilde_y2 = y + delta_y2
tilde_xi1 = lstsq(A, tilde_y1, rcond=None)[0]
tilde_xi2 = lstsq(A, tilde_y2, rcond=None)[0]
display(Math(r"\tilde \xi_i=\operatorname{arg\,min}\|A\xi-(y+\delta y_i)\|"))
display(Math(rf"\|\tilde \xi_1-\xi\|_2 = {norm(tilde_xi1 - xi):.3f}" + r",\quad" \
rf"\|\tilde \xi_2-\xi\|_2 = {norm(tilde_xi2 - xi):.3f}"))
tilde_xi1_hat = lstsq(A_hat, tilde_y1_hat, rcond=None)[0]
tilde_xi2_hat = lstsq(A_hat, tilde_y2_hat, rcond=None)[0]
display(Math(r"\tilde{\hat{\xi}}_i=\operatorname{arg\,min}\|\hat A\xi-(\hat y+\delta y_i)\|"))
display(Math(r"\|\tilde{\hat{\xi}}_1-\hat{\xi}\|_2 = " + f"{norm(tilde_xi1_hat - xi_hat):.3f}" + r",\quad" \
r"\|\tilde{\hat{\xi}}_2-\hat{\xi}\|_2 = " + f"{norm(tilde_xi2_hat - xi_hat):.1f}"))
```
%% Cell type:code id: tags:
``` python
display(Math(r"\frac{\|\tilde \xi_1-\xi\|_2}{\|\delta y_1\|_2} = " + f"{norm(tilde_xi1 - xi) / norm(delta_y1):.1f}" + r",\quad" \
r"\frac{\|\tilde{\hat{\xi}}_1-\hat{\xi}\|_2}{\|\delta y_1\|_2} = " + f"{norm(tilde_xi1_hat - xi_hat) / norm(delta_y1):.1f}"))
display(Math(r"\frac{\|\tilde \xi_2-\xi\|_2}{\|\delta y_2\|_2} = " + f"{norm(tilde_xi2 - xi) / norm(delta_y2):.1f}" + r",\quad" \
r"\frac{\|\tilde{\hat{\xi}}_2-\hat{\xi}\|_2}{\|\delta y_2\|_2} = " + f"{norm(tilde_xi2_hat - xi_hat) / norm(delta_y2):.1e}"))
```
%% 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