请高手帮我看一下我的matlab程序错在哪里了?min (1000-x(1)^2-2*x(2)^2-x(3)^2-x(1)*x(2)-x(1)*x(3))约束为x(1)^2+x(2)^2+x(3)^2-25=08*x(1)+14*x(2)+7*x(3)-56=0x1,x2,x3>=0程序如下先在MATLAB编辑器中建立非线性约束函数文

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/29 00:00:45

请高手帮我看一下我的matlab程序错在哪里了?min (1000-x(1)^2-2*x(2)^2-x(3)^2-x(1)*x(2)-x(1)*x(3))约束为x(1)^2+x(2)^2+x(3)^2-25=08*x(1)+14*x(2)+7*x(3)-56=0x1,x2,x3>=0程序如下先在MATLAB编辑器中建立非线性约束函数文
请高手帮我看一下我的matlab程序错在哪里了?min (1000-x(1)^2-2*x(2)^2-x(3)^2-x(1)*x(2)-x(1)*x(3))
约束为
x(1)^2+x(2)^2+x(3)^2-25=0
8*x(1)+14*x(2)+7*x(3)-56=0
x1,x2,x3>=0
程序如下
先在MATLAB编辑器中建立非线性约束函数文件:
function y=opt_funl(x)
y=1000-x(1)*x(1)-2*x(2)*x(2)-x(3)*x(3)-x(1)*x(2)-x(1)*x(3);
function [c,ceq]=opt_conl(x)
ceq=[x(1)*x(1)+x(2)*x(2)+x(3)*x(3)-25;8*x(1)+14*x(2)+7*x(3)-56];
c=[];
在命令窗口键入如下命令:
>> ff=optimset; ff.largeScale='off';ff.Display='iter';
ff.TolFun=1e-30; ff.TolX=1e-15; ff.TolCon=1e-20;
X0=[1;1;1];xm=[0;0;0];xM=[];A=[];B=[];Aeq=[];Beq=[];
[x,f_opt,c,d]=fmincon('opt_fun1',x0,A,B,Aeq,Beq,xm,xM,'opt_con1',ff);
程序运行显示错误为
Input argument "x" is undefined.
Error in ==> opt_fun1 at 2
y=1000-x(1)*x(1)-2*x(2)*x(2)-x(3)*x(3)-x(1)*x(2)-x(1)*x(3);

请高手帮我看一下我的matlab程序错在哪里了?min (1000-x(1)^2-2*x(2)^2-x(3)^2-x(1)*x(2)-x(1)*x(3))约束为x(1)^2+x(2)^2+x(3)^2-25=08*x(1)+14*x(2)+7*x(3)-56=0x1,x2,x3>=0程序如下先在MATLAB编辑器中建立非线性约束函数文
1 新建m文件把function y=opt_funl(x)贴进去保存;
2 新建m文件把
function [c,ceq]=opt_conl(x)
ceq=[x(1)*x(1)+x(2)*x(2)+x(3)*x(3)-25];%%去掉第二个式子,那是线性的,不是非线性的
c=[];
贴进去保存,ceq是非线性等式约束,楼主的第二个式子是线性的.
3 在command window里输入
>> options=optimset('LargeScale','off','Display','iter','TolFun',1e-30,'TolX',1e-15,'TolCon',1e-20);
x0=[1;1;1];lb=[0;0;0];Aeq=[8 14 7];Beq=[56];%%线性等式约束用Aeq和beq
[x,f_opt,c,d]=fmincon('opt_funl',x0,[],[],[8 14 7],[56],[0;0;0],[],'opt_conl',options)
max Directional First-order
Iter F-count f(x) constraint Step-size derivative optimality Procedure
0 4 994 27 Infeasible start point
1 10 955.336 22.9 0.25 -295 18.3 infeasible
2 14 964.012 5.773 1 0.811 6.26 Hessian modified
3 18 967.157 0.5242 1 2.83 4.27 Hessian modified
4 22 965.898 0.2011 1 -1.38 3.67
5 26 957.178 3.886 1 -11 0.251
6 30 961.697 0.2039 1 4.24 1.59
7 34 961.564 0.1416 1 -0.196 1.63
8 38 961.679 0.02987 1 0.1 0.0113
9 42 961.715 1.734e-005 1 0.0365 0.002
10 46 961.715 9.332e-011 1 2.12e-005 0.000116 Hessian modified twice
11 50 961.715 2.56e-009 1 -5.53e-009 3.51e-005 Hessian modified
12 54 961.715 6.622e-011 1 2.95e-009 2.67e-006 Hessian modified
13 58 961.715 5.947e-012 1 7.25e-011 7.69e-006 Hessian modified
14 62 961.715 1.357e-012 1 1.4e-012 3.45e-006 Hessian modified
15 72 961.715 1.386e-012 0.0156 1.3e-011 2.55e-005 Hessian modified
16 76 961.715 3.553e-015 1 1.84e-012 3.54e-006 Hessian modified
17 81 961.715 0 0.5 -1.55e-014 6.49e-007 Hessian modified
18 85 961.715 7.105e-015 1 -5.16e-015 8e-006 Hessian modified
19 89 961.715 0 1 7.06e-015 8.32e-006 Hessian modified twice
20 93 961.715 0 1 8.83e-019 6.51e-006 Hessian modified twice
21 97 961.715 0 1 -6.05e-028 1.33e-006 Hessian modified twice
Optimization terminated:magnitude of search direction less than 2*options.TolX
and maximum constraint violation is less than options.TolCon.
No active inequalities.
x =
3.5121
0.2170
3.5522
f_opt =
961.7152
c =
4
d =
iterations:21
funcCount:97
stepsize:1
algorithm:'medium-scale:SQP,Quasi-Newton,line-search'
firstorderopt:1.3292e-006
cgiterations:[]
message:[1x142 char]