Skip to content

Instantly share code, notes, and snippets.

@johanlofberg
Created May 3, 2021 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johanlofberg/066ed372cd68c3a5ff94bca85346638e to your computer and use it in GitHub Desktop.
Save johanlofberg/066ed372cd68c3a5ff94bca85346638e to your computer and use it in GitHub Desktop.
%% Always start by clearing the internals
yalmip('clear')
% Define variables
x = sdpvar(10,1);
% Define constraints
Constraints = [sum(x) <= 10, x(1) == 0, 0.5 <= x(2) <= 1.5];
for i = 1 : 7
Constraints = [Constraints, x(i) + x(i+1) <= x(i+2) + x(i+3)];
end
% Define an objective
Objective = x'*x+norm(x,1);
% Set some options for YALMIP and solver
options = sdpsettings('verbose',1,'solver','quadprog','quadprog.maxiter',100);
% Solve the problem
sol = optimize(Constraints,Objective,options);
% Analyze error flags
if sol.problem == 0
% Extract and display value
solution = value(x)
else
display('Hmm, something went wrong!');
sol.info
yalmiperror(sol.problem)
end
%% YALMIPs symbolic variable
P = sdpvar(3,3,'full')
x = sdpvar(3,1);
D = diag(x) ; % Diagonal matrix
H = hankel(x); % Hankel matrix
T = toeplitz(x); % Hankel matrix
x = sdpvar(1,1); y = sdpvar(1,1);
x = sdpvar(1); y = sdpvar(1);
sdpvar x y
P = sdpvar(3,3) + diag(sdpvar(3,1));
X = [P P;P eye(length(P))] + 2*trace(P);
Y = X + sum(sum(P*rand(length(P)))) + P(end,end)+hankel(X(:,1));
X
% Cells and nd-arrays
clear X
for i = 1:5
X{i} = sdpvar(2,3);
end
X = sdpvar([2 2 2 2 2],[3 3 3 3 3]);
X = sdpvar(2,3,5);
Y = sum(X,3)
X(:,:,2)
X = sdpvar(2,2,2,2,'full');
%% Constraints
n = 3;
P = sdpvar(n,n);
C = [P>=0]
P = sdpvar(n,n);
C = [P(:)>=0]
C = [triu(P)>=0]
C = [P(find(triu(ones(n))))>=0];
P = sdpvar(n,2*n);
C = [P>=0]
P = sdpvar(n,n,'full');
C = [P>=0];
P = sdpvar(n,n);
C = [P>=0] + [P(1,1)>=2];
C = [P>=0, P(1,1)>=2];
C = [P>=0, P(1,1)<=2, sum(sum(P))==10]
F = [0 <= P(1,1) <= 2];
my_tolerance_for_strict = 1e-5;
F = [0 <= P(1,1) <= 2-my_tolerance_for_strict];
F = [0 <= P(1,1) <= 2];
for i = 2:n-1
F = [F, P(i,1) <= P(2,i) - P(i,i)];
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment