梯度线搜索Matlab代码卡住

计算科学 优化 matlab 非线性规划
2021-12-14 06:29:04
x0=[10 5]';
tol=1e-6;
syms x y
f= (1/2)*(x^2)+(9/2)*(y^2);
g=gradient(f, [x, y]);
g0=subs(g, x, x0(1,1));
g0=subs(g0, y, x0(2,1));
while norm(g0)>tol
    s0=-g0;
    %perform a linesearch to find an acceptable step size
    alpha=1/2;
    L0=1; 
    f0=subs(f,x, x0(1,1));
    f0=subs(f0,y,x0(2,1));

    v=f0;
    xnew=x0+L0*s0;

    f1=subs(f,x, xnew(1,1));
    f1=subs(f,y,xnew(2,1));

    u=f1;
    w=alpha*L0*g0'*s0;

    while u > v+w

        L0=L0/2;

    end

    L0;
    x0=x0+L0*s0;

end

double(x0)

当我尝试在 Matlab 中运行此脚本时。它卡住了。我不知道为什么!代码有问题吗?

1个回答

while 循环只改变L0, 但是u,vw保持不变。所以循环的条件永远不会改变。