当前位置:编程学习 > Matlab >>

Matlab分支定界源代码的问题

源代码如下: function [x,y]=ILp(f,G,h,Geq,heq,lb,ub,x,id,options) %整数线性规划分支定界法,可求解纯整数规划和混合整数规划。 %y=minf’*x s.t. G*x<=h Geq*x=heq x为全整数或混合整数列向量 %用法 %[x,y]=ILp(f,G,h,Geq,heq,lb,ub,x,id,options) %参数说明 %lb:解的下界列向量(Default:-int) %ub:解的上界列向量(Default:int) %x:迭代初值列向量 %id:整数变量指标列向量,1-整数,0-实数(Default:1) global upper opt c x0 A b Aeq beq ID options; if nargin<10,options=optimset({});options.Display='off'; options.LargeScale='off';end if nargin<9,id=ones(size(f));end if nargin<8,x=[];end if nargin<7 |isempty(ub),ub=inf*ones(size(f));end if nargin<6 |isempty(lb),lb=zeros(size(f));end if nargin<5,heq=[];end if nargin<4,Geq=[];end upper=inf;c=f;x0=x;A=G;b=h;Aeq=Geq;beq=heq;ID=id; ftemp=ILP(lb(:),ub(:)); x=opt;y=upper; %下面是子函数 function ftemp=ILP(vlb,vub) global upper opt c x0 A b Aeq beq ID options; [x,ftemp,how]=linprog(c,A,b,Aeq,beq,vlb,vub,x0,options); if how <=0 return; end; if ftemp-upper>0.00005 %in order to avoid error return; end; if max(abs(x.*ID-round(x.*ID)))<0.00005 if upper-ftemp>0.00005 %in order to avoid error opt=x';upper=ftemp; return; else opt=[opt;x']; return; end; end; notintx=find(abs(x-round(x))>=0.00005); %in order to avoid error intx=fix(x);tempvlb=vlb;tempvub=vub; if vub(notintx(1,1),1)>=intx(notintx(1,1),1)+1; tempvlb(notintx(1,1),1)=intx(notintx(1,1),1)+1; ftemp=IntLP(tempvlb,vub); end; if vlb(notintx(1,1),1)<=intx(notintx(1,1),1) tempvub(notintx(1,1),1)=intx(notintx(1,1),1); ftemp=IntLP(vlb,tempvub); end; 在命令行运行 >>c=[1,1,-4];a=[1,1,2;1,1,-1;-1,1,1];b=[9;2;4]; >> [x,f]=ILp(c,a,b,[],[],[0;0;0],[inf;inf;inf]) 会出现 ??? Error using ==> unknown Matrix dimensions must agree. Error in ==> ILp>ILP at 33 if max(abs(x.*ID-round(x.*ID)))<0.00005 Error in ==> ILp at 21 ftemp=ILP(lb(:),ub(:)); 请问如何解决?
补充:要求提供精准的修改过程,最后能达到使用这个函数的目的。
答案:从你的程序中的如下这条语句可以看出变量ID是全局变量:
global upper opt c x0 A b Aeq beq ID options;

该全局变量(ID)是在你的函数之外被别的程序语句进行过赋值,而且也很有可能被赋成了向量/矩阵、并且它的维数大小跟向量x的维数大小并不相同,这就造成了进行运算x.*ID的时候维数不匹配的问题。

上一个:已知周期函数第一个周期的表达式,用matlab画出前几个周期的图像
下一个:请问,我想用matlab拟合一个函数,函数类型是lnY=lnA+Blnx1+Clnx2+Dlnx3

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,