% Adapted from Exercise 4.3: Solve a simple QP with inequality constraints
% From Boyd & Vandenberghe, "Convex Optimization"
% Joëlle Skaf - 09/26/05
%
% Solves the following QP with inequality constraints:
%           minimize    1/2x'*P*x + q'*x + r
%               s.t.    -1 <= x_i <= 1      for i = 1,2,3

P = [13 12 -2; 12 17 6; -2 6 12];
q = [-22; -14.5; 13];
r = 1;
n = 3;
x_lower = -1;
x_upper = 1;

% Construct and solve the model
cvx_begin
    variable x(n)
    minimize ( (1/2)*quad_form(x,P) + q'*x + r )
    x >= x_lower;
    x <= x_upper;
cvx_end

fprintf('The computed optimal solution is (%.1f, %.1f, %.1f)\n', x(1), ...
  x(2), x(3))

