Least squares example

% Data from Yalmip example
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(a),1));
e(100:115) = 30;
y = a*x+e;

% Define the decision variable
toms 6x1 x_hat

% x_hat and the regressors a define the residuals with y
residuals = y-a*x_hat;

%The L2 problem is solved as a QP problem without any constraints.
solution = ezsolve(residuals'*residuals,[]);

plot(t,subs(a*x_hat,solution),'-',t,y,'.')
Problem type appears to be: qp
===== * * * =================================================================== * * *
TOMLAB - Tomlab Optimization Inc. Development license  999001. Valid to 2010-02-05
=====================================================================================
Problem:  1:                                    f_k   11555.596054874401000000
                                              f(x_0)      0.000000000000000000

Solver: CPLEX.  EXIT=0.  INFORM=1.
CPLEX Barrier QP solver
Optimal solution found

FuncEv    2 GradEv    2 ConstrEv    2 Iter    2