楼主: hjm1984
3885 23

[问答] 哪位高手帮忙改一下matlab程序 [推广有奖]

11
kerrydu 发表于 2011-10-11 11:11:16
错了xt不要
function f=myfun(x)
a=[0.19,0.21,0.24,0.19,0.17,x(1),x(2),x(3),x(4),x(5)];      
u=[0.00096,-0.00045,-0.00037,-0.00012,-0.00003,-0.00008,0.00138,-0.00032,-0.00068,-0.00051;
   -0.00045,0.00108,0,-0.00023,0.00022,0.00015,-0.00054,-0.0005,-0.00044,0.00120;
-0.00037,0,0.0012,0.0005,0.0018,-0.0001,-0.0007,0.00033,-0.00013,-0.00021;
-0.00012,-0.00023,0.0005,0.00038,0.00045,0.00024,-0.00016,-0.00017,-0.00011,-0.0007;
-0.00003,0.00022,0.0018,0.00045,0.00062,0.00032,-0.00001,0.00003,-0.00011,-0.0007;
-0.00008,0.00015,-0.0001,0.00024,0.00032,0.00038,-0.00018,0.00025,-0.0006,-0.00005;
0.00138,-0.00054,-0.0007,-0.00016,-0.00001,-0.00018,0.0022,-0.00023,-0.00062,-0.00065;
-0.00032,-0.0005,0.00033,-0.00017,0.00003,0.00025,-0.00023,0.00084,-0.0004,0.0014;
-0.00068,-0.00044,-0.00013,-0.00011,-0.00011,-0.0006,-0.00062,-0.0004,0.0023,-0.00016;
-0.00051,0.00120,-0.00021,-0.0007,-0.0007,-0.00005,-0.00065,0.0014,-0.00016,0.0022];

f=2.046*(a'*u*a)^0.5-0.047;

12
hjm1984 发表于 2011-10-11 12:00:42
kerrydu 发表于 2011-10-11 11:11
错了xt不要
function f=myfun(x)
a=[0.19,0.21,0.24,0.19,0.17,x(1),x(2),x(3),x(4),x(5)];
??? Undefined function or variable 'beq'.      哥们再给看一看

13
kerrydu 发表于 2011-10-11 12:10:24
sorry 我打错了,beq打成bea了
aeq1=ones(1,5); beq1=1-0.19-0.21-0.24-0.19-0.17;
aeq2=[0.048,0.044, 0.04,0.07,0.066 ];beq2=0.047-(0.032*0.19+0.036*0.21+0.042*0.24+0.048*0.19+0.046*0.17);
aeq=[aeq1;aeq2];beq=[beq1;beq2];
A=[3500,2000,2300,2500,4000;-3500,-2000,-2300,-2500,-4000];
B=[12500;-9600]

14
hjm1984 发表于 2011-10-11 12:50:53
kerrydu 发表于 2011-10-11 12:10
sorry 我打错了,beq打成bea了
aeq1=ones(1,5); beq1=1-0.19-0.21-0.24-0.19-0.17;
aeq2=[0.048,0.044, 0 ...
aeq1=ones(1,5); beq1=1-0.19-0.21-0.24-0.19-0.17;
aeq2=[0.048,0.044, 0.04,0.07,0.066 ];beq2=0.047-(0.032*0.19+0.036*0.21+0.042*0.24+0.048*0.19+0.046*0.17);
aeq=[aeq1;aeq2];beq=[beq1;beq2];
A=[3500,2000,2300,2500,4000;-3500,-2000,-2300,-2500,-4000];
B=[12500;-9600];x=[0.19,0.21,0.24,0.19,0.17,0,0,0,0,0];Ib=zeros(10,1);
[x,fval]=fmincon('myfun',x,A,B,aeq,beq,[],[],[]);
Warning: Large-scale (trust region) method does not currently solve this type of problem,
switching to medium-scale (line search).
> In fmincon at 274
??? Error using ==> fmincon
FMINCON cannot continue because user supplied objective function failed with the following error:
Error using ==> mtimes
Inner matrix dimensions must agree.

15
hjm1984 发表于 2011-10-11 12:51:51
hjm1984 发表于 2011-10-11 12:50
aeq1=ones(1,5); beq1=1-0.19-0.21-0.24-0.19-0.17;
aeq2=[0.048,0.044, 0.04,0.07,0.066 ];beq2=0.047- ...
还是出错

16
kerrydu 发表于 2011-10-11 13:03:50
hjm1984 发表于 2011-10-11 12:51
还是出错
a先做个转置,你再试试看
function f=myfun(x)
a=[0.19,0.21,0.24,0.19,0.17,x(1),x(2),x(3),x(4),x(5)]';      
u=[0.00096,-0.00045,-0.00037,-0.00012,-0.00003,-0.00008,0.00138,-0.00032,-0.00068,-0.00051;
   -0.00045,0.00108,0,-0.00023,0.00022,0.00015,-0.00054,-0.0005,-0.00044,0.00120;
-0.00037,0,0.0012,0.0005,0.0018,-0.0001,-0.0007,0.00033,-0.00013,-0.00021;
-0.00012,-0.00023,0.0005,0.00038,0.00045,0.00024,-0.00016,-0.00017,-0.00011,-0.0007;
-0.00003,0.00022,0.0018,0.00045,0.00062,0.00032,-0.00001,0.00003,-0.00011,-0.0007;
-0.00008,0.00015,-0.0001,0.00024,0.00032,0.00038,-0.00018,0.00025,-0.0006,-0.00005;
0.00138,-0.00054,-0.0007,-0.00016,-0.00001,-0.00018,0.0022,-0.00023,-0.00062,-0.00065;
-0.00032,-0.0005,0.00033,-0.00017,0.00003,0.00025,-0.00023,0.00084,-0.0004,0.0014;
-0.00068,-0.00044,-0.00013,-0.00011,-0.00011,-0.0006,-0.00062,-0.0004,0.0023,-0.00016;
-0.00051,0.00120,-0.00021,-0.0007,-0.0007,-0.00005,-0.00065,0.0014,-0.00016,0.0022];

f=2.046*(a’*u*a)^0.5-0.047;

17
hjm1984 发表于 2011-10-11 13:07:09
kerrydu 发表于 2011-10-11 13:03
a先做个转置,你再试试看
function f=myfun(x)
a=[0.19,0.21,0.24,0.19,0.17,x(1),x(2),x(3),x(4),x(5) ...
Warning: Large-scale (trust region) method does not currently solve this type of problem,
switching to medium-scale (line search).
> In fmincon at 274
??? Error using ==> fmincon
Aeq has the wrong number of columns.

18
kerrydu 发表于 2011-10-11 13:17:26
hjm1984 发表于 2011-10-11 13:07
Warning: Large-scale (trust region) method does not currently solve this type of problem,
switch ...
初始值错了
x=[0,0,0,0,0];只有5个未知数

19
hjm1984 发表于 2011-10-11 14:10:58
kerrydu 发表于 2011-10-11 13:17
初始值错了
x=[0,0,0,0,0];只有5个未知数
Warning: Large-scale (trust region) method does not currently solve this type of problem,
switching to medium-scale (line search).
> In fmincon at 274
Optimization terminated: magnitude of directional derivative in search
direction less than 2*options.TolFun and maximum constraint violation
  is less than options.TolCon.
Active inequalities (to within options.TolCon = 1e-006):
  lower      upper     ineqlin   ineqnonlin
                          2           
又出现新问题

20
kerrydu 发表于 2011-10-11 15:14:41
hjm1984 发表于 2011-10-11 14:10
Warning: Large-scale (trust region) method does not currently solve this type of problem,
switch ...
优化结果显示不等式约束2成变成等式。。。你换个初始值试试。。。看fv那个更小一点,也有可能最小值就这样

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-26 18:14