优化方法作业第二版..doc

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

优化方法上机大作业

院系:化工与环境生命学部

姓名:李翔宇

学号:31607007

指导教师:肖现涛

第一题:

1.最速下降法

源程序如下:

function x_star = ZSXJ(x0,eps)

gk = grad(x0);

res = norm(gk);

k = 0;

while res > eps && k<=10000

dk = -gk;

ak =1; f0 = fun(x0);

f1 = fun(x0+ak*dk);

slope = dot(gk,dk);

while f1 > f0 + 0.0001*ak*slope

ak = ak/2;

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

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

运行结果:

>> x0=[0,0]';

>> esp=1e-4;

>> xk=ZSXJ(x0,eps)

--The 1-th iter, the residual is 13.372079 --The 2-th iter, the residual is 12.079876 --The 3-th iter, the residual is 11.054105 ……………………………………………

--The 9144-th iter, the residual is 0.000105 --The 9145-th iter, the residual is 0.000102 --The 9146-th iter, the residual is 0.000100 xk =

0.9999

0.9998

MATLAB截屏:

2.牛顿法

源程序如下:

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

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

运行结果:

>> x0=[0,0]';eps=1e-4;

>> xk=NEWTON(x0,eps)

--The 1-th iter, the residual is 447.213595 --The 2-th iter, the residual is 0.000000 xk =

1.0000

1.0000

MATALB截屏;

3.BFGS方法

源程序如下:

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/2;

xk = x0 + ak*dk;

f1 = fun(xk);

end

k = k+1;

fa0=xk-x0;

x0 = xk;

g0=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

function f=fun(x)

相关文档
最新文档