-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathNewton_saddle_3D.html
More file actions
544 lines (495 loc) · 18.6 KB
/
Copy pathNewton_saddle_3D.html
File metadata and controls
544 lines (495 loc) · 18.6 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://interactivecomputergraphics.github.io/physics-simulation/examples/style.css">
<title>Newton's Method — The Saddle Point Problem</title>
<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" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js"></script>
<script src="https://cdn.plot.ly/plotly-2.26.0.min.js"></script>
</head>
<body>
<header class="page-header">
<h1>Newton's Method — The Saddle Point Problem</h1>
</header>
<main>
<!-- Simulation panel: canvas + controls -->
<div class="card sim-panel">
<div class="sim-canvas-wrap">
<div id="plot3d" style="width:100%;height:620px;border:2px solid #444;border-radius:4px;"></div>
<div id="statusText">Loading…</div>
</div>
</div>
<div class="controls-panel" style="width: 100%;align:center;margin-left:auto;margin-right:auto">
<h3>Controls</h3>
<div class="controls-grid" style="width: 500px;align:left">
<label>Newton steps</label>
<p style="display:flex;align-items:center;gap:6px">
<button onclick="sim.changeSteps(-1)" style="width:30px;height:30px;font-size:16px;cursor:pointer">−</button>
<input type="text" id="stepsInput" value="1" readonly style="width:40px;text-align:center">
<button onclick="sim.changeSteps(+1)" style="width:30px;height:30px;font-size:16px;cursor:pointer">+</button>
</p>
<label>x₀ = </label>
<span id="x0Display" style="font-family:monospace;background:#fff;padding:2px 9px;border-radius:6px;border:1px solid #bbb;"></span>
Presets:
<button onclick="sim.setPreset('saddle')"
title="Starts near the non-convex strip — converges to the saddle with standard Newton">
▸ Saddle start (0.10, 1.00)
</button>
<label></label>
<button onclick="sim.setPreset('min')"
title="Starts in the convex basin — converges to a minimum with both methods">
▸ Minimum start (1.50, 0.50)
</button>
<label for="showNonConvex">Show non-convex regions</label>
<input type="checkbox" id="showNonConvex" onchange="sim.update()">
<label for="useProjected">Projected Newton</label>
<input type="checkbox" id="useProjected" onchange="sim.update()">
<label for="useProjected">ε =</label>
<p><input type="range" id="epsSlider" min="0.1" max="4.0" step="0.1" value="2.0"
oninput="document.getElementById('epsVal').textContent=
parseFloat(this.value).toFixed(1); sim.update();">
<span id="epsVal" style="font-family:monospace;min-width:34px;">2.0</span>
</p>
</div>
<p style="margin:8px 0 0 4px;color:#555;font-size:0.87em;">
Click anywhere on the surface to place the starting point x₀.
</p>
</div>
<!-- Theory section -->
<div class="card theory">
<h2>Background</h2>
<h3>The double-well function</h3>
<p>
$$f(x_1, x_2) = (x_1^2 - 1)^2 + \frac{1}{2}x_2^2$$
</p>
<p>
This function has two global minima at $\mathbf{x}^* = (\pm 1,\,0)$ with $f = 0$,
and a <strong>saddle point</strong> at the origin $(0,0)$ with $f = 1$.
It serves as a minimal, transparent example where pure Newton's method does not converge to a minimum.
</p>
<h3>Gradient and Hessian</h3>
<p>
$$\nabla f = \begin{pmatrix}4x_1(x_1^2-1)\\x_2\end{pmatrix},
\qquad
\mathbf{H}(x_1,x_2) = \begin{pmatrix}12x_1^2-4 & 0\\0 & 1\end{pmatrix}$$
</p>
<p>
At the saddle: $\mathbf{H}(0,0) = \text{diag}(-4,\;1)$.
One eigenvalue is negative — the Hessian is <em>indefinite</em>.
The quadratic Taylor model $q(\mathbf{x}) = f + \nabla f^\top \delta + \frac{1}{2}\delta^\top\mathbf{H}\delta$
is then <em>unbounded below</em>: it has no global minimizer.
</p>
<h3>Non-convex regions (checkbox 1)</h3>
<p>
The smaller Hessian eigenvalue $12x_1^2 - 4$ is negative wherever
$$|x_1| < \frac{1}{\sqrt{3}} \approx 0.577.$$
Inside this vertical strip the function is locally non-convex.
Enable the first checkbox to highlight the non-convex part of the surface in orange.
</p>
<h3>Why Newton converges to the saddle</h3>
<p>
Newton's update $\mathbf{x}_{n+1} = \mathbf{x}_n - \mathbf{H}^{-1}\nabla f$
minimizes the local quadratic model.
When the Hessian is indefinite, the "minimizer" of that unbounded model
can be a saddle point of $f$.
Starting near $(0.10,\;1.00)$: the Hessian at that point is
$\mathbf{H} \approx \text{diag}(-3.88,\;1)$, and the Newton step
lands almost exactly on the saddle $(0,0)$ in one iteration.
Once there, $\nabla f = \mathbf{0}$ and the method stalls — at a point that
is <em>not</em> a local minimum.
</p>
<h3>Projected Newton — eigenvalue clamping (checkbox 2)</h3>
<p>
One standard remedy is to replace $\mathbf{H}$ by a positive-definite surrogate
$\mathbf{H}_\varepsilon$ obtained by clamping all eigenvalues from below:
$$\mathbf{H}_\varepsilon
= \mathbf{V}\,\text{diag}\!\bigl(\max(\lambda_1,\varepsilon),\;
\max(\lambda_2,\varepsilon)\bigr)\,\mathbf{V}^\top, \qquad \varepsilon > 0,$$
where $\mathbf{H} = \mathbf{V}\boldsymbol{\Lambda}\mathbf{V}^\top$ is the
eigendecomposition. Because $\mathbf{H}_\varepsilon \succ 0$, the modified step
$\mathbf{x}_{n+1} = \mathbf{x}_n - \mathbf{H}_\varepsilon^{-1}\nabla f$
is always a descent direction ($\nabla f^\top \mathbf{H}_\varepsilon^{-1} \nabla f > 0$).
A <strong>backtracking line search</strong> (Armijo condition) is applied to control
the step length and guarantee a sufficient decrease in $f$.
</p>
<p>
Use the slider to vary $\varepsilon$ and observe the trade-off:
large $\varepsilon$ strongly deflects the step away from the saddle but slows convergence
(it resembles gradient descent when $\varepsilon \gg \|\mathbf{H}\|$);
small $\varepsilon$ keeps close to the original Newton step and converges faster
once the iterates leave the non-convex region.
</p>
<p>
In practice, eigenvalue clamping is combined with a line search or trust-region strategy.
</p>
</div>
</main>
<!-- ===================== SCRIPT ===================== -->
<script>
// =======================================================================
// 2×2 linear algebra helpers
// =======================================================================
function solve2x2(H, g)
{
const [a, b] = H[0], [c, d] = H[1];
const det = a * d - b * c;
if (Math.abs(det) < 1e-14) return [0, 0];
return [(d * g[0] - b * g[1]) / det,
(a * g[1] - c * g[0]) / det];
}
function eigenvalues2x2sym(H)
{
const a = H[0][0], b = H[0][1], d = H[1][1];
const tr = a + d;
const disc = Math.sqrt(Math.max(0, (a - d) ** 2 + 4 * b * b));
return [(tr + disc) / 2, (tr - disc) / 2];
}
function eigenvectors2x2sym(H)
{
const a = H[0][0], b = H[0][1], d = H[1][1];
const disc = Math.sqrt(Math.max(0, (a - d) ** 2 + 4 * b * b));
const lam1 = ((a + d) + disc) / 2;
let v1 = Math.abs(b) > 1e-12
? [lam1 - d, b]
: (Math.abs(a) >= Math.abs(d) ? [1, 0] : [0, 1]);
const len = Math.hypot(v1[0], v1[1]);
v1 = [v1[0] / len, v1[1] / len];
return [v1, [-v1[1], v1[0]]];
}
// Project H to PD by clamping eigenvalues to >= eps
function projectHessian(H, eps)
{
const lams = eigenvalues2x2sym(H);
const [v1, v2] = eigenvectors2x2sym(H);
const l1 = Math.max(lams[0], eps);
const l2 = Math.max(lams[1], eps);
return [
[l1 * v1[0] ** 2 + l2 * v2[0] ** 2,
l1 * v1[0] * v1[1] + l2 * v2[0] * v2[1]],
[l1 * v1[1] * v1[0] + l2 * v2[1] * v2[0],
l1 * v1[1] ** 2 + l2 * v2[1] ** 2]
];
}
// =======================================================================
// Double-well function: f = (x1^2 - 1)^2 + x2^2
// Saddle at (0,0), global minima at (±1, 0)
// =======================================================================
function doubleWell(x, y)
{
const A = x * x - 1.0;
return {
f: A * A + 0.5 * y * y,
grad: [4 * x * A, y],
H: [[12 * x * x - 4, 0], [0, 1]]
};
}
// Unicode subscript digits for labeling iterates
const SUBDIGIT = ['₀','₁','₂','₃','₄','₅','₆','₇','₈','₉'];
function sub(i) { return 'x' + (i < 10 ? SUBDIGIT[i] : String(i)); }
// =======================================================================
// One Newton step (standard or projected + Armijo line search)
// =======================================================================
function oneStep(x, useProj, eps)
{
const { f: f0, grad, H } = doubleWell(x[0], x[1]);
const Heff = useProj ? projectHessian(H, eps) : H;
const d = solve2x2(Heff, grad); // Heff · d = grad
if (!useProj)
{
// Standard Newton: full step (may converge to saddle)
return [x[0] - d[0], x[1] - d[1]];
}
// Projected Newton: backtracking line search (Armijo condition).
// Direction p = -d. grad · p = -(grad · d) = -(grad · H_eps^{-1} · grad) < 0 → descent.
const slope = -(grad[0] * d[0] + grad[1] * d[1]);
const C1 = 1e-4;
let alpha = 1.0;
for (let k = 0; k < 30; k++)
{
const xt = [x[0] - alpha * d[0], x[1] - alpha * d[1]];
if (doubleWell(xt[0], xt[1]).f <= f0 + C1 * alpha * slope) return xt;
alpha *= 0.5;
}
return [x[0] - alpha * d[0], x[1] - alpha * d[1]];
}
// =======================================================================
// Simulation / Plot controller
// =======================================================================
class Sim
{
constructor()
{
this.x0 = [0.10, 1.00];
this.nSteps = 5;
this.plotInited = false;
}
setPreset(name)
{
if (name === 'saddle') this.x0 = [0.10, 1.00];
if (name === 'min') this.x0 = [1.50, 0.50];
this.update();
}
changeSteps(delta)
{
this.nSteps = Math.max(1, Math.min(20, this.nSteps + delta));
document.getElementById('stepsInput').value = this.nSteps;
this.update();
}
computePath(useProj, eps)
{
const path = [this.x0.slice()];
let cur = this.x0.slice();
for (let i = 0; i < this.nSteps; i++)
{
const next = oneStep(cur, useProj, eps);
if (!isFinite(next[0]) || !isFinite(next[1])) break;
// Stop if diverging far outside the interesting domain
if (Math.abs(next[0]) > 8 || Math.abs(next[1]) > 8) break;
path.push(next.slice());
cur = next;
}
return path;
}
// Classify the last iterate
classifyPt(p)
{
const fv = doubleWell(p[0], p[1]).f;
const dSaddle = Math.hypot(p[0], p[1]);
const dMinP = Math.hypot(p[0] - 1.0, p[1]);
const dMinN = Math.hypot(p[0] + 1.0, p[1]);
const TOLS = 0.07;
if (dSaddle < TOLS) return `<span style="color:#c00;font-weight:bold">SADDLE (0, 0) — not a minimum! f = ${fv.toFixed(4)}</span>`;
if (dMinP < TOLS) return `<span style="color:#080;font-weight:bold">minimum (+1, 0), f = ${fv.toExponential(3)}</span>`;
if (dMinN < TOLS) return `<span style="color:#080;font-weight:bold">minimum (−1, 0), f = ${fv.toExponential(3)}</span>`;
return `(${p[0].toFixed(3)}, ${p[1].toFixed(3)}), f = ${fv.toFixed(4)}`;
}
buildStatusHtml(pathStd, pathProj, useProj)
{
const n = pathStd.length - 1;
const last = pathStd[pathStd.length - 1];
let html = `<b>Standard Newton</b> after ${n} step${n !== 1 ? 's' : ''} → ${this.classifyPt(last)}`;
if (useProj && pathProj)
{
const np = pathProj.length - 1;
const lastp = pathProj[pathProj.length - 1];
html += `<br><b>Projected Newton</b> (ε = ${document.getElementById('epsVal').textContent}) after ${np} step${np !== 1 ? 's' : ''} → ${this.classifyPt(lastp)}`;
}
return html;
}
update()
{
// Read controls
const showNC = document.getElementById('showNonConvex').checked;
const useProj = document.getElementById('useProjected').checked;
const eps = parseFloat(document.getElementById('epsSlider').value);
document.getElementById('x0Display').textContent =
`(${this.x0[0].toFixed(2)}, ${this.x0[1].toFixed(2)})`;
// ---------------------------------------------------------------
// Build surface grid
// ---------------------------------------------------------------
const NX = 72, NY = 60;
const X1LO = -2.3, X1HI = 2.3, X2LO = -1.9, X2HI = 1.9;
const xArr = Array.from({ length: NX }, (_, i) => X1LO + (X1HI - X1LO) * i / (NX - 1));
const yArr = Array.from({ length: NY }, (_, j) => X2LO + (X2HI - X2LO) * j / (NY - 1));
const zGrid = []; // full surface
const zNonCvx = []; // NaN where convex, z where non-convex (min eigenvalue < 0)
for (let j = 0; j < NY; j++)
{
const rowZ = [], rowNC = [];
for (let i = 0; i < NX; i++)
{
const { f, H } = doubleWell(xArr[i], yArr[j]);
const lams = eigenvalues2x2sym(H);
const minLam = Math.min(lams[0], lams[1]);
rowZ.push(f);
rowNC.push(minLam < 0 ? f : NaN);
}
zGrid.push(rowZ);
zNonCvx.push(rowNC);
}
// ---------------------------------------------------------------
// Compute Newton paths
// ---------------------------------------------------------------
const pathStd = this.computePath(false, eps);
const pathProj = useProj ? this.computePath(true, eps) : null;
document.getElementById('statusText').innerHTML =
this.buildStatusHtml(pathStd, pathProj, useProj);
// ---------------------------------------------------------------
// Build Plotly traces
// ---------------------------------------------------------------
const Z_LIFT = 0.06; // raise paths/markers above the surface to avoid clipping
const traces = [];
// 1. Main surface (colored by function value)
traces.push({
type: 'surface',
x: xArr, y: yArr, z: zGrid,
colorscale: 'Jet',
cmin: 0,
cmax: 5,
showscale: true,
colorbar: { title: 'f(x₁,x₂)', thickness: 14, len: 0.65, x: 1.01 },
opacity: showNC ? 0.80 : 1.0,
name: 'f(x₁,x₂)',
contours: {
x: { show: true, start: -2.0, end: 2.0, size: 0.1,
color: 'rgba(60,60,60,0.55)', width: 0.1, highlight: false },
y: { show: true, start: -1.5, end: 1.5, size: 0.1,
color: 'rgba(60,60,60,0.55)', width: 0.1, highlight: false },
z: { show: false }
},
hovertemplate: 'x₁ = %{x:.3f}<br>x₂ = %{y:.3f}<br>f = %{z:.4f}<extra></extra>'
});
// 2. Non-convex overlay: same surface but shown only where min eigenvalue < 0
if (showNC)
{
traces.push({
type: 'surface',
x: xArr, y: yArr, z: zNonCvx,
colorscale: [[0, 'rgb(255,100,0)'], [1, 'rgb(255,100,0)']],
showscale: false,
opacity: 0.62,
name: 'Non-convex (λ_min < 0)',
hoverinfo: 'skip'
});
}
// 3. Standard Newton path (white)
const zStd = pathStd.map(p => doubleWell(p[0], p[1]).f + Z_LIFT);
traces.push({
type: 'scatter3d',
x: pathStd.map(p => p[0]),
y: pathStd.map(p => p[1]),
z: zStd,
mode: 'lines+markers+text',
line: { color: '#ffffff', width: 6 },
marker: { color: '#ffffff', size: 7, symbol: 'circle',
line: { color: '#333', width: 1 } },
text: pathStd.map((_, i) => sub(i)),
textfont: { size: 12, color: '#fff' },
textposition: 'top center',
name: 'Standard Newton',
showlegend: true
});
// 4. Projected Newton path (orange), only when checkbox is on
if (useProj && pathProj)
{
const zProj = pathProj.map(p => doubleWell(p[0], p[1]).f + Z_LIFT);
traces.push({
type: 'scatter3d',
x: pathProj.map(p => p[0]),
y: pathProj.map(p => p[1]),
z: zProj,
mode: 'lines+markers+text',
line: { color: '#ff9900', width: 6 },
marker: { color: '#ff9900', size: 7, symbol: 'circle',
line: { color: '#333', width: 1 } },
text: pathProj.map((_, i) => sub(i)),
textfont: { size: 12, color: '#ff9900' },
textposition: 'top center',
name: `Projected Newton (ε = ${eps.toFixed(1)})`,
showlegend: true
});
}
// 5. Saddle point marker
traces.push({
type: 'scatter3d',
x: [0], y: [0], z: [doubleWell(0, 0).f + Z_LIFT],
mode: 'markers+text',
marker: { color: '#ff2222', size: 10, symbol: 'circle',
line: { color: 'white', width: 2 } },
text: ['Saddle (0,0)'],
textfont: { size: 12, color: '#ff4444' },
textposition: 'top center',
name: 'Saddle point',
showlegend: true
});
// 6. Global minima markers
traces.push({
type: 'scatter3d',
x: [1.0, -1.0], y: [0, 0], z: [Z_LIFT, Z_LIFT],
mode: 'markers+text',
marker: { color: '#00ee66', size: 10, symbol: 'circle',
line: { color: 'white', width: 2 } },
text: ['Min (+1, 0)', 'Min (−1, 0)'],
textfont: { size: 12, color: '#00ee66' },
textposition: 'top center',
name: 'Global minima',
showlegend: true
});
// ---------------------------------------------------------------
// Layout — preserve user camera after first render
// ---------------------------------------------------------------
const gd = document.getElementById('plot3d');
let camera = { eye: { x: 1.2, y: -1.4, z: 1.6 } };
if (this.plotInited && gd._fullLayout && gd._fullLayout.scene)
{
camera = JSON.parse(JSON.stringify(gd._fullLayout.scene.camera));
}
const layout = {
title: {
text: "f (x₁, x₂) = (x₁² − 1)² + ½ x₂²",
font: { size: 15 }
},
scene: {
xaxis: { title: 'x₁', range: [X1LO, X1HI] },
yaxis: { title: 'x₂', range: [X2LO, X2HI] },
zaxis: { title: 'f', range: [-0.1, 3.5] },
camera: camera,
bgcolor: '#f5f5f8',
aspectmode: 'manual',
aspectratio: { x: 1.2, y: 1.0, z: 0.65 }
},
legend: {
x: 0.01, y: 0.99,
xanchor: 'left', yanchor: 'top',
bgcolor: 'rgba(30,30,30,0.80)',
bordercolor: '#888', borderwidth: 1,
font: { color: 'white', size: 11 }
},
margin: { l: 0, r: 0, t: 38, b: 0 },
paper_bgcolor: '#e4ecf4'
};
if (!this.plotInited)
{
Plotly.newPlot('plot3d', traces, layout).then(plotDiv =>
{
plotDiv.on('plotly_click', e =>
{
if (e.points && e.points.length > 0)
{
const pt = e.points[0];
if (typeof pt.x === 'number' && typeof pt.y === 'number')
{
this.x0 = [pt.x, pt.y];
// Defer so Plotly finishes the click event before
// react() is called — avoids re-entrancy hang.
setTimeout(() => this.update(), 0);
}
}
});
});
this.plotInited = true;
}
else
{
Plotly.react('plot3d', traces, layout);
}
}
}
const sim = new Sim();
sim.update();
</script>
</body>
</html>