How can I add constraints in a solution of a function?

조회 수: 3 (최근 30일)
gsourop
gsourop 2018년 10월 29일
댓글: Bruno Luong 2018년 11월 5일
Hi everyone,
I would like to solve the following function by adding two constraints.
S=randn(6,6); y=randn(6,1); ONE = ones(6,1); rft=randn(1,1); K = randn(1,1);
x = inv(S)*(y- ONE*rft)*0.1/sqrt(K);
I would like to include 2 additional constraints, -1<sum(x)<2 . I am not sure how I should use fmincon.
  댓글 수: 10
Bruno Luong
Bruno Luong 2018년 10월 30일
Yes, even the sqrt() might return complex result.
gsourop
gsourop 2018년 10월 30일
I see. But is there a way to impose the additional constraints without solving the relationship arithmetically?

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

채택된 답변

Bruno Luong
Bruno Luong 2018년 10월 30일
편집: Bruno Luong 2018년 10월 30일
n = 6;
L = randn(n);
S = L'*L;
y = randn(n,1);
rft = randn(1,1);
su = 2;
sl = -1;
% T = V*V'
T = S/(0.1^2);
T = 0.5*(T+T');
[V,D] = eig(T);
V = V.*sqrt(diag(D)');
A = inv(V');
% xx = V'*x
% x = V'\xx = W'*xx
% |xx| = 1
yy = V \ (-rft+y);
SX = sum(A,1);
SXu = SX/su;
SXl = SX/sl;
% ReTurn = (1-x'*ONE)*rft+x'*y
% return = rtf - x'*(rtf + y)
% = rft + xx'*yy
% with yy = W*(-rft+y)
% maximize (xx'*yy)
% such that
% |xx| = 1
% SXu*xx <= 2
% SXl*xx <= 1
xx = yy/norm(yy);
if SXu*xx > 1
n2 = (SXu*SXu');
cs2 = 1/n2;
sn = sqrt(1-cs2);
Tu = null(SXu);
yyu = Tu*(Tu'*yy);
xx = cs2*SXu' + (sn/norm(yyu))*yyu;
elseif SXl*xx > 1
n2 = (SXl*SXl');
cs2 = 1/n2;
sn = sqrt(1-cs2);
Tl = null(SXl);
yyl = Tl*(Tl'*yy);
xx = cs2*SXl' + (sn/norm(yyl))*yyl;
end
x = V' \ xx
% Check constraints
sum(x)
x'*S*x
  댓글 수: 5
gsourop
gsourop 2018년 11월 5일
I am getting an error on the dimensions of the matrix. Hence, I replaces this line with
Scaling = sqrt(diag(D)');
for i = 1:6
for j =1 : 6
V(i,j) = V(i,j).* Scaling(j);
end
end
but the validations at the end do not give me the expected result.
Bruno Luong
Bruno Luong 2018년 11월 5일
"I am getting an error on the dimensions of the matrix"
Then apply my code wrongly. I provide the code working with some fake data
S: sym-def-pos matrix (6 x 6)
y: vector (6 x 1)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear Least Squares에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by