2.牛顿法:
function f = fun(x)
f = (1-x(1))^2 + 100*(x(2)-x(1)^2)^2; end
function g = grad2(x) g = zeros(2,2);
g(1,1)=2+400*(3*x(1)^2-x(2)); g(1,2)=-400*x(1); g(2,1)=-400*x(1); g(2,2)=200; 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 = newton(x0,eps) gk = grad(x0);
bk = [grad2(x0)]^(-1); res = norm(gk); k = 0;
while res > eps && k<=1000 dk=-bk*gk; xk=x0+dk; k = k+1; x0 = xk; gk = grad(xk); bk = [grad2(xk)]^(-1); 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;
>> x1=newton(x0,eps)
--The 1-th iter, the residual is 447.213595 --The 2-th iter, the residual is 0.000000 x1 =
1.0000 1.0000
3.BFGS法:
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 = bfgs(x0,eps) g0 = grad(x0); gk=g0;
res = norm(gk); Hk=eye(2); k = 0;
while res > eps && k<=1000 dk = -Hk*gk;
相关推荐: