
function B = truncated_closed_loop_regression(X,Y,l)



[u,s,v] = svd(X);

u = u(:,1:l); % Truncate to l number of components
s = s(1:l,1:l);
v = v(:,1:l);

X = u*s*v';


[~,~,v] = svd(Y);

Y = Y*v; % Seperate into optimal and non-optimal data
X = X*v;

p =  size(Y,1); % Maximum number of components
Y_nonopt = Y(:,1:p);  % Non-optimal data
X_nonopt = X(:,1:p);
X_opt = X(:,p+1:end); % X_opt = F


n = rank(X_opt*X_opt'); % Maximum number of components
[u,s,v] = svd(X_opt*X_opt');

u = u(:,1:n); % Truncate to the maximum number of components
s = s(1:n,1:n);
v = v(:,1:n);

for i = 1:size(diag(s))
    s(i,i) = inv(s(i,i));
end

F_inv = v*s'*u';

B_tmp = F_inv*X_nonopt*inv(X_nonopt'*F_inv*X_nonopt)*Y_nonopt';
B = B_tmp';
end