Skip to content

Instantly share code, notes, and snippets.

@johanlofberg
Last active May 4, 2021 07:03
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/db43bfbe56323a47c1c82323e321bc56 to your computer and use it in GitHub Desktop.
Save johanlofberg/db43bfbe56323a47c1c82323e321bc56 to your computer and use it in GitHub Desktop.
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);
sdpvar u v
F = [cone(y-A*xhat,u), cone(xhat,v)];
optimize(F,u + v);
%% High-level
F = [norm(y-A*xhat,2) <= u, norm(xhat,2) <= v];
optimize(F,u + v);
%% Natural form
optimize([],norm(y-A*xhat,2) + norm(xhat,2));
%% Plot and compare with least-squares, use real regressor
clf
plot(t,y,'+b');
hold on
l=plot(t,Atrue*value(xhat),'r');
set(l,'linewidth',2)
grid on
optimize([],norm(y-A*xhat,2));
l=plot(t,Atrue*value(xhat),'k');
set(l,'linewidth',2)
%% Plot and compare with least-squares on the corrupted training data
clf
plot(t,y,'+b');
hold on
l=plot(t,A*value(xhat),'r');
set(l,'linewidth',2)
grid on
optimize([],norm(y-A*xhat,2));
l=plot(t,A*value(xhat),'k');
set(l,'linewidth',2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment