inner matrix dimensions must agree error in the function?

1 次查看(过去 30 天)
function [f] = objfun(W,VCVMatx)
%function for varience of the portfolio with swtN stocks
f =W*VCVMatx*W';
this is my function I am trying to run in the main program. However, when run in the main program, it keeps giving me :
Error using * Inner matrix dimensions must agree.
W is a 10(N)X1 vector, while VCVMatx is an NXN vector?
Does anyone see anything wrong, considering I multiplied it by a transposed W'
I tried running fmincon, but in the main program it says the function matrix dimensions do not agree.
f = (W'*VCVMatx)*W;
i=1;
while i<=rows(ERGrid);
ExpectedReturn = ERInc(i);
[W] = fmincon('objfun',x0,swtOther_A,swtOther_b,swtOther_Aeq,swtOther_beq,swtMinWt1,swtMaxWt1,[],options);
ERGrid(2:swtN+1,i) = W;
i=i+1;
end;
W' is a 1X10 vector while VCVMatx is a 10X10 matrix and W is 10X1 vector
fmincon cannot run as a result.

回答(1 个)

Geoff Hayes
Geoff Hayes 2014-5-30
The first matrix product (in the equation) is the problem. Since W is Nx1 and VCVMatx is NxN, then W*VCVMtx will fail due to the incompatible dimensions of the two matrices. If you are just expecting a single value, then the equation should be re-written as
f = W'*VCVMtx*W;
  29 个评论
Sameer
Sameer 2014-5-30
would you happen to know how to place two linear equality constraints in one fmincon function? thanks.
Geoff Hayes
Geoff Hayes 2014-5-30
No, unfortunately I have never used that function (nor have the toolbox for it).

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by