yancr
霏霏小雨
2019-03-31 20:26

matlab 优化方法中求最小值,使用非精确搜索获取步长、最速下降获取方向

  • 开发语言

老师布置了这样的


图片说明

我在matlab里面写,,最后一直迭代不出来。。


图片说明


图片说明


图片说明


图片说明


代码是这样的。。不怎么用matlab,求指教。。

clear;
clc;
x_k = [0; 0];
z = 1e-4;
syms f(x_1, x_2);
syms g(x_1, x_2);
syms l(y);
syms m(y);
f = vpa(100 * (x_1 ^ 2 - x_2) ^ 2 + (x_1 - 1) ^ 2, 20); %原函数
g = vpa([diff(f, x_1); diff(f, x_2)], 20); %导数
c_1 = 0.1;
c_2 = 0.5;
y = 1;
f_k = vpa(subs(f, [x_1; x_2], x_k), 20)
g_k = vpa(subs(g, [x_1; x_2], x_k), 20)
x_kk = vpa(x_k - y * g_k, 20);
f_kk = vpa(subs(f, [x_1; x_2], x_kk), 20)
g_kk = vpa(subs(g, [x_1; x_2], x_kk), 20)
while norm(g_kk, 2) >= z
    getStep = 1;
    a = 0;
    b = inf;
    y = 1;
    while getStep
        if f_k - f_kk >= - c_1 * y * g_k' * (-g_k)
            if  g_kk' * (-g_k) >= c_2 * g_k' * (-g_k)
                getStep = 0
            else
                a = y;
                y = vpa(min(2 * y, (y + b) / 2), 20)
                x_kk = vpa(x_k - y * g_k, 20);
                f_kk = vpa(subs(f, [x_1; x_2], x_kk), 20)
                g_kk = vpa(subs(g, [x_1; x_2], x_kk), 20)
            end
        else
            b = y;
            y = vpa((a + y) / 2, 20)
            x_kk = vpa(x_k - y * g_k, 20);
            f_kk = vpa(subs(f, [x_1; x_2], x_kk), 20)
            g_kk = vpa(subs(g, [x_1; x_2], x_kk), 20)
        end
    end
    f_k = f_kk;
    g_k = g_kk;
    x_kk = vpa(x_k - y * g_k, 20);
    f_kk = vpa(subs(f, [x_1; x_2], x_kk), 20)
    g_kk = vpa(subs(g, [x_1; x_2], x_kk), 20)
end


  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答