fmincon nonlinear equality constraint

조회 수: 2 (최근 30일)
Clemens Gersch
Clemens Gersch 2020년 5월 11일
댓글: Clemens Gersch 2020년 5월 12일
Hi,
I am trying to implement the following equality constraint for using it in fmincon:
where
and TargetVariance is a fixed numeric value. The size of w is 1xN.
This is a minimal example of my code.
%Input
Mu = [.1, .15, .12];
Cov = [.3, .1, .2;
.1, .5, .1;
.2, .1, .7];
% Prepare vector of ones.
N = numel(Mu);
one = ones(1,N);
% Objective function.
NegPFMu = @(w) -Mu * w';
% Set equal weights as initial point.
w0 = one * (1/N);
% Sum of weights constraint.
Aeq = one;
beq = 1;
% Shortselling constraint.
lb = zeros(1,N);
% Minimize.
wopt = fmincon(NegPFMu, w0, [], [], Aeq, beq, lb);
Can you help me with the constraint?
I was thinking about something like the following but I don't how to call that as my nonlcon then.
function [c, ceq] = mycon(w, Cov, TargetVariance)
PortfolioVariance = @(w) w*Cov*w'
ceq = PortfolioVariance(w) - Targetvariance;
c = [];
end
Could you help me please?

채택된 답변

Matt J
Matt J 2020년 5월 12일
wopt = fmincon(NegPFMu, w0, [], [], Aeq, beq, lb,[], @(w) mycon(w, Cov, TargetVariance));
function [c, ceq] = mycon(w, Cov, TargetVariance)
ceq = w*Cov*w'- Targetvariance;
c = [];
end
  댓글 수: 3
Matt J
Matt J 2020년 5월 12일
편집: Matt J 2020년 5월 12일
@(w) mycon(w, Cov, TargetVariance) is an anonymous function.
However, if you mean you would like to avoid creating the local function mycon, then you could do this,
wopt = fmincon(NegPFMu, w0, [], [], Aeq, beq, lb,[],...
@(w) deal([], w*Cov*w'- Targetvariance) );
Clemens Gersch
Clemens Gersch 2020년 5월 12일
That was exactly what I was looking for. Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Nonlinear Optimization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by