Maximize function with constraints using fmincon

14 ビュー (過去 30 日間)
Marcel
Marcel 2014 年 11 月 19 日
コメント済み: Marcel 2014 年 11 月 20 日
Hi guys, I want to max a function with constraints. I think fmincon would work but I don't know how to write it that it works.
function [s] = objectfun1(w)
%thats the function, where w is unkown wheight vector of
%10 assets and M3 is constant CoskewnessMatrix
s = w'*M3*kron(w,w);s=-s;
end
x0=zeros(10,1);
Aeq=[1 1 1 1 1 1 1 1 1 1];
beq=1;
lb=zeros(10,1);
ub=ones(10,1);
w=fmincon(@objectfun1, x0, [], [], Aeq, beq,lb,ub)
Errors : _ _Undefined function or variable 'M3'.
Error in objectfun1 (line 3)
s = w'*M3*kron(w,w);s=-s;
Error in fmincon (line 545)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in testSkew (line 7)
w = fmincon(@objectfun1,zeros(10,1),[],[],[1 1 1 1 1 1 1 1 1 1],1,zeros(10,1),ones(10,1));
Caused by:
Failure in initial user-supplied objective function evaluation. FMINCON cannot continue._ _
It should give me Max s and the coresponding weight vector. I would be glad for any help or the correct code :)
  1 件のコメント
Matt J
Matt J 2014 年 11 月 20 日
編集済み: Matt J 2014 年 11 月 20 日
It would be a little bit computationally cheaper to write the objective function as
function [s] = objectfun1(w)
n=length(w);
tmp=w.'*M3;
s = -(w.'*reshape(tmp,n,n)*w);
end

サインインしてコメントする。

採用された回答

Torsten
Torsten 2014 年 11 月 20 日
1. M3 is not defined in objectfun1 ;
2. w'*M3*kron(w,w) does not look like a scalar value to me ;
3. Better delete the blanks in the call to fmincon.
Best wishes
Torsten.
  8 件のコメント
Matt J
Matt J 2014 年 11 月 20 日
編集済み: Matt J 2014 年 11 月 20 日
You're missing some input arguments,
[w3, Sopt] = fmincon(fun,x0,[],[],Aeq,beq,lb,ub);
Marcel
Marcel 2014 年 11 月 20 日
Yes that is important and at the end this was the final mistake I made. I want to thank you both.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by