inner matrix dimensions must agree error in the function?

2 views (last 30 days)
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.

Answers (1)

Geoff Hayes
Geoff Hayes on 30 May 2014
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 Comments
Sameer
Sameer on 30 May 2014
would you happen to know how to place two linear equality constraints in one fmincon function? thanks.
Geoff Hayes
Geoff Hayes on 30 May 2014
No, unfortunately I have never used that function (nor have the toolbox for it).

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!