Matlab中最速下降法的实现

Implementation of steepest descent in Matlab

我必须使用 Matlab 实现最速下降法并在两个变量的函数上对其进行测试。这是我到目前为止所做的:

x_0 = [0;1.5]; %Initial guess
alpha = 1.5; %Step size
iteration_max = 10000;
tolerance = 10e-10;

% Two anonymous function to compute 1st and 2nd entry of gradient
f = @(x,y) (cos(y) * exp(-(x-pi)^2 - (y-pi)^2) * (sin(x) - 2*cos(x)*(pi-x)));
g = @(x,y) (cos(x) * exp(-(x-pi)^2 - (y-pi)^2) * (sin(y) - 2*cos(y)*(pi-y)));

%Initiliazation
iter = 0;
grad = [1; 1]; %Gradient

while (norm(grad,2) >= tolerance)
    grad(1,1) = f(x_0(1), x_0(2));
    grad(2,1) = g(x_0(1), x_0(2));
    x_new = x_0 - alpha * grad; %New solution
    x_0 = x_new %Update old solution
    iter = iter + 1;

    if iter > iter_max
        break
    end
end

问题是,与 WolframAlpha 的结果相比,我没有获得相同的值。对于这个特定的功能,我应该获得 (3.14,3.14) 或 (1.3,1.3) 但我获得 (0.03, 1.4).

你应该知道这种方法是局部搜索,因此它可能会陷入局部最小值,具体取决于初始猜测和步长。

  • 不同的初始猜测,它会找到不同的局部最小值。

  • 步长很重要,因为大的步长会阻止算法收敛。小步长会使算法非常慢。这就是为什么您应该在函数值减小时调整步长的原因。

通过绘图(如果可能)来理解您想要优化的功能始终是个好主意。您正在使用的函数如下所示(在 [-pi pi] 范围内):

使用以下参数值,您将获得所需的局部最小值。

x_0 = [2;2]; %Initial guess
alpha = 0.5; %Step size