⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 banana.m

📁 最优化的bfgs算法
💻 M
字号:
%以下程序段是画香蕉函数图形。
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) 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);');
      %(调用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')
        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);
               %fminsearch 是多变量函数寻优的单纯形法指令,用法和fminunc是类似的。        
        hold off
        disp(' ')
        disp('Strike any key for menu')
        pause
    end
 end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -