声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 4286|回复: 10

[编程技巧] 谁有拟牛顿法、共轭梯度法、Powell方法之一编写无约束优化程序??

[复制链接]
发表于 2005-11-19 10:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
谁有拟牛顿法、共轭梯度法、Powell方法之一编写无约束优化程序??matlab的。。
拜谢了~~~~~~~~~~~~~~~~~
回复
分享到:

使用道具 举报

发表于 2005-11-19 11:31 | 显示全部楼层
拟牛顿法、共轭梯度法论坛以前的帖子中好象有,自己找一下吧
发表于 2005-11-19 15:35 | 显示全部楼层
最优化-无约束共轭梯度法程序(c++)
  1. /////////////////////////////////////////
  2. /////vector.h头文件/////
  3. /////定义向量及其基本运算/////
  4. /////////////////////////////////////////

  5. #include
  6. #defineMAXLENGTH10

  7. //向量定义
  8. typedefstruct{
  9. inttag;//行列向量标志。行向量为0,列向量为1。
  10. intdimension;//向量的维数
  11. doubleelem[MAXLENGTH];//向量的元素
  12. }vector;

  13. vectorvecCreat(inttag,intn){
  14. //建立维数为n的向量
  15. vectorx;
  16. x.tag=tag;
  17. x.dimension=n;
  18. for(inti=0;i
  19. #include"vector.h"
  20. #defineSIZE10
  21. #defineMAX1e300

  22. doublemin(doublea,doubleb){
  23. //求两个数最小
  24. returna}

  25. doublemax(doublea,doubleb){
  26. //求两个数最大
  27. returna>b?a:b;
  28. }

  29. vectorvecGrad(double(*pf)(vectorx),vectorpointx){
  30. //求解向量的梯度
  31. //采用理查德外推算法计算函数pf在pointx点的梯度grad;
  32. vectorgrad;
  33. grad.tag=1;grad.dimension=pointx.dimension;//初始化偏导向量
  34. vectortempPnt1,tempPnt2;//临时向量
  35. doubleh=1e-3;
  36. for(inti=0;itempPnt1=tempPnt2=pointx;
  37. tempPnt1.elem+=0.5*h;
  38. tempPnt2.elem-=0.5*h;
  39. grad.elem=(4*(pf(tempPnt1)-pf(tempPnt2)))/(3*h);
  40. tempPnt1.elem+=0.5*h;
  41. tempPnt2.elem-=0.5*h;
  42. grad.elem-=(pf(tempPnt1)-pf(tempPnt2))/(6*h);
  43. }
  44. returngrad;
  45. }

  46. doublevecFun(vectorvx){
  47. //最优目标多元函数
  48. doublex[SIZE];
  49. for(inti=0;ix[i+1]=vx.elem;
  50. //----------约束目标函数--------------
  51. //returnx[1]*x[1]+x[2]*x[2];
  52. //----------无约束正定函数--------------
  53. //returnx[1]*x[1]+4*x[2]*x[2]+9*x[3]*x[3]-2*x[1]+18*x[3];//例一
  54. //----------无约束非二次函数--------------
  55. //return(1-x[1])*(1-x[1])+(1-x[4])*(1-x[4])+(x[1]*x[1]-x[2])*(x[1]*x[1]-x
  56. [2])+(x[2]*x[2]-x[3])*(x[2]*x[2]-x[3])+(x[3]*x[3]-x[4])*(x[3]*x[3]-x[4]);//例二

  57. }

  58. doublevecFun_Si(vectorvx){
  59. //不等式约束函数
  60. doublex[SIZE];
  61. for(inti=0;ix[i+1]=vx.elem;
  62. returnx[1]+1;//不等式约束函数
  63. }

  64. doublevecFun_Hi(vectorvx){
  65. //等式约束函数
  66. doublex[SIZE];
  67. for(inti=0;ix[i+1]=vx.elem;
  68. returnx[1]+x[2]-2;//等式约束函数
  69. }

  70. doublevecFun_S(vectorx,doublev,doublel,doubleu){
  71. //约束问题转化为无约束问题的增广目标函数F(x,v,l,u)
  72. doublesum1=0,sum2=0,sum3=0;//分别定义三项的和
  73. //sum1
  74. doubletemp=max(0,v-2*u*vecFun_Si(x));
  75. sum1=(temp*temp-v*v)/(4*u);
  76. //sum2
  77. sum2=l*vecFun_Hi(x);
  78. //sum3
  79. sum3=u*vecFun_Hi(x)*vecFun_Hi(x);
  80. //F(x,v,l,u)=f(x)+sum1-sum2+sum3
  81. returnvecFun(x)+sum1-sum2+sum3;
  82. }

  83. vectorvecFunD_S(vectorx,doublev,doublel,doubleu){//利用重载函数实现目标函
  84. 数F(x,v,l,u)的导数
  85. //约束问题转化为无约束问题的增广目标函数F(x,v,l,u)的导函数
  86. vectorsum1,sum2,sum3;//分别定义三项导数的和
  87. //sum1
  88. sum1.dimension=x.dimension;sum1.tag=1;
  89. sum2.dimension=x.dimension;sum2.tag=1;
  90. sum3.dimension=x.dimension;sum3.tag=1;
  91. doubletemp=max(0,v-2*u*vecFun_Si(x));
  92. if(temp==0){
  93. for(inti=0;isum1.elem=0;
  94. }
  95. else{
  96. sum1=numMultiply(-(v-2*u*vecFun_Si(x)),vecGrad(vecFun_Si,x));
  97. }
  98. //-sum2
  99. sum2=numMultiply(-l,vecGrad(vecFun_Hi,x));
  100. //sum3
  101. sum3=numMultiply(2*u*vecFun_Hi(x),vecGrad(vecFun_Hi,x));
  102. //F=f(x)+sum1-sum2+sum3
  103. returnvecAdd(vecAdd(vecGrad(vecFun,x),sum1),vecAdd(sum2,sum3));
  104. }
  105. ///////////////////////////////////////////////////
  106. /////nonrestrict.h头文件/////
  107. /////包含无约束问题的求解函数:直线搜索/////
  108. /////共轭梯度法,H终止准则/////
  109. ///////////////////////////////////////////////////
  110. #include"restrict.h"

  111. vectorlineSearch(vectorx,vectorp,doublet){
  112. //从点x沿直线p方向对目标函数f(x)作直线搜索得到极小点x2,t为初始步长。
  113. vectorx2;
  114. doublel=0.1,n=0.4;//条件1和2的参数
  115. doublea=0,b=MAX;//区间
  116. doublef1=0,f2=0,g1=0,g2=0;
  117. inti=0;//迭代次数
  118. do{
  119. if(f2-f1>l*t*g1){b=t;t=(a+b)/2;}//改变b,t循环
  120. do{
  121. if(g2f1=vecFun(x);//f1=f(x)
  122. g1=vecMultiply(vecConvert(vecGrad(vecFun,x)),p);//g1=∨f(x)*p
  123. x2=vecAdd(numMultiply(t,p),x);//x2=x+t*p
  124. if(i++&&vecFun(x2)==f2){returnx2;}//【直线搜索进入无
  125. 限跌代,则此次跌代结束。返回当前最优点】
  126. f2=vecFun(x2);//f2=f(x2)
  127. g2=vecMultiply(vecConvert(vecGrad(vecFun,x2)),p);//g2=∨f(x2
  128. )*p
  129. //cout<<"("<.elem[3]<<");";//输出本次结果
  130. //cout<<"t="<//cout<<"f(x)="<}
  131. while(g2}
  132. while(f2-f1>l*t*g1);//不满足条件i,则改变b,t循环
  133. returnx2;
  134. }

  135. intHimmulblau(vectorx0,vectorx1,doublef0,doublef1){
  136. //H终止准则.给定Xk,Xk+1,Fk,Fk+1,判断Xk+1是否是极小点.返回1是极小,否则返回0
  137. doublec1,c2,c3;//定义并初始化终止限
  138. c1=c2=10e-5;
  139. c3=10e-4;
  140. if(vecMole(vecGrad(vecFun,x1))if(vecMole(vecAdd(x1,numMultiply(-1,x0)))/(vecMole(x0)+1)if(fabs(f1-f0)/(fabs(f0)+1)return1;
  141. return0;
  142. }

  143. voidFletcher_Reeves(vectorxx,vector&minx,double&minf){
  144. //Fletcher-Reeves共轭梯度法.
  145. //给定初始点xx.对vecFun函数求得最优点x和最优解f,分别用minx和minf返回
  146. intk=0,j=0;//迭代次数,j为总迭代次数
  147. doublec=10e-1;//终止限c
  148. intn=xx.dimension;//问题的维数
  149. doublef0,f,a;//函数值f(x),a为使p方向向量共轭的系数
  150. vectorg0,g;//梯度g0,g
  151. vectorp0,p;//搜索方向P0,p
  152. vectorx,x0;//最优点和初始点
  153. doublet=1;//直线搜索的初始步长=1
  154. x=xx;//x0
  155. f=vecFun(x);//f0=f(x0)
  156. g=vecGrad(vecFun,x);//g0
  157. p0=numMultiply(-1,g);//p0=-g0,初始搜索方向为负梯度方向
  158. do{
  159. x0=x;f0=f;g0=g;
  160. x=lineSearch(x0,p0,t);//从点x出发,沿p方向作直线搜索
  161. f=vecFun(x);//f=f(x)
  162. g=vecGrad(vecFun,x);//g=g(x)
  163. if(Himmulblau(x0,x,f0,f)==1){//满足H终止准则,返回最优点及最优解。
  164. cout<<"*总迭代次数:"<minx=x;minf=f;
  165. break;
  166. }
  167. else{//不满足H准则
  168. cout<<"*第"<showPoint(x,f);//显示中间结果x,f

  169. if(k==n){//若进行了n+1次迭代
  170. k=0;//重置k=0,p0=-g
  171. p0=numMultiply(-1,g);
  172. t=vecMole(vecAdd(x,numMultiply(-1,x0)))/vecMole(p0);//下次直线
  173. 搜索步长
  174. continue;//进入下一次循环,重新直线搜索
  175. }
  176. else{//否则,计算共轭向量p
  177. a=vecMole(g)*vecMole(g)/(vecMole(g0)*vecMole(g0));
  178. p=vecAdd(numMultiply(-1,g),numMultiply(a,p0));
  179. }
  180. }
  181. if(fabs(vecMultiply(vecConvert(p),g))<=c){//共轭梯度方向下降或上升量很

  182. p0=numMultiply(-1,g);//p0=-g,k=0
  183. k=0;
  184. }
  185. elseif(vecMultiply(vecConvert(p),g)<-c){//共轭梯度方向下降并且下降量保

  186. p0=p;//p0=p,k=k+1
  187. ++k;
  188. }
  189. else{//共轭梯度方向上升并且下降量保

  190. p0=numMultiply(-1,p);//p0=-p,k=k+1
  191. ++k;
  192. }
  193. t=vecMole(vecAdd(x,numMultiply(-1,x0)))/vecMole(p0);//下次直线搜索步长

  194. }
  195. while(++j);
  196. }

  197. ///////////////////////////////////////////////////
  198. /////main.h头文件/////
  199. /////主程序文件,控制整个程序的流程/////
  200. ///////////////////////////////////////////////////
  201. #include"nonrestrict.h"

  202. voidprintSelect(){
  203. cout<<"**************最优化方法***************"<cout<<"*****************************************"<cout<<"***请选择:***"<cout<<"***1.无约束最小化问题(共轭梯度法)***"<cout<<"***2.约束最小化问题(H乘子法)***"<cout<<"***3.退出(exit)***"<cout<<"*****************************************"<}

  204. voidsub1(){
  205. charkey;
  206. cout<<"--------------------共轭梯度法求无约束最小化问题----------------"<<
  207. endl;
  208. cout<<"步骤:"<cout<<"◆1.输入f(x)及其导数.(参考function.h文件说明)"<cout<<"-----确认是否已输入<目标函数>及其<导数>?(Y/N)";
  209. cin>>key;
  210. if(key=='N'||key=='n')return;
  211. elseif(key=='Y'||key=='y'){
  212. vectorx0;//起始点
  213. intm;
  214. cout<<"◆2.起始点X0初始化"<cin>>m;
  215. x0=vecCreat(1,m);
  216. vectorminx;//求得的极小点
  217. doubleminf;//求得的极小值
  218. Fletcher_Reeves(x0,minx,minf);
  219. cout<<"◆最优解及最优值:";
  220. showPoint(minx,minf);
  221. }
  222. }

  223. voidsub2(){
  224. charkey;
  225. cout<<"------------------H乘子法求约束最小化问题-----------------"<;
  226. cout<<"步骤:"<cout<<"◆1.输入f(x)及其导数.(参考function.h文件说明)"<cout<<"-----确认是否已输入<目标函数及导数>和<约束条件>?(Y/N)";
  227. cin>>key;
  228. if(key=='N'||key=='n')return;
  229. elseif(key=='Y'||key=='y'){
  230. vectorx0;//起始点
  231. intm;
  232. cout<<"◆2.起始点X0初始化"<cin>>m;
  233. x0=vecCreat(1,m);
  234. vectorminx;//求得的极小点
  235. doubleminf;//求得的极小值
  236. Hesternes(x0,minx,minf);
  237. showPoint(minx,minf);
  238. }
  239. }

  240. voidmain(){
  241. intmark=1;
  242. while(mark){
  243. printSelect();
  244. intsel;
  245. cin>>sel;
  246. switch(sel){
  247. case1:
  248. sub1();
  249. break;
  250. case2:
  251. sub2();
  252. break;
  253. case3:
  254. mark=0;
  255. break;
  256. };
  257. }
  258. }
复制代码



自己改写吧
发表于 2005-11-19 15:37 | 显示全部楼层
另外《MATLAB 6.0程序设计与实例应用》好像有matlab的
 楼主| 发表于 2005-11-19 20:26 | 显示全部楼层
非常感谢老兄们的回复!
to 2楼:我搜过了,怎么没搜到呢?
to 3楼:看不太懂
to 4楼:我找过了,那个共轭梯度法不是用来无约束优化的
发表于 2005-11-19 20:41 | 显示全部楼层
看帖子
http://forum.vibunion.com/thread-732-1-1.html
4楼讲的就是无约束非线性规划问题
[此贴子已经被作者于2005-11-19 20:41:54编辑过]

发表于 2006-3-29 10:09 | 显示全部楼层
这有一个,看能不能用
  1. function [x,OPTIONS] = fminu(FUN,x,OPTIONS,GRADFUN,varargin)
  2. %多元函数极值拟牛顿法,适用于光滑函数优化&#65533;
  3. %x=fminu('fun',x0)牛顿法求多元函数y=f(x)在x0出发的局部极小值点&#65533;
  4. % 这里 x,x0均为向量。
  5. %x=fminu('fun',x0,options)输入options是优化中算法参数向量设置,
  6. % 用help foptions可看到各分量的含义。
  7. %x=fminu('fun',x0,options,'grad') grad给定f(x)的梯度函数表达式?杉涌旒扑闼俣取&#65533;
  8. %x=fminu('fun',x0,options,'grad',p1,p2…)p1,p2,..是表示fun的M函数中的参数。
  9. %例题 求f(x)=100(x2-x1^2)^2+(1-x1)^2在[-1.2,1]附近的极小值。
  10. % 先写M函数optfun1.m
  11. % function f=optfun1(x)
  12. % f=100*(x(2)-x(1).^2).^2+(1-x(1)).^2
  13. % 求解
  14. % clear;
  15. % x=[-1.2,1];
  16. % [x,options]=fminu('optfun1',x,options);
  17. % x,options(8)
  18. %
  19. %FMINU Finds the minimum of a function of several variables.
  20. % X=FMINU('FUN',X0) starts at the matrix X0 and finds a minimum to the
  21. % function which is described in FUN (usually an M-file: FUN.M).
  22. % The function 'FUN' should return a scalar function value: F=FUN(X).
  23. %
  24. % X=FMINU('FUN',X0,OPTIONS) allows a vector of optional parameters to
  25. % be defined. OPTIONS(1) controls how much display output is given; set
  26. % to 1 for a tabular display of results, (default is no display: 0).
  27. % OPTIONS(2) is a measure of the precision required for the values of
  28. % X at the solution. OPTIONS(3) is a measure of the precision
  29. % required of the objective function at the solution.
  30. % For more information type HELP FOPTIONS.
  31. %
  32. % X=FMINU('FUN',X0,OPTIONS,'GRADFUN') enables a function'GRADFUN'
  33. % to be entered which returns the partial derivatives of the function,
  34. % df/dX, at the point X: gf = GRADFUN(X).
  35. %
  36. % X=FMINU('FUN',X0,OPTIONS,'GRADFUN',P1,P2,...) passes the problem-
  37. % dependent parameters P1,P2,... directly to the functions FUN
  38. % and GRADFUN: FUN(X,P1,P2,...) and GRADFUN(X,P1,P2,...). Pass
  39. % empty matrices for OPTIONS, and 'GRADFUN' to use the default
  40. % values.
  41. %
  42. % [X,OPTIONS]=FMINU('FUN',X0,...) returns the parameters used in the
  43. % optimization method. For example, options(10) contains the number
  44. % of function evaluations used.
  45. %
  46. % The default algorithm is the BFGS Quasi-Newton method with a
  47. % mixed quadratic and cubic line search procedure.

  48. % Copyright (c) 1990-98 by The MathWorks, Inc.
  49. % Revision:1.27 Date:1997/11/2901:23:11
  50. % Andy Grace 7-9-90.


  51. % ------------Initialization----------------
  52. XOUT=x(:);
  53. nvars=length(XOUT);

  54. if nargin < 2, error('fminu requires two input arguments');end
  55. if nargin < 3, OPTIONS=[]; end
  56. if nargin < 4, GRADFUN=[]; end

  57. % Convert to inline function as needed.
  58. if ~isempty(FUN)
  59. [funfcn, msg] = fcnchk(FUN,length(varargin));
  60. if ~isempty(msg)
  61. error(msg);
  62. end
  63. else
  64. error('FUN must be a function name or valid expression.')
  65. end

  66. if ~isempty(GRADFUN)
  67. [gradfcn, msg] = fcnchk(GRADFUN,length(varargin));
  68. if ~isempty(msg)
  69. error(msg);
  70. end
  71. else
  72. gradfcn = [];
  73. end

  74. f = feval(funfcn,x,varargin{:});
  75. n = length(XOUT);
  76. GRAD=zeros(nvars,1);
  77. OLDX=XOUT;
  78. MATX=zeros(3,1);
  79. MATL=[f;0;0];
  80. OLDF=f;
  81. FIRSTF=f;
  82. [OLDX,OLDF,HESS,OPTIONS]=optint(XOUT,f,OPTIONS);
  83. CHG = 1e-7*abs(XOUT)+1e-7*ones(nvars,1);
  84. SD = zeros(nvars,1);
  85. diff = zeros(nvars,1);
  86. PCNT = 0;
  87. how = '';


  88. OPTIONS(10)=2; % Function evaluation count (add 1 for last evaluation)
  89. status =-1;

  90. while status ~= 1
  91. % Work Out Gradients
  92. if isempty(gradfcn) | OPTIONS(9)
  93. OLDF=f;
  94. % Finite difference perturbation levels
  95. % First check perturbation level is not less than search direction.
  96. f = find(10*abs(CHG)>abs(SD));
  97. CHG(f) = -0.1*SD(f);
  98. % Ensure within user-defined limits
  99. CHG = sign(CHG+eps).*min(max(abs(CHG),OPTIONS(16)),OPTIONS(17));
  100. for gcnt=1:nvars
  101. XOUT(gcnt,1)=XOUT(gcnt)+CHG(gcnt);
  102. x(:) = XOUT;
  103. f = feval(funfcn,x,varargin{:});
  104. GRAD(gcnt)=(f-OLDF)/(CHG(gcnt));
  105. if f < OLDF
  106. OLDF=f;
  107. else
  108. XOUT(gcnt)=XOUT(gcnt)-CHG(gcnt);
  109. end
  110. end
  111. % Try to set difference to 1e-8 for next iteration
  112. % Add eps for machines that can't handle divide by zero.
  113. CHG = 1e-8./(GRAD + eps);
  114. f = OLDF;
  115. OPTIONS(10)=OPTIONS(10)+nvars;
  116. % Gradient check
  117. if OPTIONS(9) == 1
  118. GRADFD = GRAD;
  119. x(:)=XOUT;
  120. GRAD(:) = feval(gradfcn,x,varargin{:});
  121. if isa(gradfcn,'inline')
  122. graderr(GRADFD, GRAD, formula(gradfcn));
  123. else
  124. graderr(GRADFD, GRAD, gradfcn);
  125. end
  126. OPTIONS(9) = 0;
  127. end

  128. else
  129. OPTIONS(11)=OPTIONS(11)+1;
  130. x(:)=XOUT;
  131. GRAD(:) = feval(gradfcn,x,varargin{:});
  132. end
  133. %---------------Initialization of Search Direction------------------
  134. if status == -1
  135. SD=-GRAD;
  136. FIRSTF=f;
  137. OLDG=GRAD;
  138. GDOLD=GRAD'*SD;
  139. % For initial step-size guess assume the minimum is at zero.
  140. OPTIONS(18) = max(0.001, min([1,2*abs(f/GDOLD)]));
  141. if OPTIONS(1)>0
  142. disp([sprintf('%5.0f %12.6g %12.6g ',OPTIONS(10),f,OPTIONS(18)),sprintf('%12.3g ',GDOLD)]);
  143. end
  144. XOUT=XOUT+OPTIONS(18)*SD;
  145. status=4;
  146. if OPTIONS(7)==0; PCNT=1; end

  147. else
  148. %-------------Direction Update------------------
  149. gdnew=GRAD'*SD;
  150. if OPTIONS(1)>0,
  151. num=[sprintf('%5.0f %12.6g %12.6g ',OPTIONS(10),f,OPTIONS(18)),sprintf('%12.3g ',gdnew)];
  152. end
  153. if (gdnew>0 & f>FIRSTF)|~finite(f)
  154. % Case 1: New function is bigger than last and gradient w.r.t. SD -ve
  155. % ...interpolate.
  156. how='inter';
  157. [stepsize]=cubici1(f,FIRSTF,gdnew,GDOLD,OPTIONS(18));
  158. if stepsize<0|isnan(stepsize), stepsize=OPTIONS(18)/2; how='C1f '; end
  159. if OPTIONS(18)<0.1&OPTIONS(6)==0
  160. if stepsize*norm(SD)<eps
  161. stepsize=exp(rand(1,1)-1)-0.1;
  162. how='RANDOM STEPLENGTH';
  163. status=0;
  164. else
  165. stepsize=stepsize/2;
  166. end
  167. end
  168. OPTIONS(18)=stepsize;
  169. XOUT=OLDX;
  170. elseif f<FIRSTF
  171. [newstep,fbest] =cubici3(f,FIRSTF,gdnew,GDOLD,OPTIONS(18));
  172. sk=(XOUT-OLDX)'*(GRAD-OLDG);
  173. if sk>1e-20
  174. % Case 2: New function less than old fun. and OK for updating HESS
  175. % .... update and calculate new direction.
  176. how='';
  177. if gdnew<0
  178. how='incstep';
  179. if newstep<OPTIONS(18), newstep=2*OPTIONS(18)+1e-5; how=[how,' IF']; end
  180. OPTIONS(18)=min([max([2,1.5*OPTIONS(18)]),1+sk+abs(gdnew)+max([0,OPTIONS(18)-1]), (1.2+0.3*(~OPTIONS(7)))*abs(newstep)]);
  181. else % gdnew>0
  182. if OPTIONS(18)>0.9
  183. how='int_st';
  184. OPTIONS(18)=min([1,abs(newstep)]);
  185. end
  186. end %if gdnew
  187. [HESS,SD]=updhess(XOUT,OLDX,GRAD,OLDG,HESS,OPTIONS);
  188. gdnew=GRAD'*SD;
  189. OLDX=XOUT;
  190. status=4;
  191. % Save Variables for next update
  192. FIRSTF=f;
  193. OLDG=GRAD;
  194. GDOLD=gdnew;
  195. % If mixed interpolation set PCNT
  196. if OPTIONS(7)==0, PCNT=1; MATX=zeros(3,1); MATL(1)=f; end
  197. elseif gdnew>0 %sk<=0
  198. % Case 3: No good for updating HESSIAN .. interpolate or halve step length.
  199. how='inter_st';
  200. if OPTIONS(18)>0.01
  201. OPTIONS(18)=0.9*newstep;
  202. XOUT=OLDX;
  203. end
  204. if OPTIONS(18)>1, OPTIONS(18)=1; end
  205. else
  206. % Increase step, replace starting point
  207. OPTIONS(18)=max([min([newstep-OPTIONS(18),3]),0.5*OPTIONS(18)]);
  208. how='incst2';
  209. OLDX=XOUT;
  210. FIRSTF=f;
  211. OLDG=GRAD;
  212. GDOLD=GRAD'*SD;
  213. OLDX=XOUT;
  214. end % if sk>
  215. % Case 4: New function bigger than old but gradient in on
  216. % ...reduce step length.
  217. else %gdnew<0 & F>FIRSTF
  218. if gdnew<0&f>FIRSTF
  219. how='red_step';
  220. if norm(GRAD-OLDG)<1e-10; HESS=eye(nvars); end
  221. if abs(OPTIONS(18))<eps
  222. SD=norm(nvars,1)*(rand(nvars,1)-0.5);
  223. OPTIONS(18)=abs(rand(1,1)-0.5)*1e-6;
  224. how='RANDOM SD';
  225. else
  226. OPTIONS(18)=-OPTIONS(18)/2;
  227. end
  228. XOUT=OLDX;
  229. end %gdnew>0
  230. end % if (gdnew>0 & F>FIRSTF)|~finite(F)
  231. XOUT=XOUT+OPTIONS(18)*SD;
  232. if isinf(OPTIONS(1))
  233. disp([num,how])
  234. elseif OPTIONS(1)>0
  235. disp(num)
  236. end
  237. end %----------End of Direction Update-------------------

  238. % Check Termination
  239. if max(abs(SD))<2*OPTIONS(2) & (-GRAD'*SD) < 2*OPTIONS(3)
  240. if OPTIONS(1) > 0
  241. disp('Optimization Terminated Successfully')
  242. disp(' Search direction less than 2*options(2)')
  243. disp(' Gradient in the search direction less than 2*options(3)')
  244. disp([' NUMBER OF FUNCTION EVALUATIONS=', int2str(OPTIONS(10))]);
  245. end
  246. status=1;
  247. elseif OPTIONS(10)>OPTIONS(14)
  248. if OPTIONS(1) >= 0
  249. disp('Maximum number of function evaluations exceeded;')
  250. disp(' increase options(14).')
  251. end
  252. status=1;
  253. else

  254. % Line search using mixed polynomial interpolation and extrapolation.
  255. if PCNT~=0
  256. while PCNT > 0 & OPTIONS(10) <= OPTIONS(14)
  257. x(:) = XOUT;
  258. f = feval(funfcn,x,varargin{:});
  259. OPTIONS(10)=OPTIONS(10)+1;
  260. [PCNT,MATL,MATX,steplen,f, how]=searchq(PCNT,f,OLDX,MATL,MATX,SD,GDOLD,OPTIONS(18), how);
  261. OPTIONS(18)=steplen;
  262. XOUT=OLDX+steplen*SD;
  263. end
  264. if OPTIONS(10)>OPTIONS(14)
  265. if OPTIONS(1) >= 0
  266. disp('Maximum number of function evaluations exceeded;')
  267. disp(' increase options(14).')
  268. end
  269. status=1;
  270. end
  271. else
  272. x(:)=XOUT;
  273. f = feval(funfcn,x,varargin{:});
  274. OPTIONS(10)=OPTIONS(10)+1;
  275. end
  276. end
  277. end

  278. x(:)=XOUT;
  279. f = feval(funfcn,x,varargin{:});
  280. if f > FIRSTF
  281. OPTIONS(8) = FIRSTF;
  282. x(:)=OLDX;
  283. else
  284. OPTIONS(8) = f;
  285. end
复制代码
发表于 2006-6-19 15:02 | 显示全部楼层
这个我也找到了 ~不还是看不懂啊~
发表于 2006-6-19 15:03 | 显示全部楼层
为什么哪些例题上程序那么简单啊~真的要编怎么又那么多啊 ~
发表于 2006-6-19 18:56 | 显示全部楼层
呵呵,真辛苦各位拉!
发表于 2006-6-19 19:46 | 显示全部楼层
谢谢
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-25 11:21 , Processed in 0.078620 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表