{{ message }}

Instantly share code, notes, and snippets.

# Johan Löfberg johanlofberg

Created May 6, 2021
View qpapproximation.m
 %% Generate data n = 10; Q = randn(n);Q = Q*Q'; R = randn(n);R = R*R'; S = chol(Q); T = chol(R); %% Basic factorized model x = sdpvar(n,1); y = sdpvar(n,1);
Last active May 6, 2021
View cutsdpexplained.m
 %% Optimize over manually approimated ball sdpvar x y X = [1 x y;[x;y] eye(2)]; Objective = -x-y; ops = sdpsettings('plot.shade',.1,'verbose',0); ballApproximation = [-1 <= [x y] <= 1]; clf;hold on for k = 1:10 for i = 1:20 vi = randn(3,1);
Last active May 6, 2021
View integralpwa.m
 %% Look at function ti = 0:0.001:8; fi = min(min(2*ti,4),16-3*ti); l = plot(t,fi); grid on;hold on l = plot(ti,0.001*cumsum(fi)); %% Manually derived yalmip('clear') sdpvar x
Created May 4, 2021
View samplescenarios.m
 %% Simple ball approximation x = sdpvar(2,1); ballApproximation = []; for i = 1:10 v = randn(2,1);v = v/norm(v); ballApproximation = [ballApproximation, v'*x <= 1]; end clf plot(ballApproximation,x,'blue');hold on plot(x'*x <= 1,x,'red');
Created May 4, 2021
 %% Create data x = [1 2 3 4 5 6]'; t = (0:0.02:2*pi)'; A = [sin(t) sin(2*t) sin(3*t) sin(4*t) sin(5*t) sin(6*t)]; e = (-4+8*rand(length(t),1)); e(100:115) = 30; y = A*x+e; %% Various estimates xhat = sdpvar(6,1);
Last active May 4, 2021
View socpregression.m
 x = [1 2 3 4 5 6]'; t = (0:0.02:2*pi)'; Atrue = [sin(t) sin(2*t) sin(3*t) sin(4*t) sin(5*t) sin(6*t)]; ytrue = Atrue*x; A = Atrue;%.*(0.5+rand(315,6)); A(100:210,:)=.1; y = Atrue*x+randn(length(ytrue),1); %% Low-level xhat = sdpvar(6,1);
Created May 3, 2021
View lpclassification.m
 %% 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);
Created May 3, 2021
View basics.m
 %% 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)];
Created May 2, 2021
View designingpolynomials.m
 % Data x0 = -1 x1 = 0; x2 = 1; y0 = 0; y1 = 1; y2 = 0; n = 9; % Solve conditions
Created May 2, 2021
View powerconepath.m
 %% Setup n = 10; x = randn(2,n); s = sdpvar(n,1); z = sdpvar(2,1); % Define and plot convex hull of points clf; plot([z == x*s, s>=0, sum(s)==1],z);hold on plot(x(1,:),x(2,:),'k*');