MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   MATLAB论坛 (https://www.labfans.com/bbs/forumdisplay.php?f=6)
-   -   [MATLAB基础] function 的问题 (https://www.labfans.com/bbs/showthread.php?t=7791)

yunzhongcheng 2009-04-19 17:53

function 的问题
 
function [c,ceq]=mycon(x)
c=[sqrt((x(1)-30)^2+(x(2)-8)^2)-35;
sqrt((x(1)-6)^2+(x(2)-11)^2)-35;
sqrt((x(1)-12)^2+(x(2)-65)^2)-35;
sqrt((x(1)-56)^2+(x(2)-28)^2)-35;
sqrt((x(1)-45)^2+(x(2)-39)^2)-35];
ceq=[];

function f=myfun(x)
f=200*sqrt((x(1)-30)^2+(x(2)-8)^2)
+300*sqrt((x(1)-6)^2+(x(2)-11)^2)
+250*sqrt((x(1)-12)^2+(x(2)-65)^2)
+100*sqrt((x(1)-56)^2+(x(2)-28)^2)
+150*sqrt((x(1)-45)^2+(x(2)-39)^2);

clear
x0=[31,36.5];
lb=[56,65];
ub=[6,8];
[x,fval]=fmincon(@fun,x0,[],[],[],[],lb,ub,@mycon)

运行结果为:
x =

31.0000 36.5000


fval =

[]

程序错哪了,哪位知道,谢谢了

524xu 2009-04-19 21:34

回复: function 的问题
 
首先是 lb和ub值写反了
将最后2句改为 ub=[56,65];
lb=[6,8];
[x,fval]=fmincon(@myfun,x0,[],[],[],[],lb,ub,@mycon);
可得结果

mathjiang 2009-04-23 17:43

回复: function 的问题
 
两处错:
lb=[56,65];
ub=[6,8];
[x,fval]=fmincon(@fun,x0,[],[],[],[],lb,ub,@mycon)

(1)lb应比ub小,你写反了;
(2)@fun应为@myfun。

mathjiang 2009-04-23 18:08

回复: function 的问题
 
建议楼主不要用matlab求解这个题目,因为matlab求解此类题目很烦。
lingo求解程序:
model:
min=200*@sqrt((x-30)^2+(y-8)^2)
+300*@sqrt((x-6)^2+(y-11)^2)
+250*@sqrt((x-12)^2+(y-65)^2)
+100*@sqrt((x-56)^2+(y-28)^2)
+150*@sqrt((x-45)^2+(y-39)^2);
@sqrt((x-30)^2+(y-8)^2)-35<0;
@sqrt((x-6)^2+(y-11)^2)-35<0;
@sqrt((x-12)^2+(y-65)^2)-35<0;
@sqrt((x-56)^2+(y-28)^2)-35<0;
@sqrt((x-45)^2+(x-39)^2)-35<0;
@bnd(6,x,56);
@bnd(8,y,65);
end

程序求解结果:
Global optimal solution found.
Objective value: 28546.47
Objective bound: 28546.45
Infeasibilities: 0.000000
Extended solver steps: 13
Total solver iterations: 1285
Variable Value Reduced Cost
X 22.22188 0.000000
Y 31.52593 0.000000

mathjiang 2009-04-23 18:09

回复: function 的问题
 
楼主把问题撂在这儿就不管了,O(∩_∩)O~


所有时间均为北京时间。现在的时间是 03:11

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.