优化方法 上机大作业
优化方法 上机大作业
机械工程与材料能源学部
能源与动力学院
能源与环境工程
联系方式:15326178310
能源与动力工程学院 2012级
优化方法 上机大作业
x0=[0;1]T;%初始值 s0=[-1;1]T;%初始搜索方向
c1=0.1;c2=0.5;a=0;b=inf;d=1;n=0; x1=x0+d*s0;
g0=[(x0(2)-x0(1)^2)*x0(1)-2*(1-x0(1));(x0(2)-x0(1)^2)]; g1=[(x1(2)-x1(1)^2)*x1(1)-2*(1-x1(1));(x1(2)-x1(1)^2)]; f1=(x1(2)-x1(1)^2)^2+(1-x1(1))^2; f0=(x0(2)-x0(1)^2)^2+(1-x0(1))^2;
while((f0-f1<-c1*d*g0'*s0)||(g1'*s0 g0=[(x0(2)-x0(1)^2)*x0(1)-2*(1-x0(1));(x0(2)-x0(1)^2)]; g1=[(x1(2)-x1(1)^2)*x1(1)-2*(1-x1(1));(x1(2)-x1(1)^2)]; f1=(x1(2)-x1(1)^2)^2+(1-x1(1))^2; f0=(x0(2)-x0(1)^2)^2+(1-x0(1))^2; elseif (((g1')*s0)<(c2*(g0')*s0)) a=d; if(2*d<=(d+b)/2) d=2*d; else d=(d+b)/2; end x1=x0+d*s0; g0=[(x0(2)-x0(1)^2)*x0(1)-2*(1-x0(1));(x0(2)-x0(1)^2)]; g1=[(x1(2)-x1(1)^2)*x1(1)-2*(1-x1(1));(x1(2)-x1(1)^2)]; f1=(x1(2)-x1(1)^2)^2+(1-x1(1))^2; f0=(x0(2)-x0(1)^2)^2+(1-x0(1))^2; end end x1 能源与动力工程学院 2012级 优化方法 上机大作业 f1=(x1(2)-x1(1)^2)^2+(1-x1(1))^2 x1 = -0.0000 1.0000 d = 1.1102e-016 f1 = 2 function f = fun( x ) %UNTITLED3 Summary of this function goes here % Detailed explanation goes here f=x(1)^2-2*x(1)*x(2)+2*x(2)^2+x(3)^2+x(4)^2-x(2)*x(3)+2*x(1)+3*x(2)-x(3); End function g = fun( x ) %UNTITLED4 Summary of this function goes here % Detailed explanation goes here g=[2 -2 0 0;-2 4 -1 0;0 -1 2 0;0 0 0 2]*x+[2;3;-1;0]; 能源与动力工程学院 2012级 优化方法 上机大作业 end x0=[0;0;0;0]; %初始值 eps=1.0e-4; %精度 g0=gfun(x0); s0=-g0; n=0; syms d1; while norm(g0)>eps if n<3 g=gfun(x0+d1*s0); d= double(solve(s0'*g)); x1=x0+d*s0; g1=gfun(x1); if norm(g1) n=n+1; x0=x1; break else s0=-g1+(norm(g1)^2/norm(g0)^2)*s0; x0=x1; g0=g1; end elseif n=3 x0=x1; g0=gfun(x0); s0=-g0; n=0; end n=n+1; end x0 n fun(x0) x0 = -4 -3 -1 0 能源与动力工程学院 2012级 优化方法 上机大作业 n = 3 ans = -8 function f= fun3_1(x ) %FUN3 Summary of this function goes here % Detailed explanation goes here f=x(1)+2*x(2)^2+exp(x(1)^2+x(2)^2); end function g= gfun3_1(x) %GFUN3_1 Summary of this function goes here % Detailed explanation goes here g=[1+2*x(1)*exp(x(1)^2+x(2)^2);4*x(2)+2*x(2)*exp(x(1)^2+x(2)^2)]; end (1)最速下降法 x0=[0;1];%初始值 eps=1.0e-5;%精度 n=0; g0=gfun3_1(x0); syms d1; while norm(g0)>=eps 能源与动力工程学院 2012级
优化方法 上机大作业



