Last active
December 12, 2015 00:08
-
-
Save acoada/4680968 to your computer and use it in GitHub Desktop.
Optimization methods 最优化方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%梯度下降 tiduxiajing.m | |
function min=tiduxiajiang() | |
syms x1 x2 x3 x4; | |
f=(x1+10*x2)^2+5*(x3-x4)^2+(x2-2*x3)^4+10*(x1-x4)^4; | |
e=0.05; | |
k=0; | |
x0=[1,2,3,4]; | |
x=x0; | |
tol=1; | |
df=jacobian(f) | |
while((tol>e)&(k<100)) | |
df=-jacobian(f); | |
dk=subs(df,[x1,x2,x3,x4],x); | |
tol=norm(dk); | |
dk=subs(dk); | |
x=hjfg(x,dk) ; | |
k=k+1; | |
end | |
min=subs(f,[x1,x2,x3,x4],x); | |
disp('采用最速梯度法求解多元函数:Min {f=(x1+10*x2)^2+5*(x3-x4)^2+(x2-2*x3)^4+10*(x1-x4)^4}的最优解:') | |
disp('选取初始点为:'),disp(x0) | |
disp('迭代次数:'),disp(k) | |
disp('最小点是:'),disp(x) | |
disp('此时的最小值为:'),disp(min) | |
%黄金分割 huangjinfenge.m | |
function m=hjfg(x,dk) | |
syms x1 x2 x3 x4; | |
f=(x1+10*x2)^2+5*(x3-x4)^2+(x2-2*x3)^4+10*(x1-x4)^4; | |
e=0.01; | |
k=0; | |
a=x; | |
b=x+dk; | |
tol=1; | |
while((tol>e)&(k<100)) | |
q1=a+0.328*(b-a); | |
q2=a+0.618*(b-a); | |
f1=subs(f,[x1,x2,x3,x4],q1); | |
f2=subs(f,[x1,x2,x3,x4],q2); | |
if f1>f2 | |
a=q1; | |
else | |
b=q2; | |
end | |
tol=norm(b-a); | |
end | |
m=(a+b)/2; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
法 | |
function min=newton() | |
syms x1 x2 x3 x4; | |
f=(x1+10*x2)^2+5*(x3-x4)^2+(x2-2*x3)^4+10*(x1-x4)^4; | |
e=10e-4; | |
k=0; | |
x0=[1,2,3,4]; | |
x=x0; | |
df=jacobian(f); | |
dd=subs(df,[x1,x2,x3,x4],x); | |
tol=norm(dd); | |
while((tol>e)&(k<100)) | |
dm1=[diff(df(1),x1),diff(df(1),x2),diff(df(1),x3),diff(df(1),x4)]'; | |
dm2=[diff(df(2),x1),diff(df(2),x2),diff(df(2),x3),diff(df(2),x4)]'; | |
dm3=[diff(df(3),x1),diff(df(3),x2),diff(df(3),x3),diff(df(3),x4)]'; | |
dm4=[diff(df(4),x1),diff(df(4),x2),diff(df(4),x3),diff(df(4),x4)]'; | |
dm=[dm1,dm2,dm3,dm4]'; | |
dm=subs(dm,[x1,x2,x3,x4],x); | |
dk=-dm\dd'; | |
x=huangjinfenge(x,dk'); | |
dd=subs(df,[x1,x2,x3,x4],x); | |
tol=norm(dd); | |
k=k+1; | |
end | |
min=subs(f,[x1,x2,x3,x4],x); | |
disp('采用牛顿法求解多元函数:Min {f=(x1+10*x2)^2+5*(x3-x4)^2+(x2-2*x3)^4+10*(x1-x4)^4}的最优解:') | |
disp('选取初始点为:'),disp(x0) | |
disp('迭代次数:'),disp(k) | |
disp('最小点为:'),disp(x) | |
disp('此时的最小值为:'),disp(min) | |
%黄金分割 | |
f=(x1+10*x2)^2+5*(x3-x4)^2+(x2-2*x3)^4+10*(x1-x4)^4; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%内点法 | |
function min=neidianfa() | |
e=0.005; | |
k=0; | |
t=0; | |
m=1; | |
u=0.5; | |
syms x1 x2 ; | |
f=x1^3+x2^2+m*(1/(x1+x2-1)); | |
df=jacobian(f); | |
x0=[1,1]; | |
x=x0; | |
dd=subs(df,[x1,x2],x); | |
while ((norm(dd)>e)&(k<100)) | |
dm=jacobian(df); | |
dm=subs(dm,[x1,x2],x); | |
dk=-dm\dd'; | |
x=nyiwei(x,dk,m); | |
dd=subs(df,[x1,x2],x); | |
k=k+1; | |
if((m*(1/(x(1)+x(2)-1))>e)&(t<100)) | |
t=t+1; | |
m=u*m; | |
f=x1^3+x2^2+m*(1/(x1+x2-1)); | |
df=[diff(f,x1) diff(f,x2) ]; | |
dd=subs(df,[x1,x2],x); | |
else break | |
end | |
end | |
min=x(1)^3+x(2)^2; | |
%内点法一维搜素 | |
function min=nyiwei(x,dk,m) | |
syms x1 x2 ; | |
f=x1^3+x2^2+m*(1/(x1+x2-1)); | |
a=x; | |
b=x+dk'; | |
e=0.01; | |
while(norm(b-a)>e) | |
q1=a+0.328*(b-a); | |
q2=a+0.618*(b-a); | |
f1=subs(f,[x1,x2],q1); | |
f2=subs(f,[x1,x2],q2); | |
if f1>f2 | |
a=q1; | |
else | |
b=q2; | |
end | |
f=x1^3+x2^2+m*(1/(x1+x2-1)); | |
end | |
min=(a+b)/2; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%外点法 | |
function min=waidianfa() | |
e=0.005; | |
k=0; | |
t=0; | |
m=0.5; | |
r=2; | |
syms x1 x2 ; | |
f=x1^3+x2^2+m*(x1+x2-1)^2; | |
df=[diff(f,x1) diff(f,x2) ]; | |
x0=[1,1]; | |
x=x0; | |
dd=subs(df,[x1,x2],x); | |
while((norm(dd)>e)&(k<100)) | |
dm1=[diff(df(1),x1) ,diff(df(1),x2)]'; | |
dm2=[diff(df(2),x1) ,diff(df(2),x2)]'; | |
dm=[dm1 ,dm2]'; | |
dm=subs(dm,[x1,x2],x); | |
dk=-dm\dd'; | |
dk=subs(dk); | |
x=wyiwei(x,dk,m); | |
k=k+1; | |
if (((x(1)+x(2)-1)^2>e)&(t<100)) | |
t=t+1; | |
m=r*m; | |
f=x1^3+x2^2+m*(x1+x2-1)^2; | |
df=[diff(f,x1) diff(f,x2) ]; | |
dd=subs(df,[x1,x2],x); | |
else break | |
end | |
end | |
min=x(1)^3+x(2)^2; | |
disp('采用外点法求解函数:Min{f=x1^3+x2^2}在约束条件{x1+x2-1>=0}的条件下的最优解:') | |
disp('选取初始点为:'),disp(x0) | |
disp('迭代次数:'),disp(k) | |
disp('最小点:'),disp(x) | |
disp('此时的最小值为'),disp(min) | |
%外点法一维搜索 | |
function min=wyiwei(x,dk,m) | |
syms x1 x2 ; | |
f=x1^3+x2^2+m*(x1+x2-1)^2; | |
a=x; | |
b=x+dk'; | |
e=0.01; | |
while(norm(b-a)>e) | |
q1=a+0.328*(b-a); | |
q2=a+0.618*(b-a); | |
f1=subs(f,[x1,x2],q1); | |
f2=subs(f,[x1,x2],q2); | |
if(f1>f2) | |
a=q1; | |
else | |
b=q2; | |
end | |
f=x1^3+x2^2+m*(x1+x2-1)^2; | |
end | |
min=(a+b)/2; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment