2016年大连理工大学优化方法上机大作业
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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);
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;
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)