0% found this document useful (0 votes)
25 views28 pages

Lec6 Fmincon

The fmincon function in MATLAB finds the minimum of a constrained nonlinear multivariable function. It requires specifying the objective function, constraints, and bounds. Constraints include linear inequality, linear equality, nonlinear constraints and are passed in that order with placeholders for unspecified constraints. Examples demonstrate how to write the fmincon expression for various constrained optimization problems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views28 pages

Lec6 Fmincon

The fmincon function in MATLAB finds the minimum of a constrained nonlinear multivariable function. It requires specifying the objective function, constraints, and bounds. Constraints include linear inequality, linear equality, nonlinear constraints and are passed in that order with placeholders for unspecified constraints. Examples demonstrate how to write the fmincon expression for various constrained optimization problems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Constrained Optimization

Finding the minimum of a function of several variables using “fmincon”

1
The function to be minimized should be a continuous function

fmincon finds a minimum of a constrained nonlinear multivariable function, and by


default is based on the SQP (Sequential Quadratic Programming) algorithm.

b and beq are vectors, A and Aeq are matrices, c(x) and ceq(x) are functions that return
vectors, and f(x) is a function that returns a scalar. f(x), c(x), and ceq(x) can be nonlinear
functions.
x, lb, and ub can be passed as vectors or matrices 2
3
fmincon

Find minimum of unconstrained multivariable function


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(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options,P1,P2, ...)
[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(...)
4
5
Example: A polynomial function is given as

𝑓 𝑥 = −𝑥1 𝑥2 𝑥3

subject to the constraint

0 ≤ 𝑥1 + 2𝑥2 + 2𝑥3 ≤ 72.

Minimize the given function 𝑓 𝑥 ,

6
Solution: This is a constrained optimization problem and we can use ‘fmincon’ matlab
function to solve this problem.
The constraint
0 ≤ 𝑥1 + 2𝑥2 + 2𝑥3 ≤ 72 →
can be written as
−𝑥1 − 2𝑥2 − 2𝑥3 ≤ 0
𝑥1 + 2𝑥2 + 2𝑥3 ≤ 72

which can be expressed using matrix inequality


𝑨𝒙 ≤ 𝒃

where
−1 − 2 − 2 𝑇
𝑨= 𝒙 = 𝑥1 𝑥2 𝑥3 𝒃 = 0 72 𝑇 .
1 2 2 7
8
9
Example: A polynomial function is given as

𝑓 𝑥 = 𝑒 𝑥1 4𝑥12 + 2𝑥22 + 4𝑥1 𝑥2 + 2𝑥2 + 1

subject to the constraint

𝑥1 𝑥2 − 𝑥1 − 𝑥2 + 1.5 ≤ 0
−𝑥1 𝑥2 − 10 ≤ 0

Minimize the given function 𝑓 𝑥 , take the initial condition as

𝑥0 = [−1, −1]

10
Solution:

11
12
13
Example:

If lb and Ax=b constraints are given, and there are no other constraints, then
the syntax for fmincon use becomes as:

[xmin fmin] = fmincon('objfun',x0,A,b,[],[],lb)

Note: [xmin fmin] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)

14
Example:

If ub and AeqX=beq constraints are given, and there are no other constraints,
then the syntax for fmincon use becomes as:

[xmin fmin] = fmincon('objfun',x0,[],[],Aeq, beq,[],ub)

Note: [xmin fmin] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)

15
Example:

If If only nonlinear constraints are given:

[xmin fmin] fmincon('objfun',x0,[],[] ,[],[],[],[],'constraint')

and function file constraint.m must be provided.

The function call


[c,ceq]=constraint(x)
must return c(x) and ceq(x) for given input vector x.

Note: [xmin fmin] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)


16
Summary:
The constraints have to passed to fmincon in the following order:

1. Linear inequality constraints


2. Linear equality constraints
3. Lower bounds
4. Upper bounds
5. Nonlinear constraints

If certain constraints are not required in the problem,

then their input argument should be replaced by the placeholder [] (empty


input).

17
Example: Write the matlab expression for the minimization of the following function
with the given constraint using 'fmincon'

𝑓(𝑥, 𝑦) = 𝑥 4 − 𝑥 2 + 𝑦 2 − 2𝑥 + 𝑦

𝑥≥0 𝑦≤0

Solution:

18
Solution: First we implement the matlab function as in
Objfun.m
function f=objfun(x)

f=x(1)^4-x(1)^2+x(2)^2-2*x(1)+x(2);

With constraint 𝑥 ≥ 0 𝑦 ≤ 0 fmincon is used as


x0=[value1;value2]
[x,fval]=fmincon('objfun',x0,[],[],[],[],[0;-Inf],[Inf;0])

Note: [xmin fmin] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)

19
Example: Write the matlab expression for the minimization of the following function
with the given constraint using 'fmincon'

𝑓(𝑥, 𝑦) = 𝑥 4 − 𝑥 2 + 𝑦 2 − 2𝑥 + 𝑦

𝑥 + 𝑦 = 0, 𝑥 ≤ 1 and 𝑦 ≤ 10 (ub)

Solution:
[xmin,fmin]=fmincon('objfun',x0,[],[],[1,1],0,[],[1;10])

Note: [xmin fmin] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)

20
Example: Write the matlab expression for the minimization of the following function
with the given constraint using 'fmincon'

𝑓(𝑥, 𝑦) = 𝑥 4 − 𝑥 2 + 𝑦 2 − 2𝑥 + 𝑦

𝑥 + 𝑦 ≤ 0, 𝑥2 + 𝑦2 ≤ 1
Solution:
[xmin,fmin]= fminco n('objfun',x0,[1,1],0,[],[],[],[],'constraint')

constriant.m
function [c,ceq]=constraint(x)
c=x(1)^2+x(2)^2-1;
ceq=[];

Note: [xmin fmin] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)


21
Example: Write the matlab expression for the minimization of the following function
with the given constraint using 'fmincon'
𝑓(𝑥, 𝑦) = 𝑥 4 − 𝑥 2 + 𝑦 2 − 2𝑥 + 𝑦

𝑥 2 + 𝑦 2 ≤ 1, 𝑥2 − 𝑦2 ≥ 1

Solution: [xmin,fmin]= fmincon('objfun',x0,[],[],[],[],[],[],'constraint')


constriant.m
function [c,ceq]=constraint(x)
c1=x(1)^2+x(2)^2-1;
c2=1-x(1)^2+x(2)^2;
c=[c1;c2];
ceq=[];

Note: [xmin fmin] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon) 22


Example: Write the matlab expression for the minimization of the following function
with the given constraint using 'fmincon'
𝑓(𝑥, 𝑦) = 𝑥 4 − 𝑥 2 + 𝑦 2 − 2𝑥 + 𝑦

𝑥 2 + 𝑦 2 = 1, 𝑥2 − 𝑦2 ≥ 1

Solution: [xmin,fmin]= fmincon('objfun',x0,[],[],[],[],[],[],'constraint')


constriant.m
function [c,ceq]=constraint(x)
c=1-x(1)^2+x(2)^2;
ceq=x(1)^2+x(2)^2-1 ;

Note: [xmin fmin] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon) 23


Example:

Minimize the objective function

𝑓(𝑥, 𝑦, 𝑧) = 𝑥 3 + 𝑦 3 + 𝑧 3

subject to the constraint

𝑥 ≥ 0, 𝑧 ≤ 0, 𝑥 2 + 𝑦 2 + 𝑧 2 = 1, 𝑦 2 ≥ 2𝑧 2 .

24
Solution:

function f=myfunc_1(x)
f=x(1)^3+x(2)^3+x(3)^3;

Constraint function file:


function [c,ceq]=myconstraint(x)
c=2*x(3)^2-x(2)^2;
ceq=x(1)^2+x(2)^2+x (3)^2-1;

>> x0=[0;1;2] ;
>> [xmin,fmin]=fmincon('myfunc_1',x0,[],[],[],[],[0;-Inf;-Inf],[Inf;Inf;0],‘myconstraint')

Note: [xmin fmin] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon) 25


26
27
28

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy