Skip to content

Instantly share code, notes, and snippets.

@johanlofberg
Created May 3, 2021 16:23
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/b925f3732c0d55ff7249c9ad3421ebb9 to your computer and use it in GitHub Desktop.
Save johanlofberg/b925f3732c0d55ff7249c9ad3421ebb9 to your computer and use it in GitHub Desktop.
%% Generate data
blues = randn(2,25);
greens = randn(2,25)+2;
plot(greens(1,:),greens(2,:),'g*')
hold on
plot(blues(1,:),blues(2,:),'b*')
%% Define and solve model
a = sdpvar(2,1);
b = sdpvar(1);
u = sdpvar(1,25);
v = sdpvar(1,25);
Constraints = [a'*greens+b >= 1-u, a'*blues+b <= -(1-v), u >= 0, v >= 0]
Objective = sum(u)+sum(v)
Constraints = [Constraints, -1 <= a <= 1];
optimize(Constraints,Objective)
%% Plotting using lazy YALMIP code
x = sdpvar(2,1);
P1 = [-5<=x<=5, value(a)'*x+value(b)>=0];
P2 = [-5<=x<=5, value(a)'*x+value(b)<=0];
clf
plot(P1);hold on
plot(P2);
plot(greens(1,:),greens(2,:),'g*')
plot(blues(1,:),blues(2,:),'b*')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment