[英]MATLAB code not working (gradient descent algorithm)
我正在尝试梯度下降,我写了以下内容,但没有得到任何答案,
n=0; %initialize iteration counter
eps=1; %initialize error
a=0.8; %set iteration parameter
x=[1;1]; %set starting value
f=6*x(1)^2+8*x(2)^2-3*x(1)*x(2);
%Computation loop
while eps>1e-12||n<100
gradf=[12*x(1)-3*x(2); 16*x(2)-3*x(1)]; %gradf(x)
eps=(norm(gradf)/(1+abs(f))); %error
y=x-a*gradf; %iterate
x=y; %update x
n=n+1; %counter+1
end
n;x;eps; %display end values
当我将此文件添加到路径并键入x时,它显示NaN,NaN。 怎么了?
您的代码中有几个错误。 考虑一下(我在需要更正的地方添加了评论)
n=0;
eps=1;
a=0.1; %You need a way smaller parameter to converge!
x=[1;1];
A = [6 -3/2 ; -3/2 8]; %You have a bilinear positive definite form,
%you may use matrix form for convenience
while eps>1e-12 && n<100 %You had wrong termination conditions!!
gradf=2*A*x; %(gradf in terms of matrix)
f=x'*A*x; %you need to update f every iteration!!
eps=(norm(gradf)/(1+abs(f)))
disp(eps > 1e-12)
x=x-a*gradf;
%Now you can see the orbit towards minimum
plot(x(1),x(2),'o'); hold on
n=n+1;
end
n
x
eps
例如当前值a=.1
我得到
n = 100
eps = 1.2308e-011
x =
1.0e-012 *
-0.2509
0.4688
那是我必须执行100次迭代,因为我的epsilon仍高于阈值。 如果我允许200次迭代,我得到
n = 110
eps =
7.9705e-013
x =
1.0e-013 *
-0.1625
0.3036
即110次迭代就足够了。
一般f
(即不是二次形式)。
例如,您可以使用函数handles ,即定义(在while
之前)
foo = @(x) 6*x(1)^2+8*x(2)^2-3*x(1)*x(2);
foo_x = @(x) 12*x(1)-3*x(2);
foo_y = @(x) 16*x(2)-3*x(1);
然后,在while
你替换
gradf = [foo_x(x);foo_y(x)];
f = foo(x);
PS什么涉及while
周期,请记住,你继续迭代,而你不满意你的精度( eps>1e-12
) 和你的迭代的总数低于给定阈值( n<100
)。
还请考虑您正在以有限的精度工作:数值算法永远无法达到解析解(即无限精度和无限迭代的结果),因此,您始终必须设置一个阈值 ( eps
,该阈值应高于机器)精密\\约1e-16
),这是您的0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.