?? banana.asv
字號:
%以下程序段是畫香蕉函數(shù)圖形。
xx = [-2:0.125:2]'; yy = [-1:0.125:3]'; [x,y]=meshgrid(xx',yy') ;
meshd = 100.*(y-x.*x).^2 + (1-x).^2; conts = exp(3:20);
xlabel('x1'),ylabel('x2'),title('Minimization of the Banana function')
contour(xx,yy,meshd,conts), hold on
plot(-1.9,2,'ro'),text(-1.9,2,'Start Point')
plot(1,1,'ro'),text(1,1,'Solution')
%優(yōu)化程序段開始。
x0=[-1.9,2]; %賦初值。
l=1;
while l %while 語句是可以重復(fù)運行下面的程序段,直至l=0退出循環(huán)。
clc %清除命令窗口的全體內(nèi)容 。
%以下程序段是在命令窗口顯示相應(yīng)的文字內(nèi)容 。
disp(' ')
disp(' Choose any of the following methods to minimize the … banana function')
disp('')
disp(' UNCONSTRAINED: 1) BFG direction ')
disp(' 2) DFP direction')
disp(' 3) Steepest Descent direction')
disp(' 4) Simplex Search')
disp(' 0) Quit')
method=input('Select method : '); % input 從鍵盤輸入控制變量method數(shù)據(jù)。
switch method %Switch體開始。
case 0 %當method=0,終止程序。
hold off
disp('End of demo')
break %break指令:中止程序。
case 1 %當method=1,采用BFGS法。
clf,hold on %每一個case中重新畫等值線圖,下面的程序段是重新畫圖。
xlabel('x1'),ylabel('x2'),
title('Minimization of the Banana function')
contour(xx,yy,meshd,conts)
plot(-1.9,2,'ro'), text(-1.9,2,'Start Point')
plot(1,1,'ro'), text(1,1,'Solution')
% 這里是學(xué)習(xí)的重點: OPTIONS是控制fminunc和fminsearch指令的重要參數(shù),
%用optimset('屬性','屬性值',…)指令改變設(shè)置,可以容易地控制算法。
OPTIONS=optimset('LargeScale','off');
%fminunc默認的大規(guī)模算法是“信賴域方法”,這是一種有效的算法;
%將LargeScale的屬性設(shè)置為off時,fminunc的默認中等規(guī)模的算法就是BFGS方法。
OPTIONS = optimset(OPTIONS,'gradobj','on'); %使用解析梯度。
%定義梯度函數(shù)和畫圖函數(shù)banplot6_4。
GRAD=inline('[100*(4*x(1)^3-4*x(1)*x(2))+2*x(1)-2;… 100*(2*x(2)-2*x(1)^2); banplot6_4(x)]');
f=inline('100*(x(2)-x(1)^2)^2+(1-x(1))^2'); %定義目標函數(shù)。
disp('[x,fval,exitflag,output] = fminunc({f,GRAD},x0,OPTIONS);');
%(調(diào)用fminunc指令,輸出x,fval分別為最優(yōu)點和最優(yōu)函數(shù)值,exitflag和output
% 提供算法的一些信息,讀者可在程序結(jié)束后,鍵入output或exitflag查看這些信息)
[x,fval,exitflag,output] = fminunc({f,GRAD},x0,OPTIONS);
hold off
disp(' ')
disp('Strike any key for menu')
pause
case 2 %當method=2,采用DFP法。
clf, xlabel('x1'),ylabel('x2'),
title('Minimization of the Banana function')
contour(xx,yy,meshd,conts), hold on
plot(-1.9,2,'ro'), text(-1.9,2,'Start Point')
plot(1,1,'ro'), text(1,1,'Solution')
OPTIONS=optimset('LargeScale','off');
OPTIONS = optimset(OPTIONS,'gradobj','on');
OPTIONS=optimset(OPTIONS,'HessUpdate','dfp');
% 將HessUpdate屬性設(shè)置為dfp就使fminunc指令采用DFP法。
GRAD=inline('[100*(4*x(1)^3-4*x(1)*x(2))+2*x(1)-2;…100*(2*x(2)-2*x(1)^2); banplot6_4(x)]');
f=inline('100*(x(2)-x(1)^2)^2+(1-x(1))^2');
disp('[x,fval,exitflag,output] = fminunc({f,GRAD},x0,OPTIONS);');
[x,fval,exitflag,output] = fminunc({f,GRAD},x0,OPTIONS);
hold off
disp(' ')
disp('Strike any key for menu')
pause
case 3 %當method=3,采用最速下降法。
clf, xlabel('x1'),ylabel('x2'),
title('Minimization of the Banana function')
contour(xx,yy,meshd,conts)
hold on
plot(-1.9,2,'ro'), text(-1.9,2,'Start Point')
plot(1,1,'ro'), text(1,1,'Solution')
OPTIONS=optimset('LargeScale','off');
OPTIONS = optimset(OPTIONS,'gradobj','on');
OPTIONS=optimset(OPTIONS,'HessUpdate','steepdesc');
%將HessUpdate屬性設(shè)置為steepdesc就使fminunc指令采用最速下降法。
GRAD=inline('[100*(4*x(1)^3-4*x(1)*x(2))+2*x(1)-2;…100*(2*x(2)-2*x(1)^2); banplot6_4(x)]');
f=inline('100*(x(2)-x(1)^2)^2+(1-x(1))^2');
disp('[x,fval,exitflag,output] = fminunc({f,GRAD},x0,OPTIONS);');
[x,fval,exitflag,output] = fminunc({f,GRAD},x0,OPTIONS);
hold off
disp(' ')
disp('Strike any key for menu')
pause
case 4 %當method=4,采用單純形方法。
clf,hold on, xlabel('x1'),ylabel('x2'),
title('Minimization of the Banana function')
contour(xx,yy,meshd,conts),
plot(-1.9,2,'ro'), text(-1.9,2,'Start Point')
plot(1,1,'ro'), text(1,1,'Solution')
OPTIONS=optimset('LargeScale','off');
OPTIONS = optimset(OPTIONS,'gradobj','off');
%該方法不使用導(dǎo)數(shù),所以要設(shè)置gradobj屬性為off。
f=inline('[100*(x(2)-x(1)^2)^2+(1-x(1))^2; banplot6_4(x)]');
%如果要畫迭代過程的中間圖,就要編制一個畫圖程序 banplot6_4,
% 套用本程序的格式定義目標函數(shù)。
disp('[x,fval,exitflag,output] = fminsearch(f,x0,OPTIONS);');
[x,fval,exitflag,output] = fminsearch(f,x0,OPTIONS);
%fminsearch 是多變量函數(shù)尋優(yōu)的單純形法指令,用法和fminunc是類似的。
hold off
disp(' ')
disp('Strike any key for menu')
pause
end
end
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -