Optimum Design with
MATLAB
Cheng-Liang Chen
PSE
LABORATORY
Department of Chemical Engineering National TAIWAN University
Chen CL
1
Optimization Toolbox [x, FunValue, ExitFlag, Output]
= fminX(’ObjFun’,...,op
Type of Problem
Formulation
Uncons Min (1-var)
Find x ∈ [x, xu] to min f (x)
Unconstrained Min
Find x to minimize f (x)
Function
fminbnd
fminunc fminsearch
Constrained Min
Find x to minimize f (x) s.t. N x = e, Ax ≤ b hj = 0, j = 1, . . . , p gk = 0, k = 1, . . . , m
fmincon
xi ≤ xi ≤ xiu
Linear Programming
Find x to minimize f (x) = cT x s.t. N x = e, Ax ≤ b
Quadratic Programming
Find x to min f (x) = xT x + 12 xT H x quadprog s.t. N x = e, Ax ≤ b
linprog
Chen CL
2
Explanation of Output from Optimization Function Argument
x
FunValue
Description
The solution vector or matrix found by the optimization function. If ExitFlag> 0 then x is a solution, otherwise x is the latest value from the optimization routine. Value of the objective function, ObjFun, at the solution x.
ExitFlag
The exit condition for the optimization function. If ExitFlag is positive, then the optimization routine converged to a solution x. If ExitFlag is zero, then the maximum number of function evaluations was reached. If ExitFlag is negative, then the optimization routine did not converge to a solution.
Output
The output vector contains several pieces of information about the optimization process. It provides the nunmber of function evaluations (Output.iterations) and the name of the algorithm used to solve the problem (Output.algorithm), etc.
Chen CL
3
Single-Variable Unconstrained Minimization Find x to minimize f (x) = 2 − 4x + ex,
− 10 ≤ x ≤
10.
% File Name: Example12_1.m x = % Problem: min f(x)=2-4x+exp(x) 1.3863 clear all FunVal = Lb = -10; Ub = 10; 0.4548 [x,FunVal,ExitFlag,Output]... ExitFlag = =fminbnd(’ObjFunction12_1’,Lb,Ub) 1 Output = % File Name: ObjFunction12_1.m iterations: 14 % Objective: f(x)=2-4x+exp(x) funcCount: 16 function f = ObjFunction12_1(x) algorithm:[1x46 char] f = 2 - 4*x + exp(x); Output.algorithm ans = golden section search, parabolic interpolatio
Chen CL
4
Multi-Variable Unconstrained Minimization Find x to minimize f (x) = 100(x2 − x21)2 + (1 − x1)2, x(0) = (−1.2, 1.0) % File Name: ObjAndGrad12_2.m (Rosenbruck Valley Functio function [f, df] = ObjAndGrad12_2(x) x1 = x(1); x2 = x(2); f = 100*(x2-x1^2)^2+(1-x1)^2; df(1)= -400*(x2-x1^2)*x1-2*(1-x1); df(2)= 200*(x2-x1^2);
Chen CL
5
% File Name: Example12_2.m % Problem: min Rosenbruck Valley Function % f(x)=100(x_2-x_1^2)^2+(1-x_1)^2, x^{(0)}=(-1.2,1.0) clear all x0 = [-1.2 1.0]’; % 1. Nelder-Mead Simplex Method, fminsearch options=optimset(’LargeScale’,’off’,’MaxFunEvals’,300); [x1, FunValue1, ExitFlag1, Output1] = ... fminsearch(’ObjAndGrad12_2’, x0, options) % % % 2. BFGS Method, fminunc, default option options=optimset(’LargeScale’,’off’,’MaxFunEvals’,300,... ’GradObj’,’on’); [x2, FunValue2, ExitFlag2, Output2] = ... fminunc(’ObjAndGrad12_2’, x0, options)
Chen CL
% % % %
6
File Name: Example12_2.m Problem: min Rosenbruck Valley Function f(x)=100(x_2-x_1^2)^2+(1-x_1)^2, x^{(0)}=(-1.2,1.0) (continued)
% 3. DFP Method, fminunc, HessUpdate = dfp options=optimset(’LargeScale’,’off’,’MaxFunEvals’,300,... ’GradObj’,’on’,’HessUpdate’,’dfp’); [x3, FunValue3, ExitFlag3, Output3] = ... fminunc(’ObjAndGrad12_2’, x0, options) % % 4. Steepdest descent, fminunc, HessUpdate = steepdesc options=optimset(’LargeScale’,’off’,’MaxFunEvals’,3000,... ’GradObj’,’on’,’HessUpdate’,’steepdesc’); [x4, FunValue4, ExitFlag4, Output4] = ... fminunc(’ObjAndGrad12_2’, x0, options)
Chen CL
x1 =
7
x2 =
1.0000 1.0000 1.0000 1.0000 FunValue1 = FunValue2 = 8.1777e-010 5.4185e-011 ExitFlag1 = ExitFlag2 = 1 1 Output1 = Output2 = iterations: 85 iterations: funcCount: 159 funcCount: algorithm: [1x33 char] stepsize: firstorderopt: algorithm: Output1.algorithm Output2.algorithm
26 111 1.2994 2.8785e-004 [1x38 char]
ans = ans = Nelder-Mead simplex direct search medium-scale: Quasi-Newton line searc
Chen CL
x3 = 1.0000 1.0000 FunValue3 = 3.8112e-011 ExitFlag3 = 1 Output3 = iterations: funcCount: stepsize: firstorderopt: algorithm: Output3.algorithm
8
x4 = 0.8848 0.7819 FunValue4 = 0.0134 ExitFlag4 = 0 Output4 = 27 iterations: 124 funcCount: 2.4667 stepsize: 3.4852e-004 firstorderopt: [1x38 char] algorithm: Output4.algorithm
401 1791 0.0103 1.6641 [1x38 char]
ans = ans = medium-scale: Quasi-Newton line medium-scale: search Quasi-Newton line searc
Chen CL
9
Multi-Variable Constrained Minimization min
f (x) = (x1 − 10)3 + (x2 − 20)3,
x(0) = (20.1, 5.84)
s.t. g1(x) = 100 − (x1 − 5)2 − (x2 − 5)2 g2(x) = −82.81 − (x1 − 6)2 − (x2 − 5)2
13 ≤ x1 ≤ 100, 0 ≤ x2 ≤ 100 % File Name: ObjAndGrad12_3.m function [f, gf] = ObjAndGrad12_3(x) x1 = x(1); x2 = x(2); f = (x1-10)^3+(x2-20)^3; if nargout > 1 gf(1,1) = 3*(x1-10)^2; gf(2,1) = 3*(x2-20)^2; end
≤
0
≤
0
Chen CL
% File Name: ConstAndGrad12_3.m function [g,h,gg,gh] = ConstAndGrad12_3(x) x1 = x(1); x2 = x(2); f = (x1-10)^3+(x2-20)^3; g(1) = 100-(x1-5)^2-(x2-5)^2; g(2) = -82.81+(x1-6)^2+(x2-5)^2; h = []; if nargout > 2 gg(1,1) = -2*(x1-5); gg(2,1) = -2*(x2-5); gg(1,2) = 2*(x1-6); gg(2,2) = 2*(x2-5); gh = []; end
10
Chen CL
11
% File Name: Example12_3.m % Constrained minimization with gradients available % clear all options=optimset(’LargeScale’,’off’,’GradObj’,’on’,... ’GradConstr’,’on’,’TolCon’,1e-8,’TolX’,1e-8); Lb = [13; 0]; Ub = [100; 100]; x0 = [20.1; 5.84]; [ x, FunValue, ExitFlag, Output] = ... fmincon(’ObjAndGrad12_3’, x0, [],[],[],[],... Lb,Ub,’ConstAndGrad12_3’,options)
Chen CL
x = 14.0950 0.8430 FunValue = -6.9618e+003 ExitFlag = 1 Output = iterations: funcCount: stepsize: algorithm: firstorderopt: cgiterations:
12
6 13 1 [1x44 char] 4.3682e-012 []
Output.algorithm ans = medium-scale: SQP, Quasi-Newton, line-search