下面是 armijo线搜索+最速下降法的小程序,matlab用的很不熟,费了不少劲。
函数:
function g=fun_obj(x) syms a b f = 1/2*a^2+b^2-a*b-2*a; a=x(1);b=x(2); g=eval(f);
求梯度:
function g=fun_grad(x) syms a b f = 1/2*a^2+b^2-a*b-2*a; gradient = jacobian(f,[a,b]); a = x(1);b = x(2); g = eval(gradient);
armijo线搜索:
function mk = armijo( xk, rho, sigma, d ) assert( rho > 0 && rho < 1 ); assert( sigma > 0 && sigma < 0.5 ); mk = 0; max_mk = 100; while mk <= max_mk x = xk + rho^mk * d; if fun_obj( x ) <= fun_obj( xk ) + sigma * rho^mk *fun_grad(xk)*d'; break; end mk = mk + 1; end return;
主程序:
function result = armijograd(x0) max_iter = 5000; % max number of iterations EPS = 1e-6; % threshold of gradient norm rho = 0.45; sigma = 0.2; % Armijo parameters k = 0; xk = x0; % initialization while k < max_iter k = k + 1; dk = fun_grad( xk ); % gradient vector d = -1 * dk; % search direction if norm( dk ) < EPS %precision break; end mk = armijo( xk, rho, sigma, d); %armijo line search xk = xk + rho^mk * d; %update end result = xk; return;
最终结果是:[4,2]';程序正确。