-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathNewton_minimization.html
More file actions
306 lines (278 loc) · 7.63 KB
/
Newton_minimization.html
File metadata and controls
306 lines (278 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<style>
body {font-family: Helvetica, sans-serif;}
table {background-color:#CCDDEE;text-align:left}
</style>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js"],
jax: ["input/TeX", "output/HTML-CSS"],
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
processEscapes: true
},
"HTML-CSS": { fonts: ["TeX"] }
});
</script>
<script type="text/javascript" aync src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js"></script>
<script src="https://cdn.plot.ly/plotly-2.5.1.min.js"></script>
<title>Newton's method for minimization</title>
</head>
<body>
<main>
<h1 style="text-align:center">Newton's method for minimization</h1>
<table style="align_center;border-radius: 20px;padding: 20px;margin:auto">
<col width="1000">
<tr>
<td>
<div id="plotOutput" style="width: 1000px; height: 600px;border:2px solid #000000;border-radius: 0px;background-color:#EEEEEE"></div>
</td>
</tr>
<tr>
<td><table style="margin:20px">
<col width="200" style="padding-right:10px">
<col width="100">
<tr>
<td><label for="newton_steps">Newton steps</label></td>
<td><input type="text" id="textInput" value="1" readonly></td>
</tr>
<tr>
<td></td>
<td><input onchange="document.getElementById('textInput').value=this.value;plot.reset()" id="newton_steps" value="1" type="range" min="1" max="50" step="1"></td>
</tr>
<tr>
<td><label for="fct">Function</label></td>
<td><select onchange="plot.reset()" id="fct" size="1">
<option selected="selected">Quartic function</option>
<option>Sinusoidal function</option>
<option>Exponential function</option>
<option>Logarithmic function</option>
</select>
</td>
</tr>
</table></td>
</tr>
<tr><td>
<h2>Newton's method for minimization</h2>
<p>Newton's method can be used to find the minimum of a function $f(x)$. At a minimum the derivative vanishes, $f'(x^*) = 0$, so minimization reduces to finding the root of $f'(x)$.</p>
<p>At each iterate $x_n$ a quadratic (second-order Taylor) approximation is formed:</p>
$$q(x) = f(x_n) + f'(x_n)(x-x_n) + \frac{1}{2}f''(x_n)(x-x_n)^2$$
<p>The next iterate $x_{n+1}$ is the minimizer of $q(x)$, giving the update rule:</p>
$$\begin{equation*}
x_{n+1} = x_{n} - \frac{f'(x_n)}{f''(x_n)}
\end{equation*}$$
<p>The green curves show the quadratic approximation at each step. Provided $f''(x_n) > 0$ and $x_0$ is close enough to a local minimum, the method converges rapidly.</p>
</td></tr>
</table>
</main>
<script id="simulation_code" type="text/javascript">
class Plot
{
constructor()
{
this.reset();
this.num_newton_steps = 1;
}
reset()
{
this.num_newton_steps = parseInt(document.getElementById('newton_steps').value);
this.fct = document.getElementById('fct').value;
this.plotFunctions();
}
// f(x) = x^4 - 4x^2 + 2 (two minima at x = ±√2 ≈ ±1.414)
quartic_function(x)
{
return x*x*x*x - 4*x*x + 2;
}
grad_quartic_function(x)
{
return 4*x*x*x - 8*x;
}
hess_quartic_function(x)
{
return 12*x*x - 8;
}
// f(x) = 0.5x^2 + 2sin(x) (local minimum near x ≈ -1.03)
sinusoidal_function(x)
{
return 0.5*x*x + 2*Math.sin(x);
}
grad_sinusoidal_function(x)
{
return x + 2*Math.cos(x);
}
hess_sinusoidal_function(x)
{
return 1 - 2*Math.sin(x);
}
// f(x) = exp(x) - 3x (minimum at x = ln 3 ≈ 1.099)
exponential_function(x)
{
return Math.exp(x) - 3*x;
}
grad_exponential_function(x)
{
return Math.exp(x) - 3;
}
hess_exponential_function(x)
{
return Math.exp(x);
}
// f(x) = x²/2 - 3·ln(x+4) (minimum at x = -2+√7 ≈ 0.646)
// Strictly convex; the log term makes the quadratic approximations
// visibly different from the true curve.
logarithmic_function(x)
{
return 0.5*x*x - 3*Math.log(x + 4);
}
grad_logarithmic_function(x)
{
return x - 3/(x + 4);
}
hess_logarithmic_function(x)
{
return 1 + 3/((x + 4)*(x + 4));
}
newton_step(grad_f, hess_f, x)
{
const h = hess_f(x);
if (Math.abs(h) < 1e-10)
return x;
return x - grad_f(x) / h;
}
computeData(fct, grad_fct, hess_fct, x0, data)
{
let xValues = [];
let yValues = [];
let x = -3;
let num_steps = 5000;
for (let i = 0; i <= num_steps; i++)
{
xValues.push(x);
yValues.push(fct(x));
x += 6 / num_steps;
}
// Collect Newton iterates: x_0, x_1, ..., x_{num_newton_steps}
let iterates = [x0];
let current_x = x0;
for (let i = 0; i < this.num_newton_steps; i++)
{
current_x = this.newton_step(grad_fct, hess_fct, current_x);
iterates.push(current_x);
}
// Main function trace
data.push({
x: xValues,
y: yValues,
name: "f(x)",
showlegend: true,
line: { color: 'rgb(31, 119, 180)', width: 2 }
});
// For each Newton step: draw the local quadratic approximation and
// a vertical dashed line marking the current iterate x_n
for (let i = 0; i < this.num_newton_steps; i++)
{
const xn = iterates[i];
const xn1 = iterates[i + 1];
const fn = fct(xn);
const gn = grad_fct(xn);
const hn = hess_fct(xn);
// q(x) = f(xn) + f'(xn)(x-xn) + 0.5*f''(xn)(x-xn)^2
// drawn over a range that covers both xn and xn1
const lo = Math.min(xn, xn1) - 0.4;
const hi = Math.max(xn, xn1) + 0.4;
const qx = [], qy = [];
for (let j = 0; j <= 200; j++)
{
const xq = lo + j * (hi - lo) / 200;
const dx = xq - xn;
qx.push(xq);
qy.push(fn + gn * dx + 0.5 * hn * dx * dx);
}
data.push({
x: qx,
y: qy,
line: { color: 'rgb(0, 150, 0)', width: 2 },
name: "quadratic approx.",
showlegend: i == 0
});
// Vertical dashed line from x-axis up to f(x_n)
data.push({
type: 'line',
x: [xn, xn],
y: [0, fn],
line: { color: 'rgb(0, 0, 0)', width: 2, dash: 'dash' },
text: ["x_" + i.toString(), ""],
textposition: "bottom center",
mode: 'lines+markers+text',
name: "x_n",
showlegend: i == 0
});
}
// Vertical dashed line for the final iterate
const xFinal = iterates[this.num_newton_steps];
const fFinal = fct(xFinal);
data.push({
type: 'line',
x: [xFinal, xFinal],
y: [0, fFinal],
line: { color: 'rgb(0, 0, 0)', width: 2, dash: 'dash' },
text: ["x_" + this.num_newton_steps.toString(), ""],
textposition: "bottom center",
mode: 'lines+markers+text',
name: "x_n",
showlegend: false
});
}
plotFunctions()
{
var data = [];
if (this.fct == "Quartic function")
{
this.computeData(
this.quartic_function,
this.grad_quartic_function,
this.hess_quartic_function,
2.5, data)
}
if (this.fct == "Sinusoidal function")
{
this.computeData(
this.sinusoidal_function,
this.grad_sinusoidal_function,
this.hess_sinusoidal_function,
-2.5, data)
}
if (this.fct == "Exponential function")
{
this.computeData(
this.exponential_function,
this.grad_exponential_function,
this.hess_exponential_function,
2.0, data)
}
if (this.fct == "Logarithmic function")
{
this.computeData(
this.logarithmic_function,
this.grad_logarithmic_function,
this.hess_logarithmic_function,
-2.5, data)
}
var layout = {
title: "Minimization with Newton's method",
width: 1000,
height: 600
};
Plotly.newPlot('plotOutput', data, layout);
}
}
plot = new Plot();
plot.reset();
</script>
</body>
</html>