强wolfe准则的FR共轭梯度算法matlab
时间: 2023-11-07 13:26:11 浏览: 84
以下是一个使用强 Wolfe准则的共轭梯度算法的matlab代码示例:
function [x, fval, k] = cg_wolfe(f, x0, max_iter, tol)
% CG_WOLFE Strong Wolfe's rule Conjugate Gradient
%
% [x, fval, k] = CG_WOLFE(f, x0, max_iter, tol)
%
% Inputs:
% f function handle to objective function
% x0 initial point
% max_iter maximum number of iterations
% tol tolerance for stopping criterion
%
% Outputs:
% x solution point
% fval function value at solution point
% k number of iterations
% Initialize
x = x0;
g = grad(f, x);
d = -g;
k = 0;
while norm(g) > tol && k < max_iter
% Compute step size
[alpha,~,~,~,~] = zoom(f, x, d);
% Update x
x = x + alpha*d;
% Compute gradient
g_prev = g;
g = grad(f, x);
% Compute beta
beta = (g'*g - g'*g_prev)/(g_prev'*g_prev);
% Update direction
d = -g + beta*d;
k = k + 1;
end
% Compute function value at solution point
fval = f(x);
end
function [g] = grad(f, x)
% GRAD Compute gradient of function f at point x
h = 1e-8;
g = zeros(size(x));
for i = 1:length(x)
ei = zeros(size(x));
ei(i) = 1;
g(i) = (f(x + h*ei) - f(x - h*ei))/(2*h);
end
end
function [alpha, fval, k, ls_evals, ls_iters] = zoom(f, x, d)
% ZOOM Zoom algorithm for strong Wolfe's rule
%
% [alpha, fval, k, ls_evals, ls_iters] = ZOOM(f, x, d)
%
% Inputs:
% f function handle to objective function
% x starting point
% d search direction
%
% Outputs:
% alpha step size
% fval function value at x + alpha*d
% k number of iterations
% ls_evals number of function evaluations
% ls_iters number of iterations (linesearch)
% Set parameters
c1 = 1e-4;
c2 = 0.9;
max_iter = 100;
a_lo = 0;
a_hi = Inf;
ls_evals = 0;
ls_iters = 0;
% Compute function and gradient at 0
f0 = f(x);
g0 = grad(f, x);
% Initialize alpha
alpha = 1;
% Iterate until stopping criterion is met
for k = 1:max_iter
% Compute function and gradient at alpha
f_alpha = f(x + alpha*d);
g_alpha = grad(f, x + alpha*d);
ls_evals = ls_evals + 1;
% Check Wolfe conditions
if f_alpha > f0 + c1*alpha*g0'*d || (f_alpha >= f(x + a_lo*d) && k > 1)
a_hi = alpha;
alpha = (a_lo + a_hi)/2;
else
if abs(g_alpha'*d) <= -c2*g0'*d
break;
elseif g_alpha'*d >= 0
a_hi = alpha;
alpha = (a_lo + a_hi)/2;
else
a_lo = alpha;
alpha = 2*alpha;
end
end
ls_iters = ls_iters + 1;
end
fval = f_alpha;
end
阅读全文