
1、创建目标函数,myobj(x)
f=-(2*x1+3*x1^2+3*x2+x2^2+x3)
2、创建约束条件函数,mycon(x)
根据给出的条件,来写不等式条件和等式条件
3、使用fmincon()函数,求解x1,x2,x3。即
[x,fval,exitflag]=fmincon(@myobj,x0,[],[],[],[],lb,ub,@mycon)
这里,x0—初值,lb,ub—xi的上下限
4、得到x1,x2,x3,进行验证是否满足约束条件
function example()options=optimset('largescale','off')
[x,y]=fmincon(@fun1,rand(1,3),[],[],[],[],zeros(1,3),[],@fun2,options)
function f=fun1(x)
f=sum(x.^2)+8
function [g,h]=fun2(x)
g=[-x(1)^2+x(2)-x(3)^2
x(1)+x(2)^2+x(3)^3-20]
h=[-x(1)-x(2)^2+2
x(2)+2*x(3)^2-3]
结果
Warning: Options LargeScale = 'off' and Algorithm =
'trust-region-reflective' conflict.
Ignoring Algorithm and running active-set algorithm. To run
trust-region-reflective, set
LargeScale = 'on'. To run active-set without this warning, use
Algorithm = 'active-set'.
>In fmincon at 445
In example at 3
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 function tolerance,
and constraints were satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
Active inequalities (to within options.TolCon = 1e-006):
lower upper ineqlin ineqnonlin
1
x =
0.55221.20330.9478
y =
10.6511
>>
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)