【发布时间】:2017-07-23 03:33:48
【问题描述】:
我必须使用 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)。
【问题讨论】: