Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MBEM-2021
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Benjamin Berkels
MBEM-2021
Commits
c0dfbe58
Commit
c0dfbe58
authored
4 years ago
by
Benjamin Berkels
Browse files
Options
Downloads
Patches
Plain Diff
added IPython notebook for Example 1.1.1
parent
a0765e66
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
exp-111.ipynb
+135
-0
135 additions, 0 deletions
exp-111.ipynb
with
135 additions
and
0 deletions
exp-111.ipynb
0 → 100644
+
135
−
0
View file @
c0dfbe58
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### IPython notebook for Example 1.1.1 from the lecture"
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"import numpy as np\n",
"from numpy.linalg import lstsq, matrix_rank, norm\n",
"from IPython.display import display, Math\n",
"from sympy import Matrix, latex\n",
"\n",
"def my_latex(expr):\n",
" return latex(expr, mat_delim='(').replace('.0 &', '&').replace('.0\\\\', '\\\\')\n",
"\n",
"delta_t = 0.15\n",
"n = 6\n",
"t = np.arange(1, n + 1) * delta_t\n",
"xi_hat = np.array([1.2, 0.6, 1.6, 0.9])\n",
"xi = xi_hat[:-1]\n",
"\n",
"A_hat = np.stack((t, np.exp(t), t ** 3, np.sin(t)), axis=1)\n",
"A = A_hat[:, :-1]\n",
"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 ' + \\\n",
" 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 ' + \\\n",
" rf't = {my_latex(Matrix(t))}'))"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"metadata": {},
"source": [
"display(Math(r\"\\operatorname{rank}(\\hat A) = \" + f\"{matrix_rank(A_hat)}\"))\n",
"display(Math(r\"\\operatorname{rank}(A) = \" + f\"{matrix_rank(A)}\"))"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"metadata": {},
"source": [
"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))}\"))\n",
"y_hat = np.matmul(A_hat, xi_hat)\n",
"y = np.matmul(A, xi)\n",
"\n",
"delta_y1 = 1e-2 * np.array([1, 0, -1, -1, -0.5, 1])\n",
"delta_y2 = 1e-2 * np.array([-1, 1, 1, -0.5, -2, 1])\n",
"\n",
"display(Math(rf\"\\delta y_1 = {my_latex(Matrix(delta_y1))},\\quad \\delta y_2 = {my_latex(Matrix(delta_y2))}\"))\n",
"display(Math(f\"\\|\\delta y_1\\|_2 = {norm(delta_y1):.3f}\" + r\",\\quad\" \\\n",
" f\"\\|\\delta y_2\\|_2 = {norm(delta_y2):.3f}\"))"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"metadata": {},
"source": [
"tilde_y1_hat = y_hat + delta_y1\n",
"tilde_y2_hat = y_hat + delta_y2\n",
"tilde_y1 = y + delta_y1\n",
"tilde_y2 = y + delta_y2\n",
"\n",
"tilde_xi1 = lstsq(A, tilde_y1, rcond=None)[0]\n",
"tilde_xi2 = lstsq(A, tilde_y2, rcond=None)[0]\n",
"\n",
"display(Math(r\"\\tilde \\xi_i=\\operatorname{arg\\,min}\\|A\\xi-(y+\\delta y_i)\\|\"))\n",
"\n",
"display(Math(rf\"\\|\\tilde \\xi_1-\\xi\\|_2 = {norm(tilde_xi1 - xi):.3f}\" + r\",\\quad\" \\\n",
" rf\"\\|\\tilde \\xi_2-\\xi\\|_2 = {norm(tilde_xi2 - xi):.3f}\"))\n",
"\n",
"tilde_xi1_hat = lstsq(A_hat, tilde_y1_hat, rcond=None)[0]\n",
"tilde_xi2_hat = lstsq(A_hat, tilde_y2_hat, rcond=None)[0]\n",
"\n",
"display(Math(r\"\\tilde{\\hat{\\xi}}_i=\\operatorname{arg\\,min}\\|\\hat A\\xi-(\\hat y+\\delta y_i)\\|\"))\n",
"\n",
"display(Math(r\"\\|\\tilde{\\hat{\\xi}}_1-\\hat{\\xi}\\|_2 = \" + f\"{norm(tilde_xi1_hat - xi_hat):.3f}\" + r\",\\quad\" \\\n",
" r\"\\|\\tilde{\\hat{\\xi}}_2-\\hat{\\xi}\\|_2 = \" + f\"{norm(tilde_xi2_hat - xi_hat):.1f}\"))"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"metadata": {},
"source": [
"display(Math(r\"\\frac{\\|\\tilde \\xi_1-\\xi\\|_2}{\\|\\delta y_1\\|_2} = \" + f\"{norm(tilde_xi1 - xi) / norm(delta_y1):.1f}\" + r\",\\quad\" \\\n",
" r\"\\frac{\\|\\tilde{\\hat{\\xi}}_1-\\hat{\\xi}\\|_2}{\\|\\delta y_1\\|_2} = \" + f\"{norm(tilde_xi1_hat - xi_hat) / norm(delta_y1):.1f}\"))\n",
"display(Math(r\"\\frac{\\|\\tilde \\xi_2-\\xi\\|_2}{\\|\\delta y_2\\|_2} = \" + f\"{norm(tilde_xi2 - xi) / norm(delta_y2):.1f}\" + r\",\\quad\" \\\n",
" r\"\\frac{\\|\\tilde{\\hat{\\xi}}_2-\\hat{\\xi}\\|_2}{\\|\\delta y_2\\|_2} = \" + f\"{norm(tilde_xi2_hat - xi_hat) / norm(delta_y2):.1e}\"))"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"metadata": {},
"source": [],
"outputs": [],
"execution_count": null
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
\ No newline at end of file
%% 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
)
:
.
3
f
}
"
+
r
"
,\quad
"
\
f
"
\|\delta y_2\|_2 =
{
norm
(
delta_y2
)
:
.
3
f
}
"
))
```
%% 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
)
:
.
3
f
}
"
+
r
"
,\quad
"
\
rf
"
\|\tilde \xi_2-\xi\|_2 =
{
norm
(
tilde_xi2
-
xi
)
:
.
3
f
}
"
))
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
)
:
.
3
f
}
"
+
r
"
,\quad
"
\
r
"
\|\tilde{\hat{\xi}}_2-\hat{\xi}\|_2 =
"
+
f
"
{
norm
(
tilde_xi2_hat
-
xi_hat
)
:
.
1
f
}
"
))
```
%% 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
)
:
.
1
f
}
"
+
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
)
:
.
1
f
}
"
))
display
(
Math
(
r
"
\frac{\|\tilde \xi_2-\xi\|_2}{\|\delta y_2\|_2} =
"
+
f
"
{
norm
(
tilde_xi2
-
xi
)
/
norm
(
delta_y2
)
:
.
1
f
}
"
+
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
)
:
.
1
e
}
"
))
```
%% Cell type:code id: tags:
```
python
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment