home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 4.ddi / OPTIM.DI$ / TUTDEMO.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  10.3 KB  |  348 lines

  1. echo on; clc
  2. %TUTDEMO Tutorial for Optimization Toolbox.
  3. %#########################################################################
  4. %
  5. %   This is a demonstration script-file for the OPTIMIZATION TOOLBOX 
  6. %   It closely follows the Tutorial section of the users' guide
  7. %
  8. %   It consists of:     (1) unconstrained optimization example
  9. %            (2) constrained optimization example 
  10. %            (3) constrained example using gradients
  11. %            (4) bounded example
  12. %            (5) equality constrained example
  13. %            (6) unconstrained ex. using user-supplied settings
  14. %
  15. %   All the principles outlined in this demonstration apply to the other
  16. %   routines: attgoal, minimax, leastsq, fsolve.
  17. %
  18. %   The routines differ from the Tutorial Section examples in the manual 
  19. %   only in that M-files for the objective functions have not been coded 
  20. %   up. Instead the expression form of the syntax has been
  21. %   used where functions to be minimized are expressed as MATLAB string 
  22. %   variables. 
  23. %
  24. %##########################################################################
  25. %
  26. pause % Strike any key to continue (Note: use Ctrl-C to abort)
  27. clc
  28.  
  29. %###########################################################################
  30. %               DEMO 1: UNCONSTRAINED PROBLEM
  31. %---------------------------------------------------------------------------
  32. %
  33. %    Consider initially the problem of finding a minimum to the function:
  34. %
  35. %                  2       2
  36. %        f(x)=exp(x(1)) .(4x(1)  + 2x(2) + 4x(1)x(2) + 1
  37. %
  38. %---------------------------------------------------------------------------
  39. pause % Strike any key to continue
  40.  
  41.  
  42. %   Take a guess at the solution
  43.  
  44. X0=[-1, 1];
  45.  
  46. pause % Strike any key to continue
  47.  
  48. options=[];    % Use default parameters
  49.  
  50. pause % Strike any key to start the optimization
  51.  
  52. [x,options]=fminu('exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1)', X0, options);
  53.  
  54.  
  55. %  The optimizer has found a solution at:
  56. x
  57. %  The function value at the solution is:
  58. options(8)
  59. pause % Strike any key to continue
  60. %  The total number of function evaluations was: 
  61. options(10)
  62.  
  63. % Note: by entering the expression explicitly in a string using the 
  64. %        variable 'x' we avoid having to write an M-file.
  65.  
  66. pause % Strike any key for next demo
  67. clc
  68. %%###########################################################################
  69. %               DEMO 2: CONSTRAINED PROBLEM
  70. %---------------------------------------------------------------------------
  71. %
  72. %    Consider the above problem with two additional constraints:
  73. %
  74. %                  2       2
  75. %    minimize f(x)=exp(x(1)) .(4x(1)  + 2x(2) + 4x(1)x(2) + 1
  76. %
  77. %    subject to: 1.5 + x(1).x(2) -x(1) -x(2) <=0
  78. %            -x(1).x(2) <= 10
  79. %
  80. %-------------------------------------------------------------------------
  81. pause % Strike any key to continue
  82.  
  83. % Make a guess at the solution:
  84.  
  85. X0=[-1, 1];
  86.  
  87. options=[];     % Use default parameters
  88.  
  89. pause % Strike any key to start optimization:
  90.  
  91. [x,options]=constr(['f = exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);',...
  92.     'g(1)=1.5+x(1)*x(2)-x(1)-x(2);',...
  93.     'g(2)=-x(1)*x(2)-10;'], X0, options);
  94.  
  95.  
  96. % A solution to this problem has been found at:
  97. % The function value at the solution is: 
  98. options(8)
  99. pause % Strike any key to continue
  100. % Both the constraints are active at the solution:
  101. g   =[    1.5+x(1)*x(2)-x(1)-x(2)
  102.     -x(1)*x(2)-10    ]
  103. % The total number of function evaluations was: 
  104. options(10)
  105.  
  106. pause % Strike any key for next demo
  107. clc
  108.  
  109. %############################################################################
  110. %               DEMO 3: BOUNDED EXAMPLE
  111. %----------------------------------------------------------------------------
  112. %
  113. %    Consider the above problem with additional bound constraints:
  114. %
  115. %                  2       2
  116. %    minimize f(x)=exp(x(1)) .(4x(1)  + 2x(2) + 4x(1)x(2) + 1
  117. %
  118. %    subject to: 1.5 + x(1).x(2) -x(1) -x(2) <=0
  119. %            -x(1).x(2) <= 10
  120. %
  121. %        and  x(1) >=0
  122. %             x(2) >=0
  123. %
  124. %----------------------------------------------------------------------------
  125.  
  126.  
  127. pause % Strike any key to continue
  128.  
  129. % Again, make a guess at the solution:
  130.   
  131. X0=[-1, 1];
  132.  
  133. pause % Strike any key to continue
  134.  
  135. % This time we will use the bounded syntax:
  136. %    X=CONSTR(`FUN', X0, OPTIONS, VLB, VUB) 
  137.  
  138.     VLB=zeros(1,2);    % Lower bounds X>0
  139.     VUB=[];     % No upper bounds 
  140.  
  141. pause % Strike any key to start the optimization
  142.  
  143. [x,options]=constr(['f = exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);',...
  144.     'g(1)=1.5+x(1)*x(2)-x(1)-x(2);',...
  145.     'g(2)=-x(1)*x(2)-10;'], X0, options, VLB, VUB);
  146.  
  147. % The solution to this problem has been found at:
  148. % The function value at the solution is: 
  149. options(8)
  150. % The constraint values at the solution are:
  151. g   =[    1.5+x(1)*x(2)-x(1)-x(2)
  152.     -x(1)*x(2)-10    ]
  153. % The total number of function evaluations was: 
  154. options(10)
  155.  
  156. pause % Strike any key for next demo
  157. clc
  158. %%###########################################################################
  159. %               DEMO 4: USER-SUPPLIED GRADIENTS
  160. %----------------------------------------------------------------------------
  161. %  The above problem can be solved more efficiently and accurately if gradients
  162. %  are supplied by the user. This demo shows how this may be performed.
  163. %
  164. %  Again the  problem is:
  165. %
  166. %                  2       2
  167. %    minimize f(x)=exp(x(1)) .(4x(1)  + 2x(2) + 4x(1)x(2) + 1
  168. %
  169. %    subject to: 1.5 + x(1).x(2) -x(1) -x(2) <=0
  170. %            -x(1).x(2) <= 10
  171. %
  172. %-------------------------------------------------------------------------
  173.  
  174. pause % Strike any key to continue
  175.  
  176. % Make a guess at the solution:
  177.  
  178. X0=[-1, 1];
  179.  
  180.  
  181. options=[];     % Use default parameters
  182.  
  183. pause % Strike any key to enter function and constraints into a string:
  184.  
  185.  
  186. fun = [
  187. % Objective Function and constraints
  188.     'f = exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);', ...
  189.     'g(1)=1.5+x(1)*x(2)-x(1)-x(2);',...  %Constraints
  190.     'g(2)=-x(1)*x(2)-10;'];
  191.  
  192.  
  193. pause % Strike any key to load the partial derivatives into a string: 
  194.  
  195. jacob = ['t= exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);',...
  196.     'gf=[t+4*exp(x(1))*(2*x(1)+x(2));',...
  197.         '4*exp(x(1))*(x(1)+x(2)+0.5)];',...
  198.     'gg = [ x(2)-1,   -x(2);',... 
  199.         'x(1)-1,  -x(1)];'];
  200.  
  201.  
  202. pause % Strike any key to start the optimization
  203.  
  204. [x,options]=constr(fun, X0, options, [], [], jacob);
  205.  
  206. % As before the solution to this problem has been found at:
  207. % The function value at the solution is: 
  208. options(8)
  209. pause % Strike any key to continue
  210. % Both the constraints are active at the solution:
  211. g   =[    1.5+x(1)*x(2)-x(1)-x(2)
  212.     -x(1)*x(2)-10    ]
  213. % The total number of function evaluations was: 
  214. options(10)
  215.  
  216. pause % Strike any key for next demo
  217. clc
  218. %############################################################################
  219. %               DEMO 5: EQUALITY CONSTRAINED EXAMPLE
  220. %---------------------------------------------------------------------------
  221. %
  222. %    Consider the above problem with an additional equality constraint
  223. %
  224. %                  2       2
  225. %    minimize f(x)=exp(x(1)) .(4x(1)  + 2x(2) + 4x(1)x(2) + 1
  226. %
  227. %    subject to: x(1)+ x(2) = 0
  228. %                   1.5 + x(1).x(2) -x(1) -x(2) <=0
  229. %            -x(1).x(2) <= 0
  230. %
  231. %-------------------------------------------------------------------------
  232.  
  233.  
  234.  % Again, make a guess at the solution: 
  235.  
  236. X0=[-1, 1];
  237.  
  238. pause % Strike any key to continue
  239.  
  240. % This time we will indicate the number of equality constraints
  241. % by setting options(13) to the number of constraints. 
  242. % The equality constraints must be contained the first few elements of g
  243.  
  244. options(13)=1; % One equality constraint
  245.  
  246. pause % Strike any key to start the optimization
  247.  
  248. [x,options]=constr(['f = exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);',...
  249.     'g(1)=x(1)+x(2);',...
  250.     'g(2)=1.5+x(1)*x(2)-x(1)-x(2);',...
  251.     'g(3)=-x(1)*x(2)-10;'], X0, options);
  252.  
  253. % The solution to this problem has been found at:
  254. % The function value at the solution is: 
  255. options(8)
  256. pause % Strike any key to continue
  257. % The constraint values at the solution are:
  258. g   =[    x(1)+x(2)
  259.     1.5+x(1)*x(2)-x(1)-x(2)
  260.     -x(1)*x(2)-10    ]
  261. % The total number of function evaluations was: 
  262. options(10)
  263.  
  264. pause % Strike any key  for next demo
  265. clc
  266.  
  267.  
  268. %############################################################################
  269. %               DEMO 6: CHANGING THE DEFAULT SETTINGS
  270. %---------------------------------------------------------------------------
  271. %
  272. %    Consider the original unconstrained problem
  273. %
  274. %                  2       2
  275. %    minimize f(x)=exp(x(1)) .(4x(1)  + 2x(2) + 4x(1)x(2) + 1
  276. %
  277. %    this time we will solve it more accurately by overriding the 
  278. %       default termination criteria (OPTIONS(2) and OPTIONS(3)). 
  279. %-------------------------------------------------------------------------
  280.  
  281.  
  282. pause % Strike any key to continue
  283.  
  284. % Again, make a guess at the solution:
  285.  
  286. X0=[-1, 1];
  287.  
  288. pause % Strike any key to continue
  289.  
  290. % Override the default termination criteria:
  291. options(2)=1e-8;       % Termination tolerance on X
  292. options(3)=1e-8;    % Termination tolerance on F
  293.  
  294. pause % Strike any key to start the optimization
  295.  
  296. [x,options]=fminu('exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1)', X0, options);
  297.  
  298. %  The optimizer has found a solution at:
  299. x
  300. %  The function value at the solution is:
  301. options(8)
  302. pause % Strike any key to continue
  303. %  The total number of function evaluations was: 
  304. options(10)
  305.  
  306.  
  307. %     Note: warning messages may be displayed because of problems
  308. %     in the calculation of finite difference gradients at the 
  309. %    solution point. They are an indication that tolerances are overly 
  310. %    stringent, however, the optimizer always continues to make steps 
  311. %    towards the solution. 
  312.  
  313. pause % Strike any key to continue
  314.  
  315. % If we want a tabular display of each iteration we can set options(1)=1
  316. % as follows:
  317.  
  318. options=1;  % Set display parameter to on, the others to default. 
  319.  
  320. pause % Strike any key to start the optimization
  321.  
  322. X0=[-1, 1];
  323. [x,options]=fminu('exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1)', X0, options);
  324.  
  325. %  The optimizer has found a solution at:
  326. x
  327. %  The function value at the solution is:
  328. options(8)
  329. pause % Strike any key to continue
  330. %  The total number of function evaluations was: 
  331. options(10)
  332.  
  333. %  At each major iteration the table displayed consists of: 
  334. %            -     number of function values
  335. %            -    function value
  336. %            -    step length used in the line search
  337. %            -    gradient in the direction of search
  338. %            -    line search procedures
  339. %    
  340.  
  341. %------------------------------Demo End-----------------------------
  342. pause % Strike any key for main menu
  343. echo off
  344.