Maximize function with constraints using fmincon

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일

0 개 추천

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

Marcel
Marcel 2014년 11월 20일
M3 is a constant matrix and loaded in the workspace. It is a known argument. How can I define it in the function correctly ?
Matt J
Matt J 2014년 11월 20일
편집: Matt J 2014년 11월 20일
Torsten:
By inserting the line
global M3
at the positions where you define M3 and where you access M3
?
Best wishes
Torsten.
Matt J
Matt J 2014년 11월 20일
편집: Matt J 2014년 11월 20일
Marcel
Ok I have changed it so far and I receive "only" 2 errors.
function [s,weight] = objectfun1(w3,M3)
s = w3'*M3*kron(w3,w3);s=-s;
weight = w3;
end
fun=objectfun1;
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);
[w3, Sopt] = fmincon(fun,x0,Aeq,beq,lb,ub);
Error using objectfun1 (line 4)
Not enough input arguments. (I need only w3 and M3 to compute s)
Error in test4 (line 2)
fun=objectfun1; (no idea ?)
I think the final solution cant be that far away.
Regards
Matt J
Matt J 2014년 11월 20일
편집: Matt J 2014년 11월 20일
Torsten
function [s] = objectfun1(w,M)
s=-w'*M*kron(w,w); % As I told you: I doubt that w3'*M3*kron(w3,M3) is correct
end
fun = @(w)objectfun1(w,M3);
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);
[w3, Sopt] = fmincon(fun,x0,Aeq,beq,lb,ub);
Best wishes
Torsten.
Matt J
Matt J 2014년 11월 20일
Guys, for the benefit of the rest of us, please reply to each other using the Comment boxes, instead of the Answer boxes. The Answer boxes are meant purely for starting threads for alternative answers.
Marcel
Marcel 2014년 11월 20일
편집: Matt J 2014년 11월 20일
Hm ... when I run the script it says me :
Error using fmincon (line 284)
Aeq must have 10 column(s). (Doesn't make any sense)
Error in test4 (line 14)
[w3, Sopt] = fmincon(fun,x0,Aeq,beq,lb,ub);
When I run the optimization tool it says "Not enough input arguments."
I think there is still something wrong with the function. Maybe fmincon is not appropriate but I dont know any better. Quadratic would be for a Covariance Matrix but in this case we have
s = -w'*M3*kron(w,w) not s=-w'*M3*w
I want to calculate the optimum Skewness Sopt and therefore need the weights (10x1). M3 is the CoSkewness Matrix (10x100).
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개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 11월 19일

댓글:

2014년 11월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by