inner matrix dimensions must agree error in the function?

1 回表示 (過去 30 日間)
Sameer
Sameer 2014 年 5 月 29 日
編集済み: Image Analyst 2014 年 5 月 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 Center および File ExchangeSolver 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