type test1 function S=test(x) % Rosenbrock, 1960 % x0 = [-1.2, 1]' % xo = [1, 1]' % S(xo) = 0 % x0=[-0.8 0], d=[8 5] (busca Coggins) S=100*(x(2)-x(1).^2).^2+(1-x(1)).^2; type restr1 function [g,h]=restr(x) % restricao adicionada a funcao de Rosenbrock % x0 = [-1.2, 1]' % xo = [1, 1]' % S(xo) = 0 g=x(1).^2+x(2).^2-2; h=[]; %op=optimset('LargeScale','off','MaxFunEvals',inf,'MaxIter',200,'Display','iter',... % 'TolFun',1e-6,'TolX',1e-6,'LineSearch','quadcubic','TolCon',1e-6); %[x,S,ex,out,lambda]=fmincon('test1',[-1.9 2],[],[],[],[],[],[],'restr1',op) help bfgs Unconstrained optimization using BFGS. [xo,Ot,nS]=bfgs(S,x0,ip,G,method,Lb,Ub,problem,tol,mxit) S: objective function x0: initial point ip: (0) no plot (default), (>0) plot figure ip with pause, (<0) plot figure ip G: gradient vector function method: line-search method: (0) quadratic+cubic (default), (1) cubic Lb, Ub: lower and upper bound vectors to plot (default = x0*(1+/-2)) problem: (-1): minimum (default), (1): maximum tol: tolerance (default = 1e-4) mxit: maximum number of iterations (default = 50*(1+4*~(ip>0))) xo: optimal point Ot: optimal value of S nS: number of objective function evaluations [xo,Ot,nS]=bfgs('test1',[-1.9 2],1) Pause: hit any key to continue... Directional Iteration Func-count f(x) Step-size derivative 1 2 267.62 0.001 -1.62e+006 Pause: hit any key to continue... 2 8 6.03794 0.000360666 -1.04e+004 Pause: hit any key to continue... 3 15 5.98743 0.0965277 -0.000508 Pause: hit any key to continue... 4 21 5.40845 4.68404 -0.0605 Pause: hit any key to continue... 5 27 4.92023 0.712063 -0.445 Pause: hit any key to continue... 6 34 3.24245 2.43591 -0.361 Pause: hit any key to continue... 7 40 2.51677 2.65295 -0.0818 Pause: hit any key to continue... 8 46 2.16662 0.168702 -0.809 Pause: hit any key to continue... 9 52 1.81218 0.288521 -0.105 Pause: hit any key to continue... 10 58 1.69953 0.913139 -0.00622 Pause: hit any key to continue... 11 65 0.939367 3.28708 -0.108 Pause: hit any key to continue... 12 71 0.841775 1.23735 -0.000432 Pause: hit any key to continue... 13 77 0.656054 0.187819 -0.792 Pause: hit any key to continue... 14 83 0.583785 0.0987556 -0.194 Pause: hit any key to continue... 15 89 0.532741 1.63963 -0.00243 Pause: hit any key to continue... 16 96 0.229036 5.00434 -0.0219 Pause: hit any key to continue... 17 102 0.208322 0.415223 -1.45e-007 Pause: hit any key to continue... 18 108 0.139441 0.912904 -0.0411 Pause: hit any key to continue... 19 114 0.118122 0.457061 -0.0045 Pause: hit any key to continue... 20 121 0.0380167 3.37376 -0.00796 Pause: hit any key to continue... 21 127 0.0301717 0.433759 -1.02e-007 Pause: hit any key to continue... 22 133 0.0133286 1.24805 -0.00486 Pause: hit any key to continue... 23 139 0.00790343 1.01501 -0.000276 Pause: hit any key to continue... 24 145 0.00178547 1.18024 -6.01e-007 Pause: hit any key to continue... 25 151 0.000339992 0.95937 -3.4e-005 Pause: hit any key to continue... 26 158 5.48253e-006 2.14957 -2.06e-006 Optimization terminated successfully: Current search direction is a descent direction, and magnitude of directional derivative in search direction less than 2*options.TolFun Pause: hit any key to continue... xo = 1.0009 1.0016 Ot = 5.4825e-006 nS = 159 help sqp Constrained optimization using SQP to solve problems like: min S(x) subject to: g(x) <= 0, h(x) = 0 (nonlinear constraints) x Lb <= x <= Ub [xo,Ot,nS,lambda]=sqp(S,Res,x0,ip,Gr,linesearch,Lb,Ub,problem,tol,mxit) S: objective function x0: initial point ip: (0) no plot (default), (>0) plot figure ip with pause, (<0) plot figure ip Res: contraint function returning [g(x),h(x)] Gr: gradient vector function of S(x) linesearch: (0) quadratic+cubic (default), (1) cubic Lb, Ub: lower and upper bound vectors [used also to plot (plot default = x0*(1+/-2))] problem: (-1): minimum (default), (1): maximum tol: tolerance (default = 1e-6) mxit: maximum number of iterations (default = 50*(1+4*~(ip>0))) xo: optimal point Ot: optimal value of S nS: number of objective function evaluations lambda: Lagrange multipliers cd v:\cursos\pos\otimiza\aulas [xo,Ot,nS,lambda]=sqp('test1','restr1',[-1.9 2],1) Pause: hit any key to continue... max Directional Iter F-count f(x) constraint Step-size derivative Procedure 1 3 267.62 5.61 1 -1.62e+006 Pause: hit any key to continue... 2 16 565.61 5.162 0.00195 -6.32e+005 Pause: hit any key to continue... 3 29 397.898 3.254 0.00195 -1.77e+003 Pause: hit any key to continue... 4 33 141.346 1.367 1 -136 Pause: hit any key to continue... 5 37 37.3103 0.2034 1 -75.1 Pause: hit any key to continue... 6 41 0.0553945 -0.3941 1 -0.111 Pause: hit any key to continue... 7 45 0.00523106 -0.381 1 -0.000944 Pause: hit any key to continue... 8 49 0.00475214 -0.3818 1 -1.08e-005 Hessian modified Pause: hit any key to continue... 9 53 0.00474145 -0.3814 1 -0.000942 Hessian modified Pause: hit any key to continue... 10 57 0.00404631 -0.3469 1 -0.00109 Hessian modified Pause: hit any key to continue... 11 61 0.0032356 -0.2895 1 -0.00147 Hessian modified Pause: hit any key to continue... 12 65 0.00215099 -0.2059 1 -0.00185 Hessian modified Pause: hit any key to continue... 13 69 0.000809399 -0.1128 1 -0.00121 Hessian modified Pause: hit any key to continue... 14 73 8.17019e-005 -0.05096 1 -0.000128 Hessian modified Pause: hit any key to continue... 15 77 4.76847e-006 -0.0113 1 -8.81e-006 Hessian modified Pause: hit any key to continue... 16 81 3.36076e-008 -0.0009993 1 -6.7e-008 Hessian modified Optimization terminated successfully: Magnitude of directional derivative in search direction less than 2*options.TolFun and maximum constraint violation is less than options.TolCon Active Constraints: 1 Pause: hit any key to continue... xo = 0.9999 0.9998 Ot = 8.7239e-009 nS = 84 lambda = lower: [2x1 double] upper: [2x1 double] eqlin: [1x0 double] eqnonlin: [1x0 double] ineqlin: [1x0 double] ineqnonlin: 7.2543e-007 help qr QR Orthogonal-triangular decomposition. [Q,R] = QR(A) produces an upper triangular matrix R of the same dimension as A and a unitary matrix Q so that A = Q*R. [Q,R,E] = QR(A) produces a permutation matrix E, an upper triangular R and a unitary Q so that A*E = Q*R. The column permutation E is chosen so that abs(diag(R)) is decreasing. [Q,R] = QR(A,0) produces the "economy size" decomposition. If A is m-by-n with m > n, then only the first n columns of Q are computed. [Q,R,E] = QR(A,0) produces an "economy size" decomposition in which E is a permutation vector, so that Q*R = A(:,E). The column permutation E is chosen so that abs(diag(R)) is decreasing. By itself, QR(A) returns the output of LINPACK'S ZQRDC routine. TRIU(QR(A)) is R. For sparse matrices, QR can compute a "Q-less QR decomposition", which has the following slightly different behavior. R = QR(A) returns only R. Note that R = chol(A'*A). [Q,R] = QR(A) returns both Q and R, but Q is often nearly full. [C,R] = QR(A,B), where B has as many rows as A, returns C = Q'*B. R = QR(A,0) and [C,R] = QR(A,B,0) produce economy size results. The sparse version of QR does not do column permutations. The full version of QR does not return C. The least squares approximate solution to A*x = b can be found with the Q-less QR decomposition and one step of iterative refinement: x = R\(R'\(A'*b)) r = b - A*x e = R\(R'\(A'*r)) x = x + e; See also LU, NULL, ORTH, QRDELETE, QRINSERT, QRUPDATE. a=rand(3) a = 0.9501 0.4860 0.4565 0.2311 0.8913 0.0185 0.6068 0.7621 0.8214 [q,r]=qr(a) q = -0.8256 0.4039 -0.3940 -0.2008 -0.8629 -0.4637 -0.5273 -0.3037 0.7936 r = -1.1508 -0.9821 -0.8137 0 -0.8043 -0.0810 0 0 0.4634 q'*a ans = -1.1508 -0.9821 -0.8137 -0.0000 -0.8043 -0.0810 0 0 0.4634 help qr QR Orthogonal-triangular decomposition. [Q,R] = QR(A) produces an upper triangular matrix R of the same dimension as A and a unitary matrix Q so that A = Q*R. [Q,R,E] = QR(A) produces a permutation matrix E, an upper triangular R and a unitary Q so that A*E = Q*R. The column permutation E is chosen so that abs(diag(R)) is decreasing. [Q,R] = QR(A,0) produces the "economy size" decomposition. If A is m-by-n with m > n, then only the first n columns of Q are computed. [Q,R,E] = QR(A,0) produces an "economy size" decomposition in which E is a permutation vector, so that Q*R = A(:,E). The column permutation E is chosen so that abs(diag(R)) is decreasing. By itself, QR(A) returns the output of LINPACK'S ZQRDC routine. TRIU(QR(A)) is R. For sparse matrices, QR can compute a "Q-less QR decomposition", which has the following slightly different behavior. R = QR(A) returns only R. Note that R = chol(A'*A). [Q,R] = QR(A) returns both Q and R, but Q is often nearly full. [C,R] = QR(A,B), where B has as many rows as A, returns C = Q'*B. R = QR(A,0) and [C,R] = QR(A,B,0) produce economy size results. The sparse version of QR does not do column permutations. The full version of QR does not return C. The least squares approximate solution to A*x = b can be found with the Q-less QR decomposition and one step of iterative refinement: x = R\(R'\(A'*b)) r = b - A*x e = R\(R'\(A'*r)) x = x + e; See also LU, NULL, ORTH, QRDELETE, QRINSERT, QRUPDATE. q*r ans = 0.9501 0.4860 0.4565 0.2311 0.8913 0.0185 0.6068 0.7621 0.8214 a a = 0.9501 0.4860 0.4565 0.2311 0.8913 0.0185 0.6068 0.7621 0.8214 help lu LU LU factorization. [L,U] = LU(X) stores an upper triangular matrix in U and a "psychologically lower triangular matrix" (i.e. a product of lower triangular and permutation matrices) in L, so that X = L*U. X must be square. [L,U,P] = LU(X) returns lower triangular matrix L, upper triangular matrix U, and permutation matrix P so that P*X = L*U. LU(X), with one output argument, returns the output from LINPACK'S ZGEFA routine. LU(X,THRESH) controls pivoting in sparse matrices, where THRESH is a pivot threshold in [0,1]. Pivoting occurs when the diagonal entry in a column has magnitude less than THRESH times the magnitude of any sub-diagonal entry in that column. THRESH = 0 forces diagonal pivoting. THRESH = 1 is the default. See also LUINC, QR, RREF. help chol CHOL Cholesky factorization. CHOL(X) uses only the diagonal and upper triangle of X. The lower triangular is assumed to be the (complex conjugate) transpose of the upper. If X is positive definite, then R = CHOL(X) produces an upper triangular R so that R'*R = X. If X is not positive definite, an error message is printed. [R,p] = CHOL(X), with two output arguments, never produces an error message. If X is positive definite, then p is 0 and R is the same as above. But if X is not positive definite, then p is a positive integer. When X is full, R is an upper triangular matrix of order q = p-1 so that R'*R = X(1:q,1:q). When X is sparse, R is an upper triangular matrix of size q-by-n so that the L-shaped region of the first q rows and first q columns of R'*R agree with those of X. See also CHOLINC, CHOLUPDATE. restr what M-files in the current directory v:\cursos\pos\otimiza\aulas CATALIS ex_karma gtest13 nlconst test14 EXTRATOR ex_qp1 gtest2 nlp_internal test15 LUCRO ex_qp2 gtest9 powell test16 MINQUA ex_qp3 hkjeeves qpsub test17 MODELO ex_swarm htest1 refino test18 OPT_RES fmincon1 htest2 restr test19 PLANOS fminunc1 interior restr1 test2 READ2 fminusub karmarkar restr14 test20 SEMIDEF fun lmarqua restr15 test3 SMODELO gmilp1 lp_nlp restr16 test4 aurea gminlp1 milp1 restr17 test5 bandem1 gminlp2 minlp restr20 test6 bfgs gminlp3 minlp1 rosembr test7 bracket gminlp4 minlp2 set1 test8 buscarnd gminlp5 minlp3 setoptim test9 cgrad gminlp6 minlp4 sqp univar checkbounds gmodelagem minlp5 steepdes varmetr coggins gmurray minlp6 swarm writearq compdir grad minlpn test1 xplot complex grg modelagem test10 dfp gtest1 newton test11 dual gtest10 newton_h test12 ex2_karm gtest12 newtont test13 type restr14 function [g,h]=restr(x) % Edgar & Himmelblau, 1988 % x0 = [-1, -2]' % xo = [0.8229, 0.9114]' % S(xo) = 1.3935 g=(x(1).^2)/4+x(2).^2-1; h=x(1)-2*x(2)+1; type restr15 function [g,h]=restr(x) % Edgar & Himmelblau, 1988 % com restricao: % x0 = [-1, -2]' % xo = [5.1663, -3.9157]' , minimo local % S(xo) = 202.6 % x0 = [0, 6]' % xo=[2.3537, 3.1157]' , minimo global % S(xo) = -4.9606 g=18-5*x(1)-2*x(2); h=-2*x(1)+x(2).^2-5; type restr16 function [g,h]=restr(x) % lucro de um processo catalitico % x(1) --> catalisador (fracao molar) % x(2) --> vapor (razao vapor/produto) lucro=50*(0.1+0.3*x(1)+1e-4*x(2)-1e-4*x(1).*x(2))- ... (2+10*x(1)+20*x(1).^2)-(1+0.003*x(2)+2e-6*x(2).^2); S=-lucro; g(1)=S; g(2)=-x(1); g(3)=-x(2); g(4)=x(1)-1; h=[]; type restr17 function [g,h]=restr(x) % Edgar & Himmelblau, 1988 % maximo global: % x0 = [1, 1]' ou x0 = [-1, -1]' % xo = [3.5355 3.5355]' ou xo = [-3.5355 -3.5355]' % S(xo) = 12.5 % ponto sela: % xo=[0 0]' % S(xo) = 0 % minimo global: % x0 = [-1, 1]' ou x0 = [1, -1]' % xo = [-3.5355 3.5355]' ou xo = [3.5355 -3.5355]' % S(xo) = -12.5 g=x(1).^2+x(2).^2-25; h=[]; type test17 function [S,G,H]=test(x) % Edgar & Himmelblau, 1988 % minimo local: % x0 = [1, 1]' ou x0 = [-1, -1]' % xo = [3.5355 3.5355]' ou xo = [-3.5355 -3.5355]' % S(xo) = 0 % minimo global: % x0 = [-1, 1]' ou x0 = [1, -1]' % xo = [-3.5355 3.5355]' ou xo = [3.5355 -3.5355]' % S(xo) = -12.5 %op=optimset('LargeScale','off','MaxFunEvals',inf,'MaxIter',200,'Display','iter',... % 'TolFun',1e-6,'TolX',1e-6,'LineSearch','quadcubic','TolCon',1e-6); %op=optimset('LargeScale','off','MaxFunEvals',inf,'MaxIter',200,'Display','iter',... % 'TolFun',1e-6,'TolX',1e-6,'LineSearch','quadcubic','TolCon',1e-6,'GradObj','on', ... % 'HessUpdate','bfgs'); %[x,S,ex,out,lambda,grad,hess]=fmincon('test17',[1 1],[],[],[],[],[],[],'restr17',op) S=x(1).*x(2); if nargout > 1 G=[x(2); x(1)]; % gradiente if nargout > 2 H = [0 1;1 0]; end end help sqp Constrained optimization using SQP to solve problems like: min S(x) subject to: g(x) <= 0, h(x) = 0 (nonlinear constraints) x Lb <= x <= Ub [xo,Ot,nS,lambda]=sqp(S,Res,x0,ip,Gr,linesearch,Lb,Ub,problem,tol,mxit) S: objective function x0: initial point ip: (0) no plot (default), (>0) plot figure ip with pause, (<0) plot figure ip Res: contraint function returning [g(x),h(x)] Gr: gradient vector function of S(x) linesearch: (0) quadratic+cubic (default), (1) cubic Lb, Ub: lower and upper bound vectors [used also to plot (plot default = x0*(1+/-2))] problem: (-1): minimum (default), (1): maximum tol: tolerance (default = 1e-6) mxit: maximum number of iterations (default = 50*(1+4*~(ip>0))) xo: optimal point Ot: optimal value of S nS: number of objective function evaluations lambda: Lagrange multipliers [xo,Ot,nS,lambda]=sqp('test17','restr17',[1 1],1,[],[],[-6 -6],[6 6]) Pause: hit any key to continue... max Directional Iter F-count f(x) constraint Step-size derivative Procedure 1 3 1 -5 1 -2 Pause: hit any key to continue... 2 7 2.52482e-017 -6 1 -5.05e-017 Optimization terminated successfully: Search direction less than 2*options.TolX and maximum constraint violation is less than options.TolCon No Active Constraints Pause: hit any key to continue... xo = 1.0e-016 * -0.2525 -0.2525 Ot = 6.3747e-034 nS = 9 lambda = lower: [2x1 double] upper: [2x1 double] eqlin: [0x1 double] eqnonlin: [0x1 double] ineqlin: [0x1 double] ineqnonlin: 0 type test17 function [S,G,H]=test(x) % Edgar & Himmelblau, 1988 % minimo local: % x0 = [1, 1]' ou x0 = [-1, -1]' % xo = [3.5355 3.5355]' ou xo = [-3.5355 -3.5355]' % S(xo) = 0 % minimo global: % x0 = [-1, 1]' ou x0 = [1, -1]' % xo = [-3.5355 3.5355]' ou xo = [3.5355 -3.5355]' % S(xo) = -12.5 %op=optimset('LargeScale','off','MaxFunEvals',inf,'MaxIter',200,'Display','iter',... % 'TolFun',1e-6,'TolX',1e-6,'LineSearch','quadcubic','TolCon',1e-6); %op=optimset('LargeScale','off','MaxFunEvals',inf,'MaxIter',200,'Display','iter',... % 'TolFun',1e-6,'TolX',1e-6,'LineSearch','quadcubic','TolCon',1e-6,'GradObj','on', ... % 'HessUpdate','bfgs'); %[x,S,ex,out,lambda,grad,hess]=fmincon('test17',[1 1],[],[],[],[],[],[],'restr17',op) S=x(1).*x(2); if nargout > 1 G=[x(2); x(1)]; % gradiente if nargout > 2 H = [0 1;1 0]; end end setoptim op ??? Undefined function or variable 'op'. who Your variables are: G Y ans opt x1 Ot Z lambda q x2 X a nS r xo opt opt = DerivativeCheck: 'off' Diagnostics: 'off' Display: 'final' GradConstr: 'off' GradObj: 'on' Hessian: 'on' LargeScale: 'on' DiffMaxChange: 0.1000 DiffMinChange: 1.0000e-008 MaxIter: 400 PrecondBandWidth: 0 TolCon: 1.0000e-006 TolFun: 1.0000e-006 TolPCG: 0.1000 TolX: 1.0000e-006 opt opt = DerivativeCheck: 'off' Diagnostics: 'off' Display: 'final' GradConstr: 'off' GradObj: 'on' Hessian: 'on' LargeScale: 'off' DiffMaxChange: 0.1000 DiffMinChange: 1.0000e-008 MaxIter: 400 PrecondBandWidth: 0 TolCon: 1.0000e-006 TolFun: 1.0000e-006 TolPCG: 0.1000 TolX: 1.0000e-006 type test17 function [S,G,H]=test(x) % Edgar & Himmelblau, 1988 % minimo local: % x0 = [1, 1]' ou x0 = [-1, -1]' % xo = [3.5355 3.5355]' ou xo = [-3.5355 -3.5355]' % S(xo) = 0 % minimo global: % x0 = [-1, 1]' ou x0 = [1, -1]' % xo = [-3.5355 3.5355]' ou xo = [3.5355 -3.5355]' % S(xo) = -12.5 %op=optimset('LargeScale','off','MaxFunEvals',inf,'MaxIter',200,'Display','iter',... % 'TolFun',1e-6,'TolX',1e-6,'LineSearch','quadcubic','TolCon',1e-6); %op=optimset('LargeScale','off','MaxFunEvals',inf,'MaxIter',200,'Display','iter',... % 'TolFun',1e-6,'TolX',1e-6,'LineSearch','quadcubic','TolCon',1e-6,'GradObj','on', ... % 'HessUpdate','bfgs'); %[x,S,ex,out,lambda,grad,hess]=fmincon('test17',[1 1],[],[],[],[],[],[],'restr17',op) S=x(1).*x(2); if nargout > 1 G=[x(2); x(1)]; % gradiente if nargout > 2 H = [0 1;1 0]; end end [x,S,ex,out,lambda,grad,hess]=fmincon('test17',[1 1],[],[],[],[],[],[],'restr17',opt) ??? Error using ==> C:\Apps\Matlab\toolbox\optim\private\nlconst Undefined calltype in FMINCON Error in ==> C:\Apps\Matlab\toolbox\optim\fmincon.m On line 413 ==> [X,FVAL,lambda,EXITFLAG,OUTPUT,GRAD,HESSIAN]=... help fmincon FMINCON Finds the constrained minimum of a function of several variables. FMINCON solves problems of the form: min F(X) subject to: A*X <= B, Aeq*X = Beq (linear constraints) X C(X) <= 0, Ceq(X) = 0 (nonlinear constraints) LB <= X <= UB X=FMINCON(FUN,X0,A,B) starts at X0 and finds a minimum X to the function described in FUN, subject to the linear inequalities A*X <= B. X0 may be a scalar, vector or matrix. The function FUN (usually an M-file or inline object) should return a scalar function value F evaluated at X when called with feval: F=feval(FUN,X). X=FMINCON(FUN,X0,A,B,Aeq,Beq) minimizes FUN subject to the linear equalities Aeq*X = Beq as well as A*X <= B. (Set A=[] and B=[] if no inequalities exist.) X=FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB) defines a set of lower and upper bounds on the design variables, X, so that the solution is in the range LB <= X <= UB. Use empty matrices for LB and UB if no bounds exist. Set LB(i) = -Inf if X(i) is unbounded below; set UB(i) = Inf if X(i) is unbounded above. X=FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON) subjects the minimization to the constraints defined in NONLCON. The function NONLCON should return the vectors C and Ceq, representing the nonlinear inequalities and equalities respectively, when called with feval: [C, Ceq] = feval(NONLCON,X). FMINCON minimizes FUN such that C(X)<=0 and Ceq(X)=0. (Set LB=[] and/or UB=[] if no bounds exist.) X=FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,OPTIONS) minimizes with the default optimization parameters replaced by values in the structure OPTIONS, an argument created with the OPTIMSET function. See OPTIMSET for details. Used options are Display, TolX, TolFun, TolCon, DerivativeCheck, Diagnostics, GradObj, GradConstr, Hessian, MaxFunEvals, MaxIter, DiffMinChange and DiffMaxChange, LargeScale, MaxPCGIter, PrecondBandWidth, TolPCG, TypicalX, HessPattern. Use the GradObj option to specify that FUN may be called with two output arguments where the second, G, is the partial derivatives of the function df/dX, at the point X: [F,G] = feval(FUN,X). Use the GradConstr option to specify that NONLCON may be called with four output arguments: [C,Ceq,GC,GCeq] = feval(NONLCON,X) where GC is the partial derivatives of the constraint vector of inequalities C an GCeq is the partial derivatives of the constraint vector of equalities Ceq. Use OPTIONS = [] as a place holder if no options are set. X=FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,OPTIONS,P1,P2,...) passes the problem-dependent parameters P1,P2,... directly to the functions FUN and NONLCON: feval(FUN,X,P1,P2,...) and feval(NONLCON,X,P1,P2,...). Pass empty matrices for A, B, Aeq, Beq, OPTIONS, LB, UB, and NONLCON to use the default values. [X,FVAL]=FMINCON(FUN,X0,...) returns the value of the objective function FUN at the solution X. [X,FVAL,EXITFLAG]=FMINCON(FUN,X0,...) returns a string EXITFLAG that describes the exit condition of FMINCON. If EXITFLAG is: > 0 then FMINCON converged to a solution X. 0 then the maximum number of function evaluations was reached. < 0 then FMINCON did not converge to a solution. [X,FVAL,EXITFLAG,OUTPUT]=FMINCON(FUN,X0,...) returns a structure OUTPUT with the number of iterations taken in OUTPUT.iterations, the number of function evaluations in OUTPUT.funcCount, the algorithm used in OUTPUT.algorithm, the number of CG iterations (if used) in OUTPUT.cgiterations, and the first-order optimality (if used) in OUTPUT.firstorderopt. [X,FVAL,EXITFLAG,OUTPUT,LAMBDA]=FMINCON(FUN,X0,...) returns the Lagrange multipliers at the solution X: LAMBDA.lower for LB, LAMBDA.upper for UB, LAMBDA.ineqlin is for the linear inequalities, LAMBDA.eqlin is for the linear equalities, LAMBDA.ineqnonlin is for the nonlinear inequalities, and LAMBDA.eqnonlin is for the nonlinear equalities. [X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD]=FMINCON(FUN,X0,...) returns the value of the gradient of FUN at the solution X. [X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN]=FMINCON(FUN,X0,...) returns the value of the HESSIAN of FUN at the solution X. See also OPTIMSET, FMINUNC, FMINBND, FMINSEARCH. [x,S,ex,out,lambda,grad,hess]=fmincon('test17',[1 1],[],[],[],[],[],[],'restr17',opt) Optimization terminated successfully: Search direction less than 2*options.TolX and maximum constraint violation is less than options.TolCon No Active Constraints x = 0 0 S = 0 ex = 1 out = iterations: 2 funcCount: 9 stepsize: 1 algorithm: 'medium-scale: SQP, Quasi-Newton, line-search' firstorderopt: [] cgiterations: [] lambda = lower: [2x1 double] upper: [2x1 double] eqlin: [1x0 double] eqnonlin: [1x0 double] ineqlin: [1x0 double] ineqnonlin: 0 grad = 0 0 hess = 1 0 0 1 [xo,Ot,nS,lambda]=sqp('test17','restr17',[1 -1],1,[],[],[-6 -6],[6 6]) Pause: hit any key to continue... max Directional Iter F-count f(x) constraint Step-size derivative Procedure 1 3 -1 -5 1 -2 Pause: hit any key to continue... 2 7 -4 -4 1 -8.5 Hessian modified twice Pause: hit any key to continue... 3 11 -17.0156 9.031 1 4.52 Hessian modified twice Pause: hit any key to continue... 4 15 -12.7996 0.5992 1 0.3 Pause: hit any key to continue... 5 19 -12.5018 0.003581 1 0.00179 Hessian modified Pause: hit any key to continue... 6 23 -12.5 1.286e-007 1 6.41e-008 Hessian modified Optimization terminated successfully: Magnitude of directional derivative in search direction less than 2*options.TolFun and maximum constraint violation is less than options.TolCon Active Constraints: 5 Pause: hit any key to continue... xo = 3.5355 -3.5355 Ot = -12.5000 nS = 25 lambda = lower: [2x1 double] upper: [2x1 double] eqlin: [0x1 double] eqnonlin: [0x1 double] ineqlin: [0x1 double] ineqnonlin: 0.5000 [xo,Ot,nS,lambda]=sqp('test17','restr17',[=1 1],1,[],[],[-6 -6],[6 6]) ??? est17','restr17',[= | Missing "]" or "}" or incomplete expression. [xo,Ot,nS,lambda]=sqp('test17','restr17',[-1 1],1,[],[],[-6 -6],[6 6]) Pause: hit any key to continue... max Directional Iter F-count f(x) constraint Step-size derivative Procedure 1 3 -1 -5 1 -2 Pause: hit any key to continue... 2 7 -4 -4 1 -8.5 Hessian modified twice Pause: hit any key to continue... 3 11 -17.0156 9.031 1 4.52 Hessian modified twice Pause: hit any key to continue... 4 15 -12.7996 0.5992 1 0.3 Pause: hit any key to continue... 5 19 -12.5018 0.003581 1 0.00179 Hessian modified Pause: hit any key to continue... 6 23 -12.5 1.286e-007 1 6.41e-008 Hessian modified Optimization terminated successfully: Magnitude of directional derivative in search direction less than 2*options.TolFun and maximum constraint violation is less than options.TolCon Active Constraints: 5 Pause: hit any key to continue... xo = -3.5355 3.5355 Ot = -12.5000 nS = 25 lambda = lower: [2x1 double] upper: [2x1 double] eqlin: [0x1 double] eqnonlin: [0x1 double] ineqlin: [0x1 double] ineqnonlin: 0.5000 help fminunc FMINUNC Finds the minimum of a function of several variables. X=FMINUNC(FUN,X0) starts at the point X0 and finds a minimum X of the function described in FUN. X0 can be a scalar, vector or matrix. The function FUN (usually an M-file or inline object) should return a scalar function value F evaluated at X when called with feval: F=feval(FUN,X). See the examples below for more about FUN. X=FMINUNC(FUN,X0,OPTIONS) minimizes with the default optimization parameters replaced by values in the structure OPTIONS, an argument created with the OPTIMSET function. See OPTIMSET for details. Used options are Display, TolX, TolFun, DerivativeCheck, Diagnostics, GradObj, HessPattern, LineSearchType, Hessian, HessUpdate, MaxFunEvals, MaxIter, DiffMinChange and DiffMaxChange, LargeScale, MaxPCGIter, PrecondBandWidth, TolPCG, TypicalX. Use the GradObj option to specify that FUN can be called with two output arguments where the second, G, is the partial derivatives of the function df/dX, at the point X: [F,G] = feval(FUN,X). Use Hessian to specify that FUN can be called with three output arguments where the second, G, is the partial derivatives of the function df/dX, and the third H is the 2nd partial derivatives of the function (the Hessian) at the point X: [F,G,H] = feval(FUN,X). The Hessian is only used by the large-scale method, not the line-search method. X=FMINUNC(FUN,X0,OPTIONS,P1,P2,...) passes the problem-dependent parameters P1,P2,... directly to the function FUN, e.g. FUN would be called using feval as in: feval(FUN,X,P1,P2,...). Pass an empty matrix for OPTIONS to use the default values. [X,FVAL]=FMINUNC(FUN,X0,...) returns the value of the objective function FUN at the solution X. [X,FVAL,EXITFLAG]=FMINUNC(FUN,X0,...) returns a string EXITFLAG that describes the exit condition of FMINUNC. If EXITFLAG is: > 0 then FMINUNC converged to a solution X. 0 then the maximum number of function evaluations was reached. < 0 then FMINUNC did not converge to a solution. [X,FVAL,EXITFLAG,OUTPUT]=FMINUNC(FUN,X0,...) returns a structure OUTPUT with the number of iterations taken in OUTPUT.iterations, the number of function evaluations in OUTPUT.funcCount, the algorithm used in OUTPUT.algorithm, the number of CG iterations (if used) in OUTPUT.cgiterations, and the first-order optimality (if used) in OUTPUT.firstorderopt. [X,FVAL,EXITFLAG,OUTPUT,GRAD]=FMINUNC(FUN,X0,...) returns the value of the gradient of FUN at the solution X. [X,FVAL,EXITFLAG,OUTPUT,GRAD,HESSIAN]=FMINUNC(FUN,X0,...) returns the value of the Hessian of the objective function FUN at the solution X. Examples Minimize the one dimensional function f(x) = sin(x) + 3: To use an M-file, i.e. FUN = 'myfun', create a file myfun.m: function f = myfun(x) f = sin(x)+3; Then call FMINUNC to find a minimum of FUN near 2: x = fminunc('myfun',2) To minimize this function with the gradient provided, modify the m-file myfun.m so the gradient is the second output argument: function [f,g]= myfun(x) f = sin(x) + 3; g = cos(x); and indicate the gradient value is available by creating an options structure with OPTIONS.GradObj set to 'on' (using OPTIMSET): options = optimset('GradObj','on'); x = fminunc('myfun',2,options); To minimize the function f(x) = sin(x) + 3 using an inline object: f = inline('sin(x)+3'); x = fminunc(f,2); To use inline objects for the function and gradient, FUN is a cell array of two inline objects where the first is the objective and the second is the gradient of the objective: options = optimset('GradObj','on'); x = fminunc({ inline('sin(x)+3'), inline('cos(x)') } ,2,options); setoptim opt opt = DerivativeCheck: 'off' Diagnostics: 'off' Display: 'final' GradObj: 'on' Hessian: 'on' HessUpdate: 'bfgs' LargeScale: 'off' LineSearchType: 'quadcubic' DiffMaxChange: 0.1000 DiffMinChange: 1.0000e-008 MaxIter: 400 PrecondBandWidth: 0 TolFun: 1.0000e-006 TolPCG: 0.1000 TolX: 1.0000e-006 [X,FVAL,EXITFLAG,OUTPUT,GRAD,HESSIAN]=FMINUNC('test17',[1 1],opt) Warning: Line-search algorithm requested (OPTIONS.LargeScale = 0) so user-provided Hessian will be ignored. > In C:\Apps\Matlab\toolbox\optim\fminunc.m at line 170 Optimization terminated successfully: Search direction less than 2*options.TolX X = 0 0 FVAL = 0 EXITFLAG = 1 OUTPUT = iterations: 2 funcCount: 5 stepsize: 1 firstorderopt: 0 algorithm: 'medium-scale: Quasi-Newton line search' GRAD = 0 0 HESSIAN = 1 0 0 1 setoptim [X,FVAL,EXITFLAG,OUTPUT,GRAD,HESSIAN]=FMINUNC('test17',[1 1],opt) Optimization terminated successfully: First-order optimality less than OPTIONS.TolFun, and no negative/zero curvature detected X = 0 0 FVAL = 0 EXITFLAG = 1 OUTPUT = iterations: 2 funcCount: 2 cgiterations: 1 firstorderopt: 0 algorithm: 'large-scale: trust-region Newton' GRAD = 0 0 HESSIAN = 0 1 1 0 type test14 function S=test(x) % Edgar & Himmelblau, 1988 % x0 = [-1, -2]' % sem restricao: % xo = [2, 1]' % S(xo) = 0 % com restricao: % xo = [0.8229, 0.9114]' % S(xo) = 1.3935 S=(x(1)-2).^2+(x(2)-1).^2; [xo,Ot,nS,lambda]=sqp('test14','restr14',[-1 -2],1,[],[],[-3 -3],[3 3]) Pause: hit any key to continue... max Directional Iter F-count f(x) constraint Step-size derivative Procedure 1 3 18 4 1 -48 Pause: hit any key to continue... 2 7 2 5.25 1 -4.5 Pause: hit any key to continue... 3 11 0.3125 1.125 1 0.44 Pause: hit any key to continue... 4 15 1.14427 0.1487 1 0.226 Pause: hit any key to continue... 5 19 1.38353 0.005398 1 0.0099 Pause: hit any key to continue... 6 23 1.39345 8.274e-006 1 1.53e-005 Hessian modified Pause: hit any key to continue... 7 27 1.39346 2.599e-011 1 3.78e-011 Hessian modified twice Optimization terminated successfully: Search direction less than 2*options.TolX and maximum constraint violation is less than options.TolCon Active Constraints: 1 6 Pause: hit any key to continue... xo = 0.8229 0.9114 Ot = 1.3935 nS = 29 lambda = lower: [2x1 double] upper: [2x1 double] eqlin: [0x1 double] eqnonlin: 1.5944 ineqlin: [0x1 double] ineqnonlin: 1.8465 type test14 function S=test(x) % Edgar & Himmelblau, 1988 % x0 = [-1, -2]' % sem restricao: % xo = [2, 1]' % S(xo) = 0 % com restricao: % xo = [0.8229, 0.9114]' % S(xo) = 1.3935 S=(x(1)-2).^2+(x(2)-1).^2; type test16 function S=test(x) % lucro de um processo catalitico % x(1) --> catalisador (fracao molar) % x(2) --> vapor (razao vapor/produto) lucro=50*(0.1+0.3*x(1)+1e-4*x(2)-1e-4*x(1)*x(2))- ... (2+10*x(1)+20*x(1)^2)-(1+0.003*x(2)+2e-6*x(2)^2); S=-lucro; type restr16 function [g,h]=restr(x) % lucro de um processo catalitico % x(1) --> catalisador (fracao molar) % x(2) --> vapor (razao vapor/produto) lucro=50*(0.1+0.3*x(1)+1e-4*x(2)-1e-4*x(1).*x(2))- ... (2+10*x(1)+20*x(1).^2)-(1+0.003*x(2)+2e-6*x(2).^2); S=-lucro; g(1)=S; g(2)=-x(1); g(3)=-x(2); g(4)=x(1)-1; h=[]; type test16 function S=test(x) % lucro de um processo catalitico % x(1) --> catalisador (fracao molar) % x(2) --> vapor (razao vapor/produto) lucro=50*(0.1+0.3*x(1)+1e-4*x(2)-1e-4*x(1)*x(2))- ... (2+10*x(1)+20*x(1)^2)-(1+0.003*x(2)+2e-6*x(2)^2); S=-lucro; [xo,Ot,nS,lambda]=sqp('test16','restr16',[0.1 100],1,[],[],[0 0],[1 200]) Pause: hit any key to continue... max Directional Iter F-count f(x) constraint Step-size derivative Procedure 1 3 -2.43 -0.1 1 -0.25 Pause: hit any key to continue... 2 12 -2.43293 -0.1156 0.0313 -0.000392 Pause: hit any key to continue... 3 16 -2.43313 -0.1125 1 -1.08e-006 Hessian modified Optimization terminated successfully: Magnitude of directional derivative in search direction less than 2*options.TolFun and maximum constraint violation is less than options.TolCon No Active Constraints Pause: hit any key to continue... xo = 0.1125 100.0021 Ot = -2.4331 nS = 18 lambda = lower: [2x1 double] upper: [2x1 double] eqlin: [0x1 double] eqnonlin: [0x1 double] ineqlin: [0x1 double] ineqnonlin: [4x1 double] [xo,Ot,nS,lambda]=sqp('test16','restr16',[0.1 160],1,[],[],[0 0],[1 200]) Pause: hit any key to continue... max Directional Iter F-count f(x) constraint Step-size derivative Procedure 1 3 -2.4888 -0.1 1 -0.04 Pause: hit any key to continue... 2 12 -2.48927 -0.1063 0.0313 -6.32e-005 Pause: hit any key to continue... 3 16 -2.4893 -0.105 1 -7.13e-007 Hessian modified Optimization terminated successfully: Magnitude of directional derivative in search direction less than 2*options.TolFun and maximum constraint violation is less than options.TolCon No Active Constraints Pause: hit any key to continue... xo = 0.1050 160.0017 Ot = -2.4893 nS = 18 lambda = lower: [2x1 double] upper: [2x1 double] eqlin: [0x1 double] eqnonlin: [0x1 double] ineqlin: [0x1 double] ineqnonlin: [4x1 double] [xo,Ot,nS,lambda]=sqp('test16','restr16',[1 160],1,[],[],[0 0],[1 200]) Pause: hit any key to continue... max Directional Iter F-count f(x) constraint Step-size derivative Procedure 1 3 13.5312 13.53 1 -35.8 Pause: hit any key to continue... 2 7 -2.2688 -5.025e-009 1 -0.441 Pause: hit any key to continue... 3 11 -2.4893 -0.105 1 -6.97e-007 Optimization terminated successfully: Magnitude of directional derivative in search direction less than 2*options.TolFun and maximum constraint violation is less than options.TolCon No Active Constraints Pause: hit any key to continue... xo = 0.1050 159.9984 Ot = -2.4893 nS = 13 lambda = lower: [2x1 double] upper: [2x1 double] eqlin: [0x1 double] eqnonlin: [0x1 double] ineqlin: [0x1 double] ineqnonlin: [4x1 double] [xo,Ot,nS,lambda]=sqp('test16','restr16',[1 150],1,[],[],[0 0],[1 200]) Pause: hit any key to continue... max Directional Iter F-count f(x) constraint Step-size derivative Procedure 1 3 13.495 13.49 1 -35.8 Pause: hit any key to continue... 2 7 -2.25499 -5.025e-009 1 -0.452 Pause: hit any key to continue... 3 11 -2.48078 -0.1063 1 -7.55e-007 Optimization terminated successfully: Magnitude of directional derivative in search direction less than 2*options.TolFun and maximum constraint violation is less than options.TolCon No Active Constraints Pause: hit any key to continue... xo = 0.1063 149.9985 Ot = -2.4808 nS = 13 lambda = lower: [2x1 double] upper: [2x1 double] eqlin: [0x1 double] eqnonlin: [0x1 double] ineqlin: [0x1 double] ineqnonlin: [4x1 double] [xo,Ot,nS,lambda]=sqp('test16','restr16',[1 150],1,[],[],[0 0],[1 2000]) Pause: hit any key to continue... max Directional Iter F-count f(x) constraint Step-size derivative Procedure 1 3 13.495 13.49 1 -35.8 Pause: hit any key to continue... 2 7 -2.25499 -5.025e-009 1 -0.452 Pause: hit any key to continue... 3 11 -2.48078 -0.1063 1 -7.55e-007 Optimization terminated successfully: Magnitude of directional derivative in search direction less than 2*options.TolFun and maximum constraint violation is less than options.TolCon No Active Constraints Pause: hit any key to continue... xo = 0.1063 149.9985 Ot = -2.4808 nS = 13 lambda = lower: [2x1 double] upper: [2x1 double] eqlin: [0x1 double] eqnonlin: [0x1 double] ineqlin: [0x1 double] ineqnonlin: [4x1 double] help univar Unconstrained optimization using univariate search (Coggins and Gold Section). [xo,Ot,nS]=univar(S,x0,ip,method,Lb,Ub,problem,tol,mxit) S: objective function x0: initial point ip: (0): no plot (default), (>0) plot figure ip with pause, (<0) plot figure ip method: (0) Coggins (default), (1): Golden Section Lb, Ub: lower and upper bound vectors to plot (default = x0*(1+/-2)) problem: (-1): minimum (default), (1): maximum tol: tolerance (default = 1e-4) mxit: maximum number of stages (default = 50*(1+4*~(ip>0))) xo: optimal point Ot: optimal value of S nS: number of objective function evaluations [xo,Ot,nS]=univar('test16',[1 150],1,[],[0 0],[1 2000]) Pause: hit any key to continue... Pause: hit any key to continue... Pause: hit any key to continue... Pause: hit any key to continue... Pause: hit any key to continue... Pause: hit any key to continue... Pause: hit any key to continue... Pause: hit any key to continue... Pause: hit any key to continue... Pause: hit any key to continue... Pause: hit any key to continue... Pause: hit any key to continue... Pause: hit any key to continue... xo = 0.0741 407.4037 Ot = -2.5926 nS = 146 help bfgs Unconstrained optimization using BFGS. [xo,Ot,nS]=bfgs(S,x0,ip,G,method,Lb,Ub,problem,tol,mxit) S: objective function x0: initial point ip: (0) no plot (default), (>0) plot figure ip with pause, (<0) plot figure ip G: gradient vector function method: line-search method: (0) quadratic+cubic (default), (1) cubic Lb, Ub: lower and upper bound vectors to plot (default = x0*(1+/-2)) problem: (-1): minimum (default), (1): maximum tol: tolerance (default = 1e-4) mxit: maximum number of iterations (default = 50*(1+4*~(ip>0))) xo: optimal point Ot: optimal value of S nS: number of objective function evaluations [xo,Ot,nS]=bfgs('test16',[1 140],1,[],[],[0 0],[1 2000]) Pause: hit any key to continue... Directional Iteration Func-count f(x) Step-size derivative 1 2 13.4592 0.0211209 -1.27e+003 Pause: hit any key to continue... 2 8 -2.47192 0.025 -3.13e-006 Optimization terminated successfully: Current search direction is a descent direction, and magnitude of directional derivative in search direction less than 2*options.TolFun Pause: hit any key to continue... xo = 0.1075 139.9999 Ot = -2.4719 nS = 9 quit