You are on page 1of 7

MEEN 357 -- Fall 2012

Lecture 33: Optimization in


MATLAB

Exercise: Formulate the Following as an


Optimization Problem (in standard form)

Design a cylindrical container that holds at least 0.2


m3 of fluid. The area of its bottom and sides are to be
minimized (it is open on the top).

MEEN 357 -- Fall 2011

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

Optimization problem: arg min ( r 2 + 2 rh )


r ,h
s.t.
0 < r < rub
0 < h < hub
0.2 r 2 h < 0
MEEN 357 -- Fall 2011

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(...)

fun is a function handle


Use just like you did for fzero, etc.
MEEN 357 -- Fall 2011

exitFlag return value


Optional return value for most optimizers in MATLAB
Usage depends on specific method, but in general >0
is good, <0 is bad.
For fminbnd:
exitflag: Integer identifying the reason the algorithm
terminated. The following lists the values of exitflag and the
corresponding reasons the algorithm terminated.
1 : Function converged to a solution x.
0 : Number of iterations exceeded options.MaxIter or
number of function evaluations exceeded
options.FunEvals.
-1 : Algorithm was terminated by the output function.
-2 : The bounds are inconsistent.

MEEN 357 -- Fall 2011

2
7

output return value

Optional return value for most optimizers in MATLAB


struct with information fields
Specific content depends on optimizer
For fminbnd:
output: Structure containing information about the
optimization. The fields of the structure are
iterationsNumber of iterations taken
funcCountNumber of function evaluations
algorithmOptimization algorithm used
messageExit message

MEEN 357 -- Fall 2011

options argument

Optional input argument to optimization methods in


MATLAB
A structure holding options for algorithms
Valid contents depend on method
Often need to change stuff for optimization algorithms
Use optimset to set the options struct
E.g.,

options=optimset('Display','iter-detailed',...
'TolX',1e-5,'TolCon',1e-2,...
'ScaleProblem','obj-and-constr');

MEEN 357 -- Fall 2011

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

fmincon distinguishes between linear and nonlinear


constraints because it handles them differently.
(It deals with linear constraints more efficiently.)

MEEN 357 -- Fall 2011

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

fmincon Handle to objective function


Syntax Initial guess at solution
x = fmincon(fun,x0,A,b)

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

12

fmincon Handle to objective function


Syntax Initial guess at solution
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)

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

4
13

fmincon Handle to function that evaluates


nonlinear constraints
Syntax
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)

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

14

Continuing the Exercise from Earlier


Optimization problem:
arg min ( r 2 + 2 rh )
r ,h

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

Objective & Constraint Functions


function area = objfun(x)

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

Script to Solve Optimization Problem


% optimization script for lecture 32

% 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);

[x_star, Area_star, exitFlag, output] =


fmincon(@objfun,X0,...
[],[],[],[],...
Xlb,Xub,@confun,options);

MEEN 357 -- Fall 2011

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.

Optimization Metric Options


norm(search direction) = 8.77e-005 TolX = 1e-004 (selected)
max(constraint violation) = 2.22e-005 TolCon = 1e-002 (selected)

Active inequalities (to within options.TolCon = 0.01):


lower upper ineqlin ineqnonlin

MEEN 357 -- Fall 2011

18

Designing a Cylindrical Beam E=20x109 N/m2

Design beam to withstand as large a P


force as possible without buckling.
Length is fixed at 3 m. The modulus of
elasticity of the beam material is
20x109 N/m2. The radius should be no
more than 0.25 m.
2 EI L
The beam will buckle if: P > Pc = 2
L
where I = r 4 4
Set the problem up for solution using
an appropriate MATLAB method.
Identify any supporting functions you
will need. r
MEEN 357-501 -- Fall 2010

6
19

Designing a Cylindrical Beam E=200x109 N/m2

Standard optimization form: P


EI
2
Er
3 4
arg max Pc = =
r L2 4 L2
s.t.
0 < r < 0.25 m
L
Nonlinear objective in a single variable with
only constraint being lower & upper bounds
fminbnd
1. Create objective function m-file
2. Create script to call fminbnd

Negate objective function to minimize r


MEEN 357-501 -- Fall 2010

20

Objective & Constraint Functions


function P = Pmax(r,L,E)

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);

fprintf('Best radius = %g [m]\n',r_star);


fprintf('Max load = %g [N]\n',-fval);

Best radius = 0.249963 [m]


Max load = 6.72485e+007 [N]
>>
MEEN 357-501 -- Fall 2010

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

Important: Three-step process:


problem to math to MATLAB

MEEN 357 -- Fall 2011

You might also like