A. MATLAB中 intvar(5,10)是什么意思
MATLAB中 intvar(5,10)是什么意思?能同时定义多个问题吗?
intvar是Yalmip工具箱的函数,用于创建符号整数变量数。
intvar(5,10)的意思是创建五行十列的符号整数变量矩阵。
根据intvar()函数的要求,只能定义一组问题的变量,不能同时定义多个问题。
下图为一个具体例子的计算结果。
B. yalmip可不可以解非线性规划
MATLAB求解线性的整数规划可以用分支定界法,但实现起来还是比较困难。可以去下载一个叫YALMIP的工具箱,用他可以解决线性规划,非线性规划,整数规划,混合规划,强烈推荐把这个工具整合到matlab中去,这个工具是私人的,不过可以免费下载使用。不过最好的方法是用LINGO求解。有了YALMIP工具箱,输入也变的相对简单,代码如下:x=intvar(2,7);f=[0.487,0.520,0.613,0.720,0.487,0.520,0.640;0.487,0.520,0.613,0.720,0.487,0.520,0.640]*x';F=set(x>=0);F=F+set(x(1,1)+x(2,1)<=8)+set(x(1,2)+x(2,2)<=7)+set(x(1,3)+x(2,3)<=9)+set(x(1,4)+x(2,4)]<=6)+set(x(1,5)+x(2,5)<=6)+set(x(1,6)+x(2,6)<=4);F=F+set([2,3,1,0.5,4,2,7;2,3,1,0.5,4,2,7]*.x'<=[40;40])+set([0.487,0.52,0.613,0.72,0.487,0.52,0.64;0.487,0.52,0.613,0.72,0.487,0.52,0.64].*x'<=[10.2;10.2])+set([0,0,0,0,0.487,0.52,0.64;0,0,0,0,0.487,0.52,0.64].*x'<=[3.027;3.027]);solvesdp(F,-f)下面用lingo求解:model:max=0.487*x11+0.52*x12+0.613*x13+0.72*x14+0.487*x15+0.52*x16+0.64*x17+0.487*x21+0.52*x22+0.613*x23+0.72*x24+0.487*x25+0.52*x26+0.64*x27;x11+x21<=8;x12+x22<=7;x13+x23<=9;x14+x24<=6;x15+x25<=6;x16+x26<=4;2*x11+3*x12+x13+0.5*x14+4*x15+2*x16+x17<=40;2*x21+3*x22+x23+0.5*x24+4*x25+2*x26+x27<=40;0.487*x11+0.52*x12+0.613*x13+0.72*x14+0.487*x15+0.52*x16+0.64*x27<=10.2;0.487*x21+0.52*x22+0.613*x23+0.72*x24+0.487*x25+0.52*x26+0.64*x17<=10.2;0.487*x15+0.52*x16+0.64*x17<=3.027;0.487*x25+0.52*x26+0.64*x27<=3.027;@gin(x11);@gin(x12);@gin(x13);@gin(x14);@gin(x15);@gin(x16);@gin(x17);@gin(x21);@gin(x22);@gin(x23);@gin(x24);@gin(x25);@gin(x26);@gin(x27);end运行结果:(由于字数超限,运行结果已删除)
C. 基于MATLAB
1、定积分和不定积分:
>>symsx
>>int(sin(x),0,pi/2)
ans=
1
>>int(1/(x^2-1))
ans=
log(x-1)/2-log(x+1)/2
2、即求函数在x=0处泰勒展开式第4项的系数:
>>f=(1+2*x+3*x^2)/(1-2*x-3*x^2);
>>subs(diff(f,4),0)/prod(1:4)
ans=
122
3、求偏导数:
>>symsxy
>>f=x^3+x*cos(x)+x^2*y;
>>diff(f,x)
ans=
cos(x)+2*x*y-x*sin(x)+3*x^2
4、微分方程组的解析解(题目写错了,左端应该是导数):
>>[x1,x2]=dsolve('Dx1=3*x1+4*x2','Dx2=-4*x1+3*x2','x1(0)=0,x2(0)=1');
>>x1=simple(x1)
x1=
sin(4*t)*exp(3*t)
>>x2=simple(x2)
x2=
cos(4*t)*exp(3*t)
D. matlab整数规划程序
可以用YALMIP工具箱解整数规划
定义变量:
sqdvar()实型
intvar()整型
binvar()0-1型
设定目标函数 :
f=目标函数
设定限定条件:
F=set(限定条件)
多个限定条件用加号相连:
F=set(限定条件)+set(限定条件1)+set(限定条件2)……
求解: solvesdp(F,f)
这里解得是F条件下目标函数f的最小值,要求最大值f前面加个负号
求解之后查看数值 :
double(f) double(变量)
intvar(m,n):生成整数型变量;
sdpvar(m,n):生产变量;
solvesdp(F,f):求解最优解(最小值),其中F为约束条件(用set连接),f为目标函数
double:显示求解的答案
有个例子:
已知非线性整数规划为:
Max z=x1^2+x2^2+3*x3^2+4*x4^2+2*x5^2-8*x1-2*x2-3*x3-x4-2*x5
s.t.
0<=xi<=99(i=1,2,...,5)
x1+x2+x3+x4+x5<=400
x1+2*x2+2*x3+x4+6*x5<=800
2*x1+x2+6*x3<=800
x3+x4+5*x5<=200
matlab中输入
>> x=intvar(1,5);
f=[1 1 3 4 2]*(x'.^2)-[8 2 3 1 2]*x';F=set(0<=x<=99);
F=F+set([1 1 1 1 1]*x'<=400)+set([1 2 2 1 6]*x'<=800)+set(2*x(1)+x(2)+6*x(3)<=800);
F=F+set(x(3)+x(4)+5*x(5)<=200);solvesdp(F,-f);
max=double(f)
sx=double(x)
* Starting YALMIP integer branch & bound.
* Lower solver : fmincon-standard
* Upper solver : rounder
* Max iterations : 300
Warning : The relaxed problem may be nonconvex. This means
that the branching process not is guaranteed to find a
globally optimal solution, since the lower bound can be
invalid. Hence, do not trust the bound or the gap...
Node Upper Gap(%) Lower Open
1 : -8.020E+004 0.03 -8.025E+004 2
2 : -8.020E+004 0.03 -8.025E+004 1
3 : -8.020E+004 0.00 -8.020E+004 2
+ 3 Finishing. Cost: -80199
max =
80199
sx =
53 99 99 99 0
E. 如何在MATLAB中重复输入有高类似度的语句; 比如我用yalmip工具箱来解优化;
for i=3:1:1000
str=['F=F+set(x(',num2str(i),')==2*x(',num2str(i-1),')*(1-x(',num2str(i-1),'))):']
eval(str)
end
望采纳
F. 用Matlab cplex工具求解锥规划问题,使用的yalmip求解器,出现Model creation failed
方法步骤如下: 1、工具需求(1)excel 2、第一步,excel里面通常不会默认添加求解器的,我们要进行简单的操作来实现。(1)首先我们选择”文件“,点击”选项“;在弹出的excel选项框中点击”加载项“,选择”excel加载项“,点击”转到“如图 (2)下一步,在弹出的加载宏对话框中勾选"规划求解”,点击“确定”;于是在数据选项卡中就添加了求解器solver工具上述即:如何用excel求解器solver求出最优化解的方法,供出现此问题的朋友们参考和使用。
G. 如何在matlab路径中安装yalmip
安装步骤:
1、将yalmip放到你的MATLAB的toolbox文件夹下;
2、将yalmip文件目录添加到MATLAB的path里(方法:内matlab主界面:file->set path)
可直接容选 Add with subfolders...
注意:要将下面每一个文件目录都添 到matlab的path中
/yalmip
/yalmip/extras
/yalmip/demos
/yalmip/solvers
/yalmip/moles
/yalmip/moles/parametric
/yalmip/moles/moment
/yalmip/moles/global
/yalmip/moles/robust
/yalmip/moles/sos
/yalmip/operators
3、重启matlab
检验yalmip工具箱是否添 成功。
键入“which sdpvar”。就可以了。
H. 如何使用yalmip 解决线性矩阵不
SeDuMi和LMI都是求解器,前者是第三方的,后者是RobustControlToolbox中的YaLMIP是基于MATLAB的建模语言,它的作用是为不同的求解器提供一致的调用接口,目前支持这些求解器:YALMIPWiki|Solvers/Solvers简单来说,YaLMIP用于描述问题,SeDuMi和LMI以及其他求解器用来解决问题