2016年大连理工大学优化方法上机大作业 学院: 专业: 班级: 学号: 姓名:
上机大作业1:
1.最速下降法:
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 = steepest(x0,eps) gk = grad(x0); res = norm(gk); k = 0;
while res > eps && k<=1000 dk = -gk;
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; gk = grad(xk); 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=steepest(x0,eps)
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);