文档库 最新最全的文档下载
当前位置:文档库 › 优化方法MATLAB编程-大连理工大学

优化方法MATLAB编程-大连理工大学

优化方法MATLAB编程-大连理工大学
优化方法MATLAB编程-大连理工大学

优化方法上机大作业

学院:

姓名:

学号:

指导老师:肖现涛

第一题

源程序如下:

function zy_x = di1ti(x)

%di1ti是用来求解优化作业第一题的函数。x0=x; yimuxulong=0.000001;

g0=g(x0);s0=-g0;

A=2*ones(100,100);

k=0;

while k<100

lanmed=-(g0)'*s0/(s0'*A*s0);

x=x0+lanmed*s0;

g=g(x);

k=k+1;

if norm(g)

zy_x=x;

fprintf('After %d iterations,obtain the optimal solution.\n \n The optimal solution is \n %f.\n\nThe optimal "x" is "ans".',k,f(x) )

break;

end

miu=norm(g)^2/norm(g0)^2;

s=-g+miu*s0;

g0=g; s0=s;x0=x;

end

function f=f(x)

f=(x'*ones(100,1))^2-x'*ones(100,1);

function g=g(x)

g=(2*x'*ones(100,1))*ones(100,1)-ones(100,1);

代入x0,运行结果如下:

>> x=zeros(100,1);

>> di1ti(x)

After 1 iterations,obtain the optimal solution.

The optimal solution is

-0.250000.

The optimal "x" is "ans".

ans =0.005*ones(100,1).

第二题

1.最速下降法。

源程序如下:

function zy_x=di2titidu(x)

%该函数用来解大作业第二题。x0=x; yimuxulong=1e-5; k=0; g0=g(x0); s0=-g0;

while k>=0

if norm(g0)

break;

else

lanmed=10;c=0.1;i=0;

while i>=0&i<100

x=x0+lanmed*s0;

if f(x)>(f(x0)+c*lanmed*g0'*s0)

lanmed=lanmed/2;

i=i+1;

else

break;

end

end

x=x0+lanmed*s0;

x0=x;

g0=g(x);

s0=-g0;

k=k+1;

end

end

zy_x=x;

zyj=f(x);

fprintf('after %d iterations,obtain the optimal solution.\n\nThe optimal solution is %f.\n\n The optimal "x" is "ans".\n',k,zyj);

function f=f(x)

x1=[1 0 0 0]*x;

x2=[0 1 0 0]*x;

x3=[0 0 1 0]*x;

x4=[0 0 0 1]*x;

f=(x1-1)^2+(x3-1)^2+100*(x2-x1^2)^2+100*(x4-x3^2)^2;

function g=g(x)

x1=[1 0 0 0]*x;

x2=[0 1 0 0]*x;

x3=[0 0 1 0]*x;

x4=[0 0 0 1]*x;

g=[2*(x1-1)-400*x1*(x2-x1^2);200*(x2-x1^2);2*(x3-1)-400*x3*(x4-x3^2);200*( x4-x3^2)];

>> x=[-1.2 1 -1.2 1]';

>> di2titidu(x)

after 5945 iterations,obtain the optimal solution.

The optimal solution is 0.000000.

The optimal "x" is "ans".

ans =

1.0000

1.0000

1.0000

1.0000

2.牛顿法

源程序如下:

function zy_x=di2tinewton(x)

%该函数用来解大作业第二题。

x0=x; yimuxulong=1e-5; k=0;

g0=g(x0); h0=h(x0);s0=-inv(h0)*g0;

while k>=0&k<1000

if norm(g0)

break;

else

x=x0+s0;

x0=x;

g0=g(x);

h0=h(x);

s0=-inv(h0)*g0;

k=k+1;

end

end

zy_x=x;

zyj=f(x);

fprintf('after %d iterations,obtain the optimal solution.\n\nThe optimal solution is %f.\n\n The optimal "x" is "ans".\n',k,zyj);

function f=f(x)

x1=[1 0 0 0]*x;

x2=[0 1 0 0]*x;

x3=[0 0 1 0]*x;

x4=[0 0 0 1]*x;

f=(x1-1)^2+(x3-1)^2+100*(x2-x1^2)^2+100*(x4-x3^2)^2;

function g=g(x)

x1=[1 0 0 0]*x;

x2=[0 1 0 0]*x;

x3=[0 0 1 0]*x;

x4=[0 0 0 1]*x;

g=[2*(x1-1)-400*x1*(x2-x1^2);200*(x2-x1^2);2*(x3-1)-400*x3*(x4-x3^2);200*( x4-x3^2)];

function h=h(x)

x1=[1 0 0 0]*x;

x2=[0 1 0 0]*x;

x3=[0 0 1 0]*x;

x4=[0 0 0 1]*x;

h=[2+1200*x1^2-400*x2 -400*x1 0 0;-400*x1 200 0 0;0 0

2+1200*x3^2-400*x4 -400*x3;0 0 -400*x3 200];

代入初始值,运行结果如下:

>> x=[-1.2 1 -1.2 1]';

>> di2tinewton(x)

after 6 iterations,obtain the optimal solution.

The optimal solution is 0.000000.

The optimal "x" is "ans".

ans =

1.0000

1.0000

1.0000

1.0000

可以看出,用Newton法经过6次迭代就能求出最优解。

3.BFGS法

源程序如下:

function zy_x=di2tiBFGS(x)

%该函数用来解大作业第二题。

x0=x; yimuxulong=1e-5; k=0;

g0=g(x0); H0=eye(4);s0=-H0*g0;

while k>=0&k<100

if norm(g0)

break;

else

lanmed=10;c=0.1;i=0;

while i>=0&i<100

x=x0+lanmed*s0;

if f(x)>(f(x0)+c*lanmed*g0'*s0)

lanmed=lanmed/2;

i=i+1;

else

break;

end

end

x=x0+lanmed*s0;

dete_x=x-x0;

dete_g=g(x)-g0;

miu=1+dete_g'*H0*dete_g/(dete_x'*dete_g);

H=H0+(miu*dete_x*dete_x'-H0*dete_g*dete_x'-dete_x*dete_g'*H0)/(dete_x'*det e_g);

s=-H*g(x);

x0=x;

s0=s;

H0=H;

g0=g(x);

k=k+1;

end

end

zy_x=x;

zyj=f(x);

fprintf('after %d iterations,obtain the optimal solution.\n\nThe optimal

solution is %f.\n\n The optimal "x" is "ans".\n',k,zyj);

function f=f(x)

x1=[1 0 0 0]*x;

x2=[0 1 0 0]*x;

x3=[0 0 1 0]*x;

x4=[0 0 0 1]*x;

f=(x1-1)^2+(x3-1)^2+100*(x2-x1^2)^2+100*(x4-x3^2)^2;

function g=g(x)

x1=[1 0 0 0]*x;

x2=[0 1 0 0]*x;

x3=[0 0 1 0]*x;

x4=[0 0 0 1]*x;

g=[2*(x1-1)-400*x1*(x2-x1^2);200*(x2-x1^2);2*(x3-1)-400*x3*(x4-x3^2);200*( x4-x3^2)];

代入初始值,计算结果如下:

>> x=[-1.2 1 -1.2 1]';

>> di2tiBFGS(x)

after 53 iterations,obtain the optimal solution.

The optimal solution is 0.000000.

The optimal "x" is "ans".

ans =

1.0000

1.0000

1.0000

1.0000

第三题

1.惩罚函数法

源程序如下:

function zy_x=di3ti(x)

%该函数用来解大作业第三题。

x0=x; M=100; c=4; m=1;

while m>0

g0=g(x0,M); yimuxulong=1e-5;k=0;s0=-inv(H(x0,M))*g0;

while k>=0

if norm(g0)

break;

else

x=x0+s0; %牛顿法;

x0=x;

g0=g(x,M);

s0=-inv(H(x0,M))*g0;

k=k+1;

end

end

if max([abs(h(x)),g1(x),g2(x),g3(x)])<0.5

break;

else

M=M*c;

m=m+1;

end

end

zy_x=x;

zyj=f(x);

fprintf('after %d iterations,obtain the optimal solution.\n\nThe optimal solution is %f.\n\n The optimal "x" is "ans".\n',m,zyj);

function F=F(x,M)

x1=[1 0]*x;

x2=[0 1]*x;

F=4*x1-x2^2-12+M*(h^2+g1^2+g2^2+g3^2);

function g=g(x,M)

x1=[1 0]*x;

x2=[0 1]*x;

g=[4+M*(-4*(25-x1^2-x2^2)*x1+2*(10*x1-x1^2+10*x2-x2^2-34)*(10-2*x1)+2* x1);-2*x2+M*(-4*(25-x1^2-x2^2)*x2+2*(10*x1-x1^2+10*x2-x2^2-34)*(10-2*x2 )+2*x2)];

function H=H(x,M)

x1=[1 0]*x;

x2=[0 1]*x;

H=[M*(24*x1^2-120*x1+8*x2^2-40*x2+238),M*(16*x1*x2-40*x1-40*x2+200); M*(16*x1*x2-40*x1-40*x2+200),-2+M*(24*x2^2-120*x2+8*x1^2-40*x1+238)]; function f=f(x)

x1=[1 0]*x;

x2=[0 1]*x;

f=4*x1-x2^2-12;

function h=h(x)

x1=[1 0]*x;

x2=[0 1]*x;

h=25-x1^2-x2^2;

function g1=g1(x)

x1=[1 0]*x;

x2=[0 1]*x;

g=10*x1-x1^2+10*x2-x2^2-34;

if g<0

g1=g;

else

g1=0;

end

function g2=g2(x)

x1=[1 0]*x;

x2=[0 1]*x;

if x1>=0

g2=0;

else

g2=x1;

end

function g3=g3(x)

x1=[1 0]*x;

x2=[0 1]*x;

if x2>=0

g3=0;

else

g3=x2;

end

代入任意初始值,运算结果如下。

相关文档