登录论坛

查看完整版本 : [MATLAB基础] function 的问题


yunzhongcheng
2009-04-19, 17:53
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
首先是 lb和ub值写反了
将最后2句改为 ub=[56,65];
lb=[6,8];
[x,fval]=fmincon(@myfun,x0,[],[],[],[],lb,ub,@mycon);
可得结果

mathjiang
2009-04-23, 17:43
两处错:
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
建议楼主不要用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
楼主把问题撂在这儿就不管了,O(∩_∩)O~