设为首页收藏本站

EPS数据狗论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1215|回复: 0

MATLAB优化工具箱教程

[复制链接]

2

主题

71

金钱

90

积分

新手用户

发表于 2018-9-17 09:17:13 | 显示全部楼层 |阅读模式
  1. echo off
  2. %优化工具箱简明教程
  3. %TUTDEMO Tutorial for Optimization Toolbox.

  4. %   Copyright (c) 1990-98 by The MathWorks, Inc.
  5. %   $Revision: 1.12 $  $Date: 1997/11/29 01:23:27 $

  6. echo on; clc
  7. %TUTDEMO Tutorial for Optimization Toolbox.
  8. %##########################################################################
  9. %
  10. %   This is a demonstration script-file for the OPTIMIZATION TOOLBOX
  11. %   It closely follows the Tutorial section of the users' guide
  12. %
  13. %   It consists of:
  14. %           (1) unconstrained optimization example
  15. %           (2) constrained optimization example
  16. %           (3) constrained example using gradients
  17. %           (4) bounded example
  18. %           (5) equality constrained example
  19. %           (6) unconstrained example using user-supplied settings
  20. %
  21. %   All the principles outlined in this demonstration apply to the other
  22. %   routines: attgoal, minimax, leastsq, fsolve.
  23. %
  24. %   The routines differ from the Tutorial Section examples in the manual
  25. %   only in that M-files for the objective functions have not been coded
  26. %   up. Instead the expression form of the syntax has been used where
  27. %   functions to be minimized are expressed as MATLAB string variables.
  28. %
  29. %##########################################################################

  30. pause % Strike any key to continue (Note: use Ctrl-C to abort)
  31. clc
  32. %##########################################################################
  33. %           DEMO 1: UNCONSTRAINED PROBLEM
  34. %--------------------------------------------------------------------------
  35. %
  36. %   Consider initially the problem of finding a minimum to the function:
  37. %
  38. %                                2        2
  39. %       f(x) = exp(x(1)) . (4x(1)  + 2x(2)  + 4x(1).x(2) + 2x(2) + 1)
  40. %
  41. %--------------------------------------------------------------------------
  42. pause % Strike any key to continue

  43. % The function to be minimized is

  44. fun = 'exp(x(1)) * (4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1)';

  45. pause % Strike any key to continue

  46. % Take a guess at the solution

  47. x0 = [-1 1];

  48. pause % Strike any key to continue

  49. % Use default parameters

  50. options = [];

  51. pause % Strike any key to start the optimization

  52. [x, options] = fminu(fun, x0, options);

  53. %  The optimizer has found a solution at:
  54. x
  55. pause % Strike any key to continue

  56. %  The function value at the solution is:
  57. options(8)
  58. pause % Strike any key to continue

  59. %  The total number of function evaluations was:
  60. options(10)
  61. % Note: by entering the expression explicitly in a string using
  62. % the variable 'x' we avoid having to write an M-file.

  63. pause % Strike any key for next demo
  64. clc
  65. %##########################################################################
  66. %           DEMO 2: CONSTRAINED PROBLEM
  67. %--------------------------------------------------------------------------
  68. %
  69. %   Consider the above problem with two additional constraints:
  70. %
  71. %                                      2        2
  72. %   minimize  f(x) = exp(x(1)) . (4x(1)  + 2x(2)  + 4x(1).x(2) + 2x(2) + 1)
  73. %
  74. %   subject to  1.5 + x(1).x(2) - x(1) - x(2) <= 0
  75. %                   - x(1).x(2)               <= 10
  76. %
  77. %--------------------------------------------------------------------------
  78. pause % Strike any key to continue

  79. % Objective Function and constraints

  80. funf = 'f = exp(x(1)) * (4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1);';
  81. fung = 'g = [1.5 + x(1)*x(2) - x(1) - x(2); -x(1)*x(2) - 10];';
  82. fun = [funf fung];

  83. pause % Strike any key to continue

  84. % Take a guess at the solution

  85. x0 = [-1 1];

  86. pause % Strike any key to continue

  87. % Use default parameters

  88. options = [];

  89. pause % Strike any key to start the optimization

  90. [x, options] = constr(fun, x0, options);

  91. % A solution to this problem has been found at:
  92. x
  93. pause % Strike any key to continue

  94. % The function value at the solution is:
  95. options(8)
  96. pause % Strike any key to continue

  97. % Both the constraints are active at the solution:
  98. g = [1.5 + x(1)*x(2) - x(1) - x(2); - x(1)*x(2) - 10]
  99. pause % Strike any key to continue

  100. % The total number of function evaluations was:
  101. options(10)
  102. pause % Strike any key for next demo
  103. clc
  104. %##########################################################################
  105. %           DEMO 3: BOUNDED EXAMPLE
  106. %--------------------------------------------------------------------------
  107. %
  108. %   Consider the above problem with additional bound constraints:
  109. %
  110. %                                      2        2
  111. %   minimize  f(x) = exp(x(1)) . (4x(1)  + 2x(2)  + 4x(1).x(2) + 2x(2) + 1)
  112. %
  113. %   subject to  1.5 + x(1).x(2) - x(1) - x(2) <= 0
  114. %                   - x(1).x(2)               <= 10
  115. %
  116. %          and                    x(1)        >= 0
  117. %                                        x(2) >= 0
  118. %
  119. %--------------------------------------------------------------------------
  120. pause % Strike any key to continue

  121. % Objective Function and constraints

  122. funf = 'f = exp(x(1)) * (4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1);';
  123. fung = 'g = [1.5 + x(1)*x(2) - x(1) - x(2); -x(1)*x(2) - 10];';
  124. fun = [funf fung];

  125. pause % Strike any key to continue

  126. % Again, make a guess at the solution
  127.   
  128. x0 = [-1 1];

  129. pause % Strike any key to continue

  130. % Use default parameters

  131. options = [];

  132. pause % Strike any key to continue

  133. % This time we will use the bounded syntax:
  134. % X = CONSTR(`FUN', X0, OPTIONS, VLB, VUB)

  135. vlb = zeros(1,2); % Lower bounds X >= 0
  136. vub = [];         % No upper bounds

  137. pause % Strike any key to start the optimization

  138. [x, options] = constr(fun, x0, options, vlb, vub);

  139. % The solution to this problem has been found at:
  140. x
  141. pause % Strike any key to continue

  142. % The function value at the solution is:
  143. options(8)
  144. pause % Strike any key to continue

  145. % The constraint values at the solution are:
  146. g = [1.5 + x(1)*x(2) - x(1) - x(2); - x(1)*x(2) - 10]
  147. pause % Strike any key to continue

  148. % The total number of function evaluations was:
  149. options(10)
  150. pause % Strike any key for next demo
  151. clc
  152. %##########################################################################
  153. %           DEMO 4: USER-SUPPLIED GRADIENTS
  154. %--------------------------------------------------------------------------
  155. %  The above problem can be solved more efficiently and accurately if
  156. %  gradients are supplied by the user. This demo shows how this may be
  157. %  performed.
  158. %
  159. %  Again the problem is:
  160. %                                      2        2
  161. %   minimize  f(x) = exp(x(1)) . (4x(1)  + 2x(2)  + 4x(1).x(2) + 2x(2) + 1)
  162. %
  163. %   subject to  1.5 + x(1).x(2) - x(1) - x(2) <= 0
  164. %                   - x(1).x(2)               <= 10
  165. %
  166. %--------------------------------------------------------------------------
  167. pause % Strike any key to continue

  168. % Objective function and constraints

  169. funf = 'f = exp(x(1)) * (4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1);';
  170. fung = 'g = [1.5 + x(1)*x(2) - x(1) - x(2); -x(1)*x(2) - 10];';
  171. fun = [funf fung];

  172. pause % Strike any key to continue

  173. % Make a guess at the solution:

  174. x0 = [-1 1];

  175. pause % Strike any key to continue

  176. % Use default parameters

  177. options = [];

  178. pause % Strike any key to continue

  179. % Partial derivatives

  180. dfdx1 = 'exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1)+4*exp(x(1))*(2*x(1)+x(2));';
  181. dfdx2 = '4*exp(x(1))*(x(1)+x(2)+0.5);';
  182. dfdg = '[x(2)-1, -x(2); x(1)-1, -x(1)];';
  183. grad = ['df=[',dfdx1, dfdx2,']; dg=', dfdg];

  184. pause % Strike any key to start the optimization

  185. [x, options] = constr(fun, x0, options, [], [], grad);

  186. % As before the solution to this problem has been found at:
  187. x
  188. pause % Strike any key to continue

  189. % The function value at the solution is:
  190. options(8)
  191. pause % Strike any key to continue

  192. % Both the constraints are active at the solution:
  193. g = [1.5 + x(1)*x(2) - x(1) - x(2); - x(1)*x(2) - 10]
  194. pause % Strike any key to continue

  195. % The total number of function evaluations was:
  196. options(10)

  197. pause % Strike any key for next demo
  198. clc
  199. %##########################################################################
  200. %           DEMO 5: EQUALITY CONSTRAINED EXAMPLE
  201. %--------------------------------------------------------------------------
  202. %
  203. %   Consider the above problem with an additional equality constraint:
  204. %
  205. %                                      2        2
  206. %   minimize  f(x) = exp(x(1)) . (4x(1)  + 2x(2)  + 4x(1).x(2) + 2x(2) + 1)
  207. %
  208. %   subject to                    x(1) + x(2)  = 0
  209. %               1.5 + x(1).x(2) - x(1) - x(2) <= 0
  210. %                   - x(1).x(2)               <= 10
  211. %
  212. %--------------------------------------------------------------------------
  213. pause % Strike any key to continue

  214. % Objective function and constraints

  215. funf = 'f = exp(x(1)) * (4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1);';
  216. fung = 'g = [x(1) + x(2); 1.5 + x(1)*x(2) - x(1) - x(2); -x(1)*x(2) - 10];';
  217. fun = [funf fung];

  218. pause % Strike any key to continue

  219. % Again, make a guess at the solution

  220. x0 = [-1 1];

  221. pause % Strike any key to continue

  222. % This time we will indicate the number of equality constraints
  223. % by setting options(13) to the number of constraints.  The equality
  224. % constraints must be contained the in first few elements of g.

  225. % One equality constraint

  226. clear options
  227. options(13) = 1;

  228. pause % Strike any key to start the optimization

  229. [x, options] = constr(fun, x0, options);

  230. % The solution to this problem has been found at:
  231. x
  232. pause % Strike any key to continue

  233. % The function value at the solution is:
  234. options(8)
  235. pause % Strike any key to continue

  236. % The constraint values at the solution are:
  237. g = [x(1) + x(2); 1.5 + x(1)*x(2) - x(1) - x(2); -x(1)*x(2) - 10]
  238. pause % Strike any key to continue

  239. % The total number of function evaluations was:
  240. options(10)

  241. pause % Strike any key for next demo
  242. clc
  243. %##########################################################################
  244. %           DEMO 6: CHANGING THE DEFAULT SETTINGS
  245. %--------------------------------------------------------------------------
  246. %
  247. %   Consider the original unconstrained problem:
  248. %
  249. %                                      2        2
  250. %   minimize  f(x) = exp(x(1)) . (4x(1)  + 2x(2)  + 4x(1).x(2) + 2x(2) + 1)
  251. %
  252. %   This time we will solve it more accurately by overriding the
  253. %   default termination criteria (OPTIONS(2) and OPTIONS(3)).
  254. %--------------------------------------------------------------------------
  255. pause % Strike any key to continue

  256. % The function to be minimized is

  257. fun = 'exp(x(1)) * (4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1)';

  258. pause % Strike any key to continue

  259. % Again, make a guess at the solution:

  260. x0 = [-1 1];

  261. pause % Strike any key to continue

  262. % Override the default termination criteria:
  263. clear options
  264. options(2) = 1e-8;    % Termination tolerance on X
  265. options(3) = 1e-8;    % Termination tolerance on F

  266. pause % Strike any key to start the optimization

  267. [x, options] = fminu(fun, x0, options);

  268. %  The optimizer has found a solution at:
  269. x
  270. pause % Strike any key to continue

  271. %  The function value at the solution is:
  272. options(8)
  273. pause % Strike any key to continue

  274. %  The total number of function evaluations was:
  275. options(10)
  276. %   Note: warning messages may be displayed because of problems
  277. %   in the calculation of finite difference gradients at the
  278. %   solution point. They are an indication that tolerances are overly
  279. %   stringent, however, the optimizer always continues to make steps
  280. %   towards the solution.

  281. pause % Strike any key to continue

  282. % If we want a tabular display of each iteration we can set
  283. % options(1) = 1 as follows:

  284. options = 1;  % Set display parameter to on, the others to default.

  285. pause % Strike any key to start the optimization

  286. [x, options] = fminu(fun, x0, options);

  287. %  The optimizer has found a solution at:
  288. x
  289. pause % Strike any key to continue

  290. %  The function value at the solution is:
  291. options(8)
  292. pause % Strike any key to continue

  293. %  The total number of function evaluations was:
  294. options(10)
  295. %  At each major iteration the table displayed consists of:
  296. %           -   number of function values
  297. %           -   function value
  298. %           -   step length used in the line search
  299. %           -   gradient in the direction of search
  300. %

  301. %------------------------------Demo End------------------------------------
  302. pause % Strike any key for main menu
  303. echo off
  304. disp('End of demo')
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

客服中心
关闭
在线时间:
周一~周五
8:30-17:30
QQ群:
653541906
联系电话:
010-85786021-8017
在线咨询
客服中心

意见反馈|网站地图|手机版|小黑屋|EPS数据狗论坛 ( 京ICP备09019565号-3 )   

Powered by BFIT! X3.4

© 2008-2028 BFIT Inc.

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