FindMinimum Newton.nb

In[109]:=
"Palgrave Article - Example 4";
"Newton's Method in FindMinimum Function";
In[111]:=
Clear@x, y, f, x0, y0, n, pointsD;
f = - HSqrt@xD + 2 Sqrt@yD + 3 Sqrt@1 - x - yDL;
x0 = 0.25;
y0 = 0.5;
n = 0;
points = Reap@FindMinimum@f, 88x, x0<, 8y, y0<<,
Method ® "Newton", StepMonitor ¦ 8++n, Sow@8x, y<D<DD@@2, 1DD;
n
points = Join@88x0, y0<<, pointsD
Out[117]=
Out[118]=
In[119]:=
6
880.25, 0.5<, 80.0220779, 0.470563<, 80.0432242, 0.279633<, 80.0627738, 0.288496<,
80.0706762, 0.285944<, 80.0714231, 0.285716<, 80.0714286, 0.285714<<
ContourPlot@f, 8x, 0, 0.3<, 8y, 0, 0.6<, ColorFunction ® Hue,
Epilog ® 8AbsolutePointSize@5D, Point@pointsD, Line@pointsD<D
0.6
0.5
0.4
Out[119]=
0.3
0.2
0.1
0.0
0.00
0.05
0.10
0.15
0.20
0.25
0.30
2
FindMinimum Newton.nb
In[120]:=
"Newton's Method by hand";
In[121]:=
Clear@x, y, f, Gradf, HessefD
f@x_, y_D = - HSqrt@xD + 2 Sqrt@yD + 3 Sqrt@1 - x - yDL;
Gradf@x_, y_D = D@f@x, yD, 88x, y<<D
Hessef@x_, y_D = D@f@x, yD, 88x, y<, 2<D
Out[123]=
Out[124]=
In[125]:=
:::
1
3
3
2
x
2
1
4
1-x-y
-
2
3
+
x32
1
,
+
4 H1 - x - yL
32
y
1-x-y
3
,
4 H1 - x - yL
32
>
>, :
3
4 H1 - x - yL
3
,
32
4 H1 - x - yL
1
+
32
2 y32
Clear@delta, eps, n, step, graderrD;
delta = 10. ^ - 8;
eps = 10. ^ - 8;
n = 0;
x@nD = 0.25;
y@nD = 0.5;
step = 81, 1<;
graderr = Norm@Gradf@x@nD, y@nDDD;
While@ HNorm@stepD  H1 + Norm@8x@nD, y@nD<DL > delta ÈÈ
graderr  H1 + Abs@f@x@nD, y@nDDDL > epsL && n < 100,
step = - Inverse@Hessef@x@nD, [email protected]@x@nD, y@nDD;
H* Bad idea to compute the inverse *L
Print@n, ", ", 8x@nD, y@nD<, ", ", f@x@nD, y@nDD, ", ", step, ", ", graderrD;
8x@n + 1D, y@n + 1D< = 8x@nD, y@nD< + step;
n ++;
graderr = Norm@Gradf@x@nD, y@nDDD;
D
Print@n, ", ", 8x@nD, y@nD<, ", ", f@x@nD, y@nDD, ", 8<, ", graderrD
>>
0, 80.25, 0.5<, - 3.41421, 8- 0.227922, - 0.0294373<, 2.55239
1, 80.0220779, 0.470563<, - 3.65741, 80.0211463, - 0.19093<, 1.41617
2, 80.0432242, 0.279633<, - 3.73417, 80.0195495, 0.00886279<, 0.586084
3, 80.0627738, 0.288496<, - 3.7411, 80.00790245, - 0.00255166<, 0.133295
4, 80.0706762, 0.285944<, - 3.74165, 80.000746859, - 0.00022828<, 0.010691
5, 80.0714231, 0.285716<, - 3.74166, 95.50297 ´ 10-6 , - 1.69505 ´ 10-6 =, 0.000077615
6, 80.0714286, 0.285714<, - 3.74166, 92.93547 ´ 10-10 , - 9.03061 ´ 10-11 =, 4.13997 ´ 10-9
7, 80.0714286, 0.285714<, - 3.74166, 8<, 9.93014 ´ 10-16
FindMinimum Newton.nb
In[135]:=
Out[142]=
Out[143]=
In[144]:=
"Rosenbrock's Banana Function";
Clear@x, y, f, x0, y0, n, pointsD;
f = 100 Hy - x ^ 2L ^ 2 + H1 - xL ^ 2;
x0 = - 1;
y0 = 0;
n = 0;
points = Reap@FindMinimum@f, 88x, x0<, 8y, y0<<,
Method ® "Newton", StepMonitor ¦ 8++n, Sow@8x, y<D<DD@@2, 1DD;
n
points = Join@88x0, y0<<, pointsD
22
88- 1, 0<, 8- 0.99005, 0.9801<, 8- 0.902533, 0.806811<, 8- 0.749508, 0.532183<,
8- 0.496539, 0.182558<, 8- 0.388084, 0.138846<, 8- 0.256617, 0.0405412<,
8- 0.0493312, - 0.0405339<, 80.0600482, - 0.00835807<, 80.191793, 0.0131532<,
80.332933, 0.090924<, 80.466772, 0.199963<, 80.583133, 0.326504<,
80.695557, 0.471161<, 80.781854, 0.603848<, 80.869483, 0.748322<,
80.920953, 0.845506<, 80.972623, 0.943326<, 80.99047, 0.980713<,
80.999429, 0.998779<, 80.999991, 0.999982<, 81., 1.<, 81., 1.<<
ContourPlot@f, 8x, - 1.1, 1.2<, 8y, - 1, 1.2<,
Epilog ® 8AbsolutePointSize@5D, Point@pointsD, Line@pointsD<,
ColorFunction ® Hue, Contours ® Range@0, 100DD
1.0
0.5
Out[144]=
0.0
-0.5
-1.0
-1.0
-0.5
0.0
0.5
1.0
3
4
FindMinimum Newton.nb
In[145]:=
"Palgrave Article - Example 2";
"Newton's Method in FindMinimum Function";
In[212]:=
Clear@x, y, f, x0, y0, n, pointsD;
f = x * Hx - 2L ^ 2;
x0 = 1.;
n = 0;
points = Reap@
FindMinimum@f, 8x, x0<, Method ¦ "Newton", StepMonitor ¦ 8++n, Sow@xD<DD@@2, 1DD;
n
points = Join@8x0<, pointsD
Out[217]=
Out[218]=
In[154]:=
5
81., 1.87498, 2.01443, 2.00015, 2., 2.<
Plot@f, 8x, - 1, 3<D
2
1
-1
2
3
Out[154]=
-2
-4
In[155]:=
"Newton's Method by hand";
In[205]:=
Clear@f, n, x, delta, eps, step, deriverrD;
f@x_D = x * Hx - 2L ^ 2;
x@0D = 1.; n = 0; delta = 10 ^ H- 8L; eps = 10 ^ H- 8L;
step = 1.;
deriverr = Abs@f '@x@nDDD;
While@Abs@stepD  H1 + Abs@x@nDDL > eps ÈÈ deriverr  H1 + Abs@f@x@nDDDL > eps,
step = - f '@x@nDD  f ''@x@nDD;
Print@n, ", ", x@nD, ", ", f@x@nDD, ", ", step, ", ", deriverrD;
x@n + 1D = x@nD + step;
n ++;
deriverr = Abs@f '@x@nDDD
D
Print@n, ", ", x@nD, ", ", f@x@nDD ", ", "8<", ", ", deriverrD
0, 1., 1., - 0.5, 1.
1, 0.5, 1.125, 0.15, 0.75
2, 0.65, 1.18463, 0.0164634, 0.0675
3, 0.666463, 1.18519, 0.000203221, 0.000813132
4, 0.666667, 1.18519, 3.09741 ´ 10-8 , 1.23896 ´ 10-7
5, 0.666667, 1.18519, 6.66134 ´ 10-16 , 2.66454 ´ 10-15
6, 0.666667, 1.18519 , 8<, 2.22045 ´ 10-16