Use of fmincon function in Matlab
Recently, I need to use the fmincon function for optimization when writing an article, so I took the time to learn it; as usual, continue to open a blog post to record the learning process
References:
[Finding the minimum value of a constrained nonlinear multivariable function - MathWorks]
[Matlab solves nonlinear programming, summary of usage of fmincon function - Blog Park]
[Matlab Nonlinear Programming - Blog Park]
1 Introduction
In Matlab, fmincon
the function can solve the minimum value of the constrained nonlinear multivariable function (Constrained nonlinear multivariable function), that is, it can be used to solve nonlinear programming problems
In matlab, the non-linear programming model is written as follows
m i n f ( x ) s . t . { A ⋅ x ≤ b A e q ⋅ x = b e q c ( x ) ≤ 0 c e q ( x ) = 0 l b ≤ x ≤ u b min \; f(x) \\ s.t. \begin{cases} A·x ≤b \\ Aeq·x = beq \\ c(x)≤0\\ ceq(x) = 0\\ lb≤x≤ub \end{cases} minf(x)s.t.⎩ ⎨ ⎧A⋅x≤bAeq⋅x=b e qc(x)≤0ce q ( x )=0lb≤x≤ub
- A. Aeq is the matrix corresponding to the linear constraint
- b. beq is the vector corresponding to the linear constraint
- C(x),Ceq(x) are nonlinear constraints (functions that return vectors)
- f(x) is the objective function (a function that returns a scalar)
2. Grammar
Matlab求解命令为:
x = f m i n c o n ( f u n , x 0 , A , b , A e q , b e q , l b , u b , n o n l c o n , o p t i o n s ) x = fmincon(fun,x0,A, b,Aeq,beq,lb,ub,nonlcon,options) x=fmincon(fun,x 0 ,A,b,A e q ,b e q _lb,ub,n o n l co n ,options)
- The return value of x is the value of the decision vector x, and the return value of fval is the value of the objective function f(x)
- fun is a function f(x) defined in an M file, which represents a (non)linear objective function
- x0 is the initial value of x
- A, b, Aeq, beq define linear constraints, if there are no linear constraints, then A=[], b=[], Aeq=[], beq=[]
- lb and ub are the lower and upper bounds of the variable x. If the lower and upper bounds are not constrained, then lb=[], ub=[], it can also be written that each component of lb is -inf, and each component of ub is inf
- nonlcon is a nonlinear vector function constraint defined in an M-file
- options defines the optimization parameters, if not filled in, it means to use the default parameter settings of Matlab
3. Examples
Solve the following nonlinear programming problem:
min f ( x ) = x 1 2 + x 2 2 + x 3 2 + 8 s . t . { x 1 2 − x 2 + x 3 2 ≥ 0 x 1 2 + x 2 2 + x 3 2 ≤ 20 − x 1 2 − x 2 2 + 2 = 0 x 2 + 2 x 3 2 = 3 x 1 , x 2 , x 3 ≥ 0 min \; f(x) = x_{1}^ 2 + x_{2}^2+x_{3}^2+8 \\ st \begin{cases} x_{1}^2-x_{2} +x_{3}^2≥0 \\ x_{1 }^2+x_{2}^2 +x_{3}^2≤20 \\ -x_{1}^2-x_{2}^2 +2=0\\ x_{2} +2x_{3} ^2=3\\ x_{1}, x_{2} ,x_{3}≥0 \end{cases}minf(x)=x12+x22+x32+8s.t.⎩
⎨
⎧x12−x2+x32≥0x12+x22+x32≤20−x12−x22+2=0x2+2x _32=3x1,x2,x3≥0
3.1 Write the M function fun1.m and define the objective function
function f = fun1(x)
f = x(1).^2 + x(2).^2 + x(3).^2 + 8;
end
3.2 Write the M function fun2.m and define nonlinear constraints
function [g,h] = fun2(x)
g(1) = - x(1).^2 + x(2) - x(3).^2;
g(2) = x(1) + x(2).^2 + x(3).^3 - 20;
% g代表不等式约束,Matlab中默认g<=0,所以这里取相反数
h(1) = - x(1).^2 - x(2).^2 + 2;
h(2) = x(2) + 2 * x(3).^2 - 3;
% h代表等式约束
end
3.3 Write the main program function
options = optimset;
[x, y] = fmincon('fun1', rand(3, 1), [], [], [], [], zeros(3, 1), [], 'fun2', options)
% 'fun1'代表目标函数,rand(3, 1)随机给了x初值,zeros(3, 1)代表下限为0,即x1, x2, x3>=0, 'fun2'即刚才写的约束条件
The obtained results, x is the optimal solution, y is the optimal value:
x 1 = 0.6312 , x 2 = 1.2656 , x 3 = 0.9312 y = 10.8672 x_{1}=0.6312,x_{2}=1.2656,x_{3} =0.9312\\ y=10.8672x1=0.6312,x2=1.2656,x3=0.9312y=10.8672