我必须使用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)。