Created
May 3, 2021 16:23
-
-
Save johanlofberg/b925f3732c0d55ff7249c9ad3421ebb9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% 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