考虑无约束优化问题f(x)=100(x-x2)2+(x1-1)2,设当前迭代点x=(-1,1),下降方向d2=(1,-2)T,试用非精确的Armijo搜索准则、Wolfe搜索准则、强Wolfe 搜索准则求解步长a的matlab程序
时间: 2024-03-21 20:16:00 浏览: 122
非精确的Armijo搜索准则:
```matlab
function a = armijo(f, x, d)
sigma = 0.5;
beta = 0.5;
a = 1;
while f(x + a*d) > f(x) + beta*a*grad(f, x)'*d
a = sigma*a;
end
end
function g = grad(f, x)
h = 1e-6;
n = length(x);
g = zeros(n, 1);
for i = 1:n
e = zeros(n, 1);
e(i) = 1;
g(i) = (f(x+h*e) - f(x-h*e)) / (2*h);
end
end
% main function
f = @(x) 100*(x(1)-x(1)^2)^2 + (x(2)-1)^2;
x = [-1;1];
d = [1;-2];
a = armijo(f, x, d)
```
Wolfe搜索准则:
```matlab
function a = wolfe(f, x, d)
sigma = 0.5;
rho = 0.1;
a = 1;
while true
if f(x + a*d) > f(x) + rho*a*grad(f, x)'*d
a = sigma*a;
elseif grad(f, x + a*d)'*d < rho*grad(f, x)'*d
a = sigma*a;
else
break;
end
end
end
% main function
f = @(x) 100*(x(1)-x(1)^2)^2 + (x(2)-1)^2;
x = [-1;1];
d = [1;-2];
a = wolfe(f, x, d)
```
强Wolfe搜索准则:
```matlab
function a = strong_wolfe(f, x, d)
sigma = 0.5;
rho = 0.1;
a = 1;
while true
fx = f(x);
g = grad(f, x);
if f(x + a*d) > fx + rho*a*g'*d
a = zoom(f, x, d, a, fx, g'*d, rho, sigma);
break;
elseif abs(g(f, x + a*d)'*d) <= -rho*g'*d
break;
elseif g(f, x + a*d)'*d >= 0
a = zoom(f, x, d, a, fx, g'*d, rho, sigma);
break;
else
a = sigma*a;
end
end
end
function a = zoom(f, x, d, a_lo, f_lo, g_lo_d, rho, sigma)
a_hi = 2*a_lo;
while true
a = (a_lo + a_hi) / 2;
f_val = f(x + a*d);
if f_val > f_lo + rho*a*g_lo_d || f_val >= f(x + a_lo*d)
a_hi = a;
else
g = grad(f, x + a*d);
if abs(g'*d) <= -rho*g_lo_d
break;
elseif g'*d*(a_hi - a_lo) >= 0
a_hi = a_lo;
a_lo = a;
else
a_lo = a;
end
end
end
end
% main function
f = @(x) 100*(x(1)-x(1)^2)^2 + (x(2)-1)^2;
x = [-1;1];
d = [1;-2];
a = strong_wolfe(f, x, d)
```
阅读全文