??? Error using ==> fmincon at 399 FMINCON cannot continue because user supplied objective function failed with the following error: Error using ==> mtimes Inner matrix dimensions must agree.
Error in ==> optimization at 27 [x,fval] = fmincon(@objfun,x0,A,b,Aeq,beq,lb,ub,@nonlconstr,options);

댓글 수: 2

John D'Errico
John D'Errico 2015년 5월 30일
It is good that you gave us the error, but not enough information to help.
Without seeing the objective function, nothing is possible to know. All that we do know is your code does not evaluate properly. How do you expect fmincon to work?
Hafiz Sahar
Hafiz Sahar 2015년 5월 30일
편집: Walter Roberson 2015년 5월 30일
...........................................................................
% OBJECTIVE FUNCTION (QUADRATIC)
% two-fixed end beam
% 20 elements
...........................................................................
function [MACerror] = objfun(x)
Stiffness = [x(1) x(2) x(3) x(4) x(5)...
x(1)*x(2) x(1)*x(3) x(1)*x(4) x(1)*x(5)...
x(2)*x(3) x(2)*x(4) x(2)*x(5)...
x(3)*x(4) x(3)*x(5)...
x(4)*x(5)...
x(1)*x(1) x(2)*x(2) x(3)*x(3) x(4)*x(4) x(5)*x(5)];
%Mode 1
CoefMS1_2 = [-0.62625; +4.80794E-003; +1.52478E-003; -2.26911E-003; -1.62215E-003; -2.44201E-003; -1.43880E-005;
+6.14842E-006; -2.15840E-006; +2.05209E-006; +1.03966E-005; +3.41834E-008; +3.69734E-006; -1.30661E-005; -5.62849E-006;
+8.42581E-006; -1.61863E-005; 6.28026E-006; +1.06436E-005; +1.02731E-005; +6.02298E-006]
CoefMS1_3 = [-1.47641; +2.08809E-003; +2.39537E-003; +7.74712E-004; -2.21658E-003; -3.05568E-003; -2.39368E-006;
+4.36019E-007; -8.81917E-006; -6.55731E-006; +1.29708E-005; -2.55919E-006; +4.12124E-007; -1.28923E-005;
-2.79162E-006; +1.45467E-005; -1.16457E-007; -1.43976E-005; -2.11790E-006; +1.42757E-005; +1.00904E-005]
CoefMS1_4 = [-1.47641; -3.05568E-003; -2.21658E-003; +7.74712E-004; +2.39537E-003; +2.08809E-003; +1.45467E-005;
-2.79162E-006; +4.12124E-007; -6.55731E-006; -1.28923E-005; -2.55919E-006; -8.81917E-006; +1.29708E-005;
+4.36019E-007; -2.39368E-006; +1.00904E-005; +1.42757E-005; -2.11790E-006; -1.43976E-005; -1.16457E-007]
CoefMS1_5 = [-0.62625; -2.44201E-003; -1.62215E-003; -2.26911E-003; +1.52478E-003; +4.80794E-003; +8.42581E-006;
-5.62849E-006; +3.69734E-006; +2.05209E-006; -1.30661E-005; +3.41834E-008; -2.15840E-006; +1.03966E-005;
+6.14842E-006; -1.43880E-005; +6.02298E-006; +1.02731E-005; +1.06436E-005; -6.28026E-006; -1.61863E-005]
%Mode 2
CoefMS2_2 = [-1.21979; +7.28609E-003; +1.01264E-003; -9.92271E-004; -5.06057E-003; -2.25956E-003; +3.65880E-005;
-1.26643E-005; +4.55874E-006; -4.77595E-006; -3.57048E-005; -1.00907E-005; -1.53279E-005; +2.85135E-005;
+1.12462E-005; -1.18236E-005; -4.28640E-005; +8.09717E-006; +8.56701E-006; +1.56762E-005; +2.00799E-005]
CoefMS2_3 = [-1.05091; -8.14401E-003; -4.51473E-003; +3.18299E-003; +4.49071E-003; +4.93829E-003; +1.51694E-005;
-1.59595E-006; -1.38934E-005; -1.47975E-005; -3.45086E-008; -1.42946E-005; -1.68346E-005; +1.85928E-005;
+5.65465E-006; -1.98276E-007; +4.21961E-005; +2.70955E-005; -2.50111E-005; -1.40455E-005; -7.70311E-006]
CoefMS2_4 = [+1.05091; -4.93829E-003; -4.49071E-003; -3.18299E-003; +4.51473E-003; +8.14401E-003; +1.98276E-007;
-5.65465E-006; +1.68346E-005; +1.47975E-005; -1.85928E-005; +1.42946E-005; +1.38934E-005; +3.45086E-008;
+1.59595E-006; -1.51694E-005; +7.70311E-006; +1.40455E-005; +2.50111E-005; -2.70955E-005; -4.21961E-005]
CoefMS2_5 = [+1.21979; +2.25956E-003; +5.06057E-003; +9.92271E-004; -1.01264E-003; -7.28609E-003; +1.18236E-005;
-1.12462E-005; +1.53279E-005; +4.77595E-006; -2.85135E-005; +1.00907E-005; -4.55874E-006; +3.57048E-005;
+1.26643E-005; -3.65880E-005; -2.00799E-005; -1.56762E-005; -8.56701E-006; -8.09717E-006; +4.28640E-005]
%Mode 3
CoefMS3_2 = [+1.52640; -6.34600E-003; +1.01301E-003; +5.18124E-003; -8.88949E-004; +1.12184E-003; -7.91227E-006;
-9.23657E-006; +1.97301E-005; +2.49429E-005; -5.45917E-005; -2.62461E-006; -3.91625E-006; +2.32291E-005;
+2.62137E-005; -5.54953E-006; +1.24108E-005; +3.08837E-005; -1.45329E-005; -1.38631E-005; -2.58159E-005]
CoefMS3_3 = [-0.63327; +0.013334; +9.94754E-003; -6.06665E-003; -7.41004E-003; -9.85752E-003; +3.90186E-005;
-9.78202E-007; -5.90612E-006; -1.63967E-005; +5.64022E-006; -1.17662E-006; -7.87053E-006; +1.04286E-005;
-9.83650E-006; -2.94670E-005; -6.42002E-005; -6.01855E-005; +2.29061E-005; +4.45621E-005; +7.38355E-005]
CoefMS3_4 = [-0.63327; -9.85752E-003; -7.41004E-003; -6.06665E-003; +9.94754E-003; +0.013334; -2.94670E-005;
-9.83650E-006; -7.87053E-006; -1.63967E-005; +1.04286E-005; -1.17662E-006; -5.90612E-006; +5.64022E-006;
-9.78202E-007; +3.90186E-005; +7.38355E-005; +4.45621E-005; +2.29061E-005; -6.01855E-005; -6.42002E-005]
CoefMS3_5 = [+1.52640; +1.12184E-003; -8.88949E-004; +5.18124E-003; +1.01301E-003; -6.34600E-003; -5.54953E-006;
+2.62137E-005; -3.91625E-006; +2.49429E-005; +2.32291E-005; -2.62461E-006; +1.97301E-005; -5.45917E-005;
-9.23657E-006; -7.91227E-006; -2.58159E-005; -1.38631E-005; -1.45329E-005; +3.08837E-005; +1.24108E-005]
MS1_2 = Stiffness*CoefMS1_2;
MS1_3 = Stiffness*CoefMS1_3;
MS1_4 = Stiffness*CoefMS1_4;
MS1_5 = Stiffness*CoefMS1_5;
MS2_2 = Stiffness*CoefMS2_2;
MS2_3 = Stiffness*CoefMS2_3;
MS2_4 = Stiffness*CoefMS2_4;
MS2_5 = Stiffness*CoefMS2_5;
MS3_2 = Stiffness*CoefMS3_2;
MS3_3 = Stiffness*CoefMS3_3;
MS3_4 = Stiffness*CoefMS3_4;
MS3_5 = Stiffness*CoefMS3_5;
MS1_rsm = [0; MS1_2; MS1_3; MS1_4; MS1_5; 0];
MS2_rsm = [0; MS2_2; MS2_3; MS2_4; MS2_5; 0];
MS3_rsm = [0; MS3_2; MS3_3; MS3_4; MS3_5; 0];
MS1_exp = MSYexpSc(1:6);
MS2_exp = MSYexpSc(7:12);
MS3_exp = MSYexpSc(13:18);
MAC1 = ((MS1_rsm'*MS1_exp)^2)/...
((MS1_rsm'*MS1_rsm)*(MS1_exp'*MS1_exp));
MAC2 = ((MS2_rsm'*MS2_exp)^2)/...
((MS2_rsm'*MS2_rsm)*(MS2_exp'*MS2_exp));
MAC3 = ((MS3_rsm'*MS3_exp)^2)/...
((MS3_rsm'*MS3_rsm)*(MS3_exp'*MS3_exp));
MACerror= (1-MAC1) + (1-MAC2) + (1-MAC3);

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2015년 5월 30일

0 개 추천

Your Stiffness is 20 elements long, necessarily so by construction, sum(1:5)+5 = 20. You used a row vector, so it is 1 x 20.
Your Coefficient vectors are all 21 elements long, a column vector, so 21 x 1.
You are trying to multiply the two, so a (1 x 20) * (21 x 1). That fails because the inner dimensions are not the same.
A natural question would be whether you are looking for
Coeff * Stiffness
to give (21 x 1)*(1 x 20) = 21 x 20. But your lines
MS1_rsm = [0; MS1_2; MS1_3; MS1_4; MS1_5; 0];
show that you expect that the 1 x 1 value "0" is compatible size, so you are either expecting a scalar result or a column vector result. When we scan down further to the formulation of MAC1 we can deduce that your MS1_rsm needs to be 6 elements long, so you are expecting Stiffness * Coeff to return a scalar.
The easiest way to reconcile these would be if your coefficient lists were 20 elements instead of 21. You could subscript them at (1:20) but then you would face the question of whether that is the proper subset of 20 elements of the 21.
Once you get past all of that, MSYexpSc(1:6) needs to have meaning. You have not defined any such variable or function in what you have shown us.

댓글 수: 2

Hafiz Sahar
Hafiz Sahar 2015년 5월 31일
this is my coding..so can you help me this problem
In optimization.m change
[x,fval] = fmincon(@objfun,x0,A,b,Aeq,beq,lb,ub,@nonlconstr,options);
to
DataExp = load('DataExp.mat', 'MSYexpSc');
[x,fval] = fmincon(@(x) objfun,(x, DataExp.MSYexpSc), x0, A, b, Aeq, beq, lb, ub, @nonlconstr, options);
However, you did not show any nonlconstr.m file; if you do not have any nonlinear constraint functions then @nonlconstr should be repaced with [] as in
[x,fval] = fmincon(@(x) objfun,(x, DataExp.MSYexpSc), x0, A, b, Aeq, beq, lb, ub, [], options);
In objfun.m replace
function [MACerror] = objfun(x)
with
function [MACerror] = objfun(x, MSYexpSc)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Nonlinear Optimization에 대해 자세히 알아보기

태그

질문:

2015년 5월 30일

댓글:

2015년 5월 31일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by