我正在尝试使用最陡峭的方法来最小化离散函数。这应该相当简单,但是我在搜索“爬升”出任何局部最小值时遇到了麻烦。这是我在 Mathematica 中的代码,但它的语法很容易理解。
x = {some ordered pair as a beginning search point};
h = 0.0000001; (* something rather small *)
lambda = 1;
While[infiniteloop == True,
x1 = x[[1]];
x2 = x[[2]];
x1Gradient = (f[x1-h,x2]-f[x1+h,x2])/(2h);
x2Gradient = (f[x1,x2-h]-f[x1,x2+h])/(2h);
gradient = {x1Gradient,x2Gradient};
(* test if minimum is found by normalizing the gradient*)
If[Sqrt[x1Gradient^2 + x2Gradient^2] > 0.000001,
xNew = x + lambda*g,
Break[];
];
(* either pass xNew or reduce lambda *)
If[f[xNew[[1]],xNew[[2]]] < f[x1,x],
x = xNew,
lambda = lambda/2;
];
];
为什么这会爬山?我很困惑,因为我什至测试新值是否小于旧值。当它通过时,我不会通过它!想法?