Lecture 33: Optimization in Matlab: Exercise: Formulate The Following As An Optimization Problem (In Standard Form)
Lecture 33: Optimization in Matlab: Exercise: Formulate The Following As An Optimization Problem (In Standard Form)
Exercise: Solution
Basic Modeling: Abot = r
2
V = r 2h
Aside = Ch = dh = 2 rh
A = Abot + Aside = r 2 + 2 rh
Design Variables: r , h
Constraints: r , h > 0 V = r 2 h > 0.2
1
4
Optimization in Matlab
fminbnd: 1D unconstrained minimization (variable
bounds allowed)
fminunc: ND unconstrained minimization (not even
variable bounds)
fminsearch: ND unconstrained minimization (not
even variable bounds) w/ non-gradient methods
fmincon: general ND constrained minimization using
gradient-based techniques
patternsearch: ND constrained minimization using
pattern search methods
and more
NOTE: these are from the MATLAB optimization toolkit & might
be unavailable on some installations.
MEEN 357 -- Fall 2011
fminbnd
Problem form: min f ( x ) s.t. x1 x x2
x
Syntax
x = fminbnd(fun,x1,x2)
x = fminbnd(fun,x1,x2,options)
x = fminbnd(problem)
[x,fval] = fminbnd(...)
[x,fval,exitflag] = fminbnd(...)
[x,fval,exitflag,output] = fminbnd(...)
2
7
options argument
options=optimset('Display','iter-detailed',...
'TolX',1e-5,'TolCon',1e-2,...
'ScaleProblem','obj-and-constr');
fmincon
Inequality
Problem form: Constraints
c ( x) 0
ceq ( x ) = 0
min f ( x ) s.t. A x b Equality
x
A x =b Constraints
eq eq
lb x ub
3
10
fmincon
Syntax
x = fmincon(fun,x0,A,b)
x = fmincon(fun,x0,A,b,Aeq,beq)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = fmincon(problem)
[x,fval] = fmincon(...)
[x,fval,exitflag] = fmincon(...)
[x,fval,exitflag,output] = fmincon(...)
[x,fval,exitflag,output,lambda] = fmincon(...)
[x,fval,exitflag,output,lambda,grad] = fmincon(...)
[x,fval,exitflag,output,lambda,grad,hessian] = fmincon(...)
MEEN 357 -- Fall 2011
11
c ( x) 0
ceq ( x ) = 0
min f ( x ) s.t. A x b
x
A x =b
eq eq
lb x ub
12
c ( x) 0
ceq ( x ) = 0
min f ( x ) s.t. A x b
x
A x =b
eq eq
lb x ub
4
13
c ( x) 0
ceq ( x ) = 0
min f ( x ) s.t. A x b
x
A x =b
eq eq
lb x ub
14
s.t.
0 < r < rub Set up for solution using fmincon
0 < h < hub
0.2 r h < 0
2 c ( x) 0
ceq ( x ) = 0
min f ( x ) s.t. A x b
x
A x =b
eq eq
lb x ub
MEEN 357 -- Fall 2011
15
r = x(1);
h = x(2);
area = pi*r^2+2*pi*r*h;
function [c,ceq] = confun(x)
r = x(1);
h = x(2);
ceq = [];
c = 0.2 - pi*r^2*h;
MEEN 357 -- Fall 2011
5
16
% Variable order: r, h
Xlb = [0,0];
Xub = [1,10];
X0 = [0.2,1.1];
options = optimset('Display','Iter-detailed',...
'ScaleProblem','obj-and-constr','TolX',
1e-4,'TolCon',1e-2);
17
Script Output
Max Line search Directional First-order
Iter F-count f(x) constraint steplength derivative optimality Procedure
0 3 1.50796 0.06177 Infeasible
start point
1 6 1.35058 0.04229 1 0.204 0.924
2 11 1.29913 0.04124 0.25 -0.0972 0.212
3 14 1.47578 0.005702 1 1.84 0.813
4 17 1.50242 0.000135 1 1.56 0.149
5 20 1.50234 7.301e-005 1 0.0103 0.0621 Hessian
modified
6 23 1.50254 2.217e-005 1 0.0466 0.000329 Hessian
modified
Optimization stopped because the norm of the current search direction, 8.767206e-005,
is less than 2*options.TolX = 1.000000e-004, and the maximum constraint
violation, 2.216959e-005, is less than options.TolCon = 1.000000e-002.
18
6
19
20
P = (pi^3 * E * r^4)/(4*L^2);
E = 20e9; % N/m^2
L = 3; % m
r_lb = 0; % m
r_ub = 0.25; % m
[r_star,fval,exitFlag] = ...
fminbnd(@(r)-Pmax(r,L,E),r_lb,r_ub);
21
Summary
Optimization in MATLAB
Lots of optimization methods to choose from in
MATLAB optimization toolkit
Lots of options for controlling optimizer behavior/
performance
No way to avoid digging around in documentation
Other commercial tools have many of the
same features