help me with this problem
Afficher commentaires plus anciens
??? 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 commentaires
John D'Errico
le 30 Mai 2015
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
le 30 Mai 2015
Modifié(e) : Walter Roberson
le 30 Mai 2015
Réponses (1)
Walter Roberson
le 30 Mai 2015
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 commentaires
Hafiz Sahar
le 31 Mai 2015
Walter Roberson
le 31 Mai 2015
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)
Catégories
En savoir plus sur Nonlinear Optimization dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!