Lec6 Fmincon
Lec6 Fmincon
1
The function to be minimized should be a continuous function
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
𝑓 𝑥 = −𝑥1 𝑥2 𝑥3
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
where
−1 − 2 − 2 𝑇
𝑨= 𝒙 = 𝑥1 𝑥2 𝑥3 𝒃 = 0 72 𝑇 .
1 2 2 7
8
9
Example: A polynomial function is given as
𝑥1 𝑥2 − 𝑥1 − 𝑥2 + 1.5 ≤ 0
−𝑥1 𝑥2 − 10 ≤ 0
𝑥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:
14
Example:
If ub and AeqX=beq constraints are given, and there are no other constraints,
then the syntax for fmincon use becomes as:
15
Example:
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);
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])
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=[];
𝑥 2 + 𝑦 2 ≤ 1, 𝑥2 − 𝑦2 ≥ 1
𝑥 2 + 𝑦 2 = 1, 𝑥2 − 𝑦2 ≥ 1
𝑓(𝑥, 𝑦, 𝑧) = 𝑥 3 + 𝑦 3 + 𝑧 3
𝑥 ≥ 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;
>> x0=[0;1;2] ;
>> [xmin,fmin]=fmincon('myfunc_1',x0,[],[],[],[],[0;-Inf;-Inf],[Inf;Inf;0],‘myconstraint')