You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/lecture_11/sparse.md
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -21,10 +21,10 @@ Often, a regularization term is added. There are two possibilities. The [ridge r
21
21
Both approaches try to keep the norm of parameters ``w`` small to prevent overfitting. The first approach results in a simpler numerical method, while the second one induces sparsity. Before we start with both topics, we will briefly mention matrix decompositions which plays a crucial part in numerical computations.
22
22
23
23
24
-
## Theory of matrix eigendecomposition
24
+
## [Theory of matrix eigendecomposition](@id matrix-eigen)
25
25
26
26
27
-
Consider a square matrix ``A\in \mathbb R^{n\times n}`` with real-valued entries. We there exist ``\lambda\in\mathbb R`` and ``v\in\mathbb^n`` such that
27
+
Consider a square matrix ``A\in \mathbb R^{n\times n}`` with real-valued entries. We there exist ``\lambda\in\mathbb R`` and ``v\in\mathbb R^n`` such that
28
28
29
29
```math
30
30
Av = \lambda v,
@@ -85,7 +85,7 @@ Since this formula uses only matrix-vector multiplication and an inversion of a
85
85
86
86
87
87
88
-
## Theory of LASSO
88
+
## [Theory of LASSO](@id lasso)
89
89
90
90
91
91
Unlike ridge regression, LASSO does not have a closed-form solution. Since it is a structured convex problem, it can be solved the [ADMM algorithm](https://web.stanford.edu/~boyd/papers/pdf/admm_distr_stats.pdf). It is a primal-dual algorithm, which employs the primal original variable ``w``, the primal auxiliary variable ``z`` and the dual variable ``u`` with the iterative updates:
To solve differential equations, we will use package [DifferentialEquations](https://diffeq.sciml.ai/stable/).
3
+
To solve differential equations, we use the package [DifferentialEquations](https://diffeq.sciml.ai/stable/), which consider ODEs in the form
4
4
5
-
## Introduction
6
-
7
-
DifferentialEquations consider ODEs in the form
8
5
```math
9
6
\dot u(t) = f(t, u(t), p(t))
10
7
```
11
-
with the initial condition ``u(t_0)= u_0``. While ``u`` is the solution, ``p`` described external parameters.
12
8
13
-
We will start with a simple problem
9
+
with the initial condition ``u(t_0)= u_0``. While ``u`` is the solution, ``p`` describes external parameters.
10
+
11
+
12
+
13
+
14
+
## Introduction
15
+
16
+
We start with the following simple problem:
17
+
14
18
```math
15
-
\dot u(t) = 0.98u
19
+
\begin{aligned}
20
+
\dot u(t) &= 0.98u, \\
21
+
u(0) &= 1.
22
+
\end{aligned}
16
23
```
17
-
with initial condition ``u(0) = 1``. This has closed-form solution ``u(t) = e^{0.98t}``. To solve this ODE by ```DifferentialEquations```, we first need to create the problem ```prob``` by supplying function ``f``, the initial point ``u_0`` and the time interval ``[t_0,t_1]`` to the constructor ```ODEProblem```
24
+
25
+
This equation has the closed-form solution ``u(t) = e^{0.98t}``. To solve it by `DifferentialEquations`, we first need to create the problem `prob` by supplying the function ``f``, the initial point ``u_0`` and the time interval ``[t_0,t_1]`` to the constructor `ODEProblem`.
Then we use the `solve` function to solve the equation.
41
+
31
42
```@example intro
32
43
sol = solve(prob)
33
44
```
34
-
The first line specifies that the solution was successful. We can automatically check whether the solution was successful by ```sol.retcode == :Success```. The second line specifies the interpolation method. Even though the solution was evaluated at only 5 points ```sol.t``` with values ```sol.u```, the interpolation
45
+
46
+
The first line says that the solution was successful, which can be automatically checked by accessing the field `sol.retcode`. The second line specifies the interpolation method, and the following lines the solution. Even though the solution was evaluated at only five points, the interpolation allows plotting a smooth function.
35
47
36
48
```@example intro
37
49
using Plots
38
50
39
-
plot(sol)
51
+
plot(sol; label="")
40
52
41
53
savefig("intro.svg") # hide
42
54
```
43
55
44
56

45
57
46
-
The ```sol``` structure is heavily overloaded. It can be used to evaluate the solution ``u`` at any time
58
+
The `sol` structure can be used to evaluate the solution ``u``.
59
+
47
60
```@example intro
48
61
sol(0.8)
49
62
```
50
63
51
-
The next exercise shows how to specify the interpolation technique and compares the resutlts.
64
+
65
+
66
+
67
+
68
+
69
+
70
+
The following exercise shows how to specify the interpolation technique and compares the results.
52
71
53
72
```@raw html
54
73
<div class = "exercise-body">
55
74
<header class = "exercise-header">Exercise:</header><p>
56
75
```
57
-
When calling the ```solve``` function, we can specify the interpolation way. Solve the ODE with linear interpolation (```dense=false```) and the Runge-Kutta method of fourth order (```RK4()```). Plot the results and compare them with the default and original solutions.
76
+
77
+
When calling the `solve` function, we can specify the interpolation way. Solve the ODE with linear interpolation (`dense=false`) and the Runge-Kutta method of the fourth order (`RK4()`). Plot the results and compare them with the default and original solutions.
78
+
58
79
```@raw html
59
80
</p></div>
60
81
<details class = "solution-body">
61
82
<summary class = "solution-header">Solution:</summary><p>
62
83
```
63
-
To compues the additional solutions, we add the arguments as specified above
84
+
85
+
To compute the additional solutions, we add the arguments as specified above.
86
+
64
87
```@example intro
65
88
sol2 = solve(prob, dense=false)
66
89
sol3 = solve(prob, RK4())
67
90
68
91
nothing # hide
69
92
```
70
-
For plotting, we create a discretization ```ts``` of the time interval and then plot the four functions
93
+
94
+
We create a discretization ```ts``` of the time interval and then plot the four functions.
We see that all solutions are the same with the exception of the linear approximation.
112
+
We see that all solutions are the same except for the linear approximation.
88
113
89
114
90
115
91
116
## Lorenz system
92
117
93
-
[Lorenz system](https://en.wikipedia.org/wiki/Lorenz_system) is a prime example of the [butterfly effect](https://en.wikipedia.org/wiki/Butterfly_effect) in the chaos theory. There, a small changes in the initial conditions results in large changes after a long time. This effect was first described in 1961 during work on weather modelling.
118
+
The [Lorenz system](https://en.wikipedia.org/wiki/Lorenz_system) is a prime example of the [butterfly effect](https://en.wikipedia.org/wiki/Butterfly_effect) in the chaos theory. There, small changes in the initial conditions result in large changes in the solution. This effect was first described in 1961 during work on weather modelling.
119
+
120
+
The following equations describe the three-dimensional Lorenz system:
94
121
95
-
The three-dimensional Lorenz system is described by the set of equations
\frac{\partial y}{\partial t} &= x (\rho - z) - y, \\
100
126
\frac{\partial z}{\partial t} &= x y - \beta z.
101
127
\end{aligned}
102
128
```
103
-
We define the right-hand side
129
+
130
+
We first define the right-hand side of the system.
131
+
104
132
```@example intro
105
133
function lorenz(u, p, t)
106
134
σ, ρ, β = p
@@ -112,7 +140,9 @@ end
112
140
113
141
nothing # hide
114
142
```
115
-
The parameters are saved in a tuple or array ```p```. Since the right-hand side of the Lorenz system is a vector, we need to return a vector as well. Now, we compute the solution in the same way as before.
143
+
144
+
The parameters are saved in a tuple or array `p`. Since the right-hand side of the Lorenz system is a vector, we need to return a vector as well. Now, we compute the solution in the same way as before.
145
+
116
146
```@example intro
117
147
u0 = [1.0; 0.0; 0.0]
118
148
p = [10; 28; 8/3]
@@ -124,7 +154,9 @@ sol = solve(prob)
124
154
125
155
nothing # hide
126
156
```
127
-
Using the same function to plot
157
+
158
+
We plot the solution:
159
+
128
160
```@example intro
129
161
plot(sol)
130
162
@@ -133,7 +165,8 @@ savefig("lorenz0.svg") # hide
133
165
134
166

135
167
136
-
results in two-dimensional graph of all coorinates. To plot 3D, we need to specify it
168
+
Since this is a two-dimensional graph of all coordinates, we need to specify that we want to plot a 3D graph.
169
+
137
170
```@example intro
138
171
plt1 = plot(sol, vars=(1,2,3), label="")
139
172
@@ -142,7 +175,8 @@ savefig("lorenz1.svg") # hide
142
175
143
176

144
177
145
-
We see again the power of interpolation. If we used linear interpolation (connected the points)
178
+
We see the power of interpolation again. If we used linear interpolation, which amounts to connecting the points, we would obtain a much coarse graph.
we would obtain a much coarse graph. This shows the strength of the ```DifferentialEquations``` package. With a small computational effort, it is able to compute a good solution. Note that the last plotting call is equivalent to
188
+
This graph shows the strength of the `DifferentialEquations` package. With a small computational effort, it can compute a good solution. Note that the last plotting call is equivalent to:
189
+
155
190
```julia
156
191
traj =hcat(sol.u...)
157
192
plot(traj[1,:], traj[2,:], traj[3,:]; label="")
158
193
```
159
194
160
-
In the introduction to this part, we mentioned the chaos theory. We will elaborate on this in the next exercise.
195
+
In the introduction, we mentioned chaos theory. We will elaborate on this in the following exercise.
161
196
162
197
```@raw html
163
198
<div class = "exercise-body">
164
199
<header class = "exercise-header">Exercise:</header><p>
165
200
```
166
-
Consider the same setting as above but perturb the first parameter of ```p``` by the smallest possible value (with respect to the machine precision). Then solve the Lorenz system again and compare results by plotting the two trajectories next to each other.
201
+
202
+
Use the `nextfloat` function to perturb the first parameter of `p` by the smallest possible value. Then solve the Lorenz system again and compare the results by plotting the two trajectories next to each other.
203
+
167
204
```@raw html
168
205
</p></div>
169
206
<details class = "solution-body">
170
207
<summary class = "solution-header">Solution:</summary><p>
171
208
```
172
-
The machine precision can be obtained by ```eps(T)```, where ```T``` is the desired type. However, when we add this to ```p[1]```, we obtain the same number
209
+
210
+
We start with the smallest possible perturbation of the initial value.
211
+
173
212
```@example intro
174
-
p[1] + eps(eltype(p)) == p[1]
213
+
p0 = (nextfloat(p[1]), p[2:end]...)
175
214
```
176
-
The reason is that ```p[1]``` has value 10 and the sum exceeds the allowed number of valid digits and it is truncated back to 10. We therefore cheat a bit and manually modify the number
The solutions look obviously different. Comparing the terminal states of both solutions
233
+
The solutions look different. Comparing the terminal states of both solutions
234
+
200
235
```@example intro
201
236
hcat(sol(tspan[2]), sol0(tspan[2]))
202
237
```
238
+
203
239
shows that they are different by a large margin. This raises a natural question.
204
240
205
241
206
242
```@raw html
207
243
<div class = "exercise-body">
208
244
<header class = "exercise-header">Exercise:</header><p>
209
245
```
246
+
210
247
Can we trust the solutions? Why?
248
+
211
249
```@raw html
212
250
</p></div>
213
251
<details class = "solution-body">
214
252
<summary class = "solution-header">Solution:</summary><p>
215
253
```
254
+
216
255
Unfortunately, we cannot. Numerical methods always introduce some errors by
217
256
-*Rounding errors* due to representing real numbers in machine precision.
218
-
-*Discretization errors* for continuous systems when the derivative is approximated by some kind of finite difference.
219
-
However, if the system itself is unstable in the way that an extremely small perturbation results in big differences in solutions, the numerical method even enhances these errors. The solution could be trusted on some small interval but not after it.
257
+
-*Discretization errors* for continuous systems when the finite difference method approximates the derivative.
258
+
However, if the system itself is unstable and an extremely small perturbation results in big differences in solutions, the numerical method even enhances these errors. The solution could be trusted on some small interval but not after it.
259
+
220
260
```@raw html
221
261
</p></details>
222
262
```
223
263
224
-
The next section will show a situation where we try to mitigate this possible effect by using mathematical formulas to compute the exact solution as long as possible. This delays the necessary discretization and may bring a better stability.
264
+
The following section shows a situation where we try to mitigate this possible effect by using mathematical formulas to compute the exact solution as long as possible. This aproach delays the necessary discretization and may bring better stability.
0 commit comments