一个很好的例子,列出优化模型和里面的约束条件
求解:
示例
求解结果
(x取(1,1)时获得最小值10)
>> main
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
x =
1.0000
1.0000
y =
10.0000
% 主函数
options=optimset;
[x,y]=fmincon('func1',rand(2,1),[],[],[],[],zeros(2,1),[],'func2',options)
%目标函数
function f=func1(x);
f=x(1).^2+x(2).^2+8;
%非线性向量函数(非线性约束)
function [g,h]=func2(x);
g=-x(1).^2+x(2);
h=-x(1)-x(2).^2+2;
求解结果
>> main
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
x =
86.1884
104.2878
126.1883
152.6878
y =
-43.0860
代码
% 主函数
options=optimset;
a=[1.1 1 0 0;1.21 1.1 1 0;1.331 1.21 1.1 1];
b=[440;484;532.4];
ub=[400;inf;inf;inf];
[x,y]=fmincon('func1',rand(4,1),a,b,[],[],zeros(4,1),ub,[],options)
function f=func1(x);
f=-(sqrt(x(1))+sqrt(x(2))+sqrt(x(3))+sqrt(x(4)));
还可以把约束条件都写到一个函数里,结果一样
% 主函数
[x,y]=fmincon('func1',rand(4,1),[],[],[],[],zeros(4,1),[],'func2',optimset)
%非线性向量函数(非线性约束)
function [g,ceq]=func2(x);
g(1)=x(1)-400;
g(2)=1.1*x(1)+x(2)-440;
g(3)=1.21*x(1)+1.1*x(2)+x(3)-484;
g(4)=1.331*x(1)+1.21*x(2)+1.1*x(3)+x(4)-532.4;
ceq=0;
function f=func1(x);
f=-(sqrt(x(1))+sqrt(x(2))+sqrt(x(3))+sqrt(x(4)));
来源:https://blog.csdn.net/qq_36607894/article/details/100106637