%%
%%A basic problem to start
clc
clear all
cvx_begin
variables x1 x2
maximize(140*x1+235*x2)
subject to
x1>=0
x2>=0
x1+x2<=180
x1+2*x2<=240
.3*x1+.1*x2<=30
%x1^2+2*x2^2<=1 %can add a convex quadratic constraint (but not a concave
%one!)
cvx_end

cvx_optval %to see the optimal value

%%
%%Least squares
clear all
m = 16; n = 8;
A = randn(m,n);
b = randn(m,1);
cvx_begin
    variable x(n) %variable is an nx1 vector here. can also have matrix variables.
    minimize((A*x-b)'*(A*x-b))  %this is a convex quadratic
    %minimize( norm(A*x-b) ) %norm of an affine function - can also try other norms
    %maximize( norm(A*x-b) ) %will produce error - input to maximize should be concave
    %minimize( norm(A*x-b) +1*norm(x,1) ) %LASSO-type problem
cvx_end

 

%% 
%%A constrained norm minimization problem
clear all
m = 16; n = 8;
A = randn(m,n);
b = randn(m,1);
p = 4;
C = randn(p,n);
d = randn(p,1);
cvx_begin
    variable x(n);
    minimize( norm(A*x-b) );
    subject to
        C*x == d; %note you need == to impose an equality constraint. interpreted elementwise.
        x>=.1; %interpreted elementwise.
        norm(x,inf) <= 100;
cvx_end
 
 
%% 
%%An infeasible problem
clear all
cvx_begin
variables x1 x2
minimize(x1+x2)
x1+x2<-5
x1^2+x2^2<1 %removing this constraint takes us from infeasible to unbounded.
cvx_end



