《数学实验与Matlab》程序(7)

2019-01-12 13:57

点。

[zi,dz1,dz2]=zxy6_3f(xi,yi); %计算函数

值和梯度方向。

v=zi;

contour(X1,X2,Z,[v v],'-'), %在点所在的高度画一条等

高线。

axis([xmin xmax ymin ymax]), x=[x,xi];y=[y,yi];

H_line2=plot(x,y); %画

已走的路径连线。

set(H_line2,'color','red','linewidth',2); %设置颜色和线宽。 xt=xi-dz1;yt=yi-dz2;

H_line=plot([xi xt],[yi yt],'k:','linewidth',1); %画最速下降方向

路径。

end %若按左键button=1,继续循环。若按右键,button~=1,循

环终止 。 】

zxy6_3f.m(模拟山谷的二次函数程序) 【 function [f,df1,df2]=zxy6_4f(x1,x2)

f=8*x1.*x1+9*x2.*x2-10*x1.*x2-12*x1-6*x2; %计算函数值。 if nargout > 1

df1=2*8*x1-10*x2-12*ones(size(x1)); %计算梯度向量。

df2=2*9*x2-10*x1-6*ones(size(x2)); end

6.3 .实验与观察(Ⅱ):Matlab优化工具箱简介

6.3.1多元函数无约束优化指令fminunc和fminsearch

1. 观察:运行香蕉函数的优化程序bandemo.m

31

2. 使用fminunc和fminsearch指令

◆ 观察:用inline生成函数。

【 f=inline('100*(x(2)-x(1)^2)^2+(1-x(1))^2'),

x=[2,2],y=f(x), %代入一个点计算看看效果 。 】

3. bandemo.m的简化和剖析

zxy6_4.m

【 clf; clear

%以下程序段是画香蕉函数图形。

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')

%优化程序段开始。

x0=[-1.9,2]; %赋初值。 l=1;

while l %while 语句是可以重复运行下面的程序段,直至l=0退出

循环。

clc %清除命令窗口的全体内容 。

%以下程序段是在命令窗口显示相应的文字内容 。

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)

32

Simplex Search')

disp(' 0) Quit')

method=input('Select method : '); % input 从键盘输入控制变量

method数据。

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')

% 这里是学习的重点: OPTIONS是控制fminunc和fminsearch指令的重要参数,

%用optimset('属性','属性值',?)指令改变设置,可以容易地控制算法。

OPTIONS=optimset('LargeScale','off');

%fminunc默认的大规模算法是“信赖域方法”,这是一种有效的算

法;

%将LargeScale的属性设置为off时,fminunc的默认中等规模的算法就是BFGS方法。

OPTIONS = optimset(OPTIONS,'gradobj','on'); %使用解析梯度。

%定义梯度函数和画图函数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'); %定义目标函数。

disp('[x,fval,exitflag,output] fminunc({f,GRAD},x0,OPTIONS);');

33

=

%(调用fminunc指令,输出x,fval分别为最优点和最优函数值,exitflag和output

% 提供算法的一些信息,读者可在程序结束后,键入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属性设置为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')

34

=

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属性设置为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'); %该方法不使用导数,所以要设置gradobj属性为off。

=

f=inline('[100*(x(2)-x(1)^2)^2+(1-x(1))^2; banplot6_4(x)]');

%如果要画迭代过程的中间图,就要编制一个画图程序 banplot6_4,

% 套用本程序的格式定义目标函数。

disp('[x,fval,exitflag,output] = fminsearch(f,x0,OPTIONS);'); [x,fval,exitflag,output] = fminsearch(f,x0,OPTIONS);

35


《数学实验与Matlab》程序(7).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:公选课试题

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: