NLP E Example
NLP Example
Unconstrained Example
Unconstrained Example
fminunc Consider the problem of finding a set of values [x1,x2]that solves Minimize f(x) X =ex1(4x,2+2x22+4xx2+2x2+1)
fminunc • Consider the problem of finding a set of values [x1, x2] that solves Minimize f ( x ) x =ex1 ( 4x 1 2 +2x 2 2 +4x 1 x 2 +2x 2 +1 )
To solve this two-dimensional problem, write an M-file that returns the function value Then,invoke the unconstrained minimization routine fminunc
• To solve this two-dimensional problem, write an M-file that returns the function value • Then, invoke the unconstrained minimization routine fminunc
Step 1:Write an M-file objfun.m function f objfun(x) f=eXp(x(1)*(4*x(1)2+2*x(2)^2+ 4*x(1)*x(2)+2*X(2)+1)
Step 1: Write an M-file objfun.m function f = objfun(x) f=exp(x(1))*(4*x(1)^2+2*x(2)^2+ 4*x(1)*x(2)+2*x(2)+1)
Step 2:Invoke one of the unconstrained optimization routines xO [-1,1];Starting guess ·options optimset('LargeScale','off); [x,fval,exitflag,output] fminunc(@objfun,x0,options);
Step 2: Invoke one of the unconstrained optimization routines • x0 = [-1,1]; % Starting guess • options = optimset('LargeScale','off'); • [x,fval,exitflag,output] = fminunc(@objfun,x0,options);
After 40 function evaluations,this produces the solution X= 0.5000-1.0000 The function at the solution x is returned in fval. fval 1.3030e-10 The exitflag tells if the algorithm converged.An exitflag 0 means a local minimum was found. exitflag 1
• After 40 function evaluations, this produces the solution x = 0.5000 -1.0000 • The function at the solution x is returned in fval. fval = 1.3030e-10 • The exitflag tells if the algorithm converged. An exitflag > 0 means a local minimum was found. exitflag = 1
Note When more than one local minimum exists, the initial guess for the vector [x1,x2] affects both the number of function evaluations and the value of the solution point
Note • When more than one local minimum exists, the initial guess for the vector [x1, x2] affects both the number of function evaluations and the value of the solution point
Nonlinear Inequality Constrained Example
Nonlinear Inequality Constrained Example
fmincon For example,find x that solves Minimize f(x =exl (4x 2 +2x22 X +4x1x2+2x2+1) subject to the constraints x1X2一X1一尤2 ≤-1.5 x12≥-10
fmincon • For example, find x that solves • Minimize f ( x ) = ex 1 ( 4 x 1 2 +2 x 2 2 x +4 x 1 x 2 +2 x 2 +1 ) • subject to the constraints x 1 x 2 –x 1 – x 2 ≤ –1.5 x 1 x 2 ≥ – 10