Last active
January 4, 2016 18:09
-
-
Save diego898/8658901 to your computer and use it in GitHub Desktop.
simple least squares example using cvx
This file contains hidden or 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
%% A imple Least Squares problem | |
% https://en.wikipedia.org/wiki/Least_squares | |
% We first consider the most basic convex optimization problem, least-squares | |
% (also known as linear regression). In a least-squares problem, we seek | |
% x \in R^n that minimizes norm(Ax?b) | |
% Setup parameters | |
n = 100; | |
A = randn(2*n,n); | |
b = randn(2*n,1); | |
% The least-squares solution x=(A'A)^{-1}A'b is easily computed using the backslash operator: | |
x_ls = A \ b; | |
% Using CVX, the same problem can be solved as follows: | |
cvx_begin | |
variable x(n) | |
minimize( norm( A*x-b ) ) | |
cvx_end | |
%% Line-by-line explanation: | |
%{ | |
cvx_begin | |
creates a placeholder for the new CVX problem and prepares Matlab to | |
accept variable declarations, constraints, an objective function, and so forth. | |
variable x(n) | |
declares x to be an optimization variable of dimension n. CVX requires that all problem variables be declared before they are used in the objective function or constraints. | |
minimize( norm(A*x-b) ) | |
specifies the objective function to be minimized. | |
cvx_end | |
signals the end of the CVX specification, and causes the problem to be solved. | |
%} | |
%% Comments | |
%{ | |
When Matlab reaches the cvx_end command, the least-squares problem is solved, | |
and the Matlab variable x is overwritten with the solution of the | |
least-squares problem. Now x is an ordinary length-n numerical vector, | |
identical to what would be obtained in the traditional approach, at least | |
to within the accuracy of the solver. In addition, several additional | |
Matlab variables are created; for instance: | |
cvx_optval | |
contains the value of the objective function; | |
cvx_status | |
contains a string describing the status of the calculation | |
All of these quantities: x, cvx_optval, and cvx_status, etc. may now be | |
freely used in other Matlab statements, just like anything else | |
The complete cvx user guide can be found here: http://cvxr.com/cvx/cvx_usrguide.pdf | |
The toolbox comes with a complete set of examples as well. | |
%} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment