How can I add constraints in a solution of a function?
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Bruno Luong
le 30 Oct 2018
Modifié(e) : Bruno Luong
le 30 Oct 2018
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 commentaires
Bruno Luong
le 5 Nov 2018
"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)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Linear Least Squares dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!