
大家好,又见面了,我是你们的朋友全栈君。
最近写文章需要用到fmincon函数做优化,于是抽空学习一下;按照惯例,继续开个博文记录一下学习的过程
参考资料: [寻找约束非线性多变量函数的最小值 – MathWorks] [Matlab求解非线性规划,fmincon函数的用法总结 – 博客园] [Matlab非线性规划 – 博客园]
在Matlab中,fmincon 函数可以求解带约束的非线性多变量函数(Constrained nonlinear multivariable function)的最小值,即可以用来求解非线性规划问题
matlab中,非线性规划模型的写法如下
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=beqc(x)≤0ceq(x)=0lb≤x≤ub
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,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
求下列非线性规划问题: m i n 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 \\ s.t. \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+2x32=3x1,x2,x3≥0
function f = fun1(x)
f = x(1).^2 + x(2).^2 + x(3).^2 + 8;
endfunction [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代表等式约束
endoptions = 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'即刚才写的约束条件所得结果,x为最优解,y为最优值: 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.8672 x1=0.6312,x2=1.2656,x3=0.9312y=10.8672
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/138937.html原文链接:https://javaforall.cn