ak =1; f0 = fun(x0); f1 = fun(x0+ak*dk); slope = dot(gk,dk);
while f1 > f0 + 0.1*ak*slope ak = ak/4; xk = x0 + ak*dk; f1 = fun(xk); end k = k+1; fa0=xk-x0; x0 = xk; go=gk; gk = grad(xk); y0=gk-g0;
Hk=((eye(2)-fa0*(y0)')/((fa0)'*(y0)))*((eye(2)-(y0)*(fa0)')/((fa0)'*(y0)))+(fa0*(fa0)')/((fa0)'*(y0)); res = norm(gk);
fprintf('--The %d-th iter, the residual is %f\\n',k,res); end
x_star = xk; End
>> clear >> x0=[0,0]'; >> eps=1e-4;
>> x=bfgs(x0,eps)
4.共轭梯度法:
function f = fun(x)
f = (1-x(1))^2 + 100*(x(2)-x(1)^2)^2; end
function g = grad(x) g = zeros(2,1);
g(1)=2*(x(1)-1)+400*x(1)*(x(1)^2-x(2)); g(2) = 200*(x(2)-x(1)^2); end
function x_star =CG(x0,eps) gk = grad(x0); res = norm(gk); k = 0; dk = -gk;
while res > eps && k<=1000
ak =1; f0 = fun(x0); f1 = fun(x0+ak*dk); slope = dot(gk,dk);
while f1 > f0 + 0.1*ak*slope ak = ak/4; xk = x0 + ak*dk; f1 = fun(xk); end k = k+1; x0 = xk; g0=gk; gk = grad(xk); res = norm(gk); p=(gk/g0)^2; dk1=dk; dk=-gk+p*dk1;
fprintf('--The %d-th iter, the residual is %f\\n',k,res); end
x_star = xk; end
>> clear >> x0=[0,0]'; >> eps=1e-4; >> x=CG(x0,eps)
相关推荐: