Skip to content

Instantly share code, notes, and snippets.

@rctay
Created December 16, 2012 21:59
Show Gist options
  • Save rctay/4313566 to your computer and use it in GitHub Desktop.
Save rctay/4313566 to your computer and use it in GitHub Desktop.
check quadratic formulation in symmetric Q
function [y,x] = make_quad(Q)
n=size(Q,1);
x=sym('x', [n 1]);
assume(x,'real');
y=simplify(1/2 * x' * Q * x);
end
>> % Consider f(x) = x1^2 + 3x1x2 + x2^2. We want to express this as 1/2 * x' * Q * x.
>> % I think this Q should be right:
>> Q=[2 3;3 2]
Q =
2 3
3 2
>> % Let's verify:
>> [y,x]=make_quad(Q)
y =
x1^2 + 3*x1*x2 + x2^2
x =
x1
x2
>> % Guess I was right!
>> % It should work on larger problems too:
>> Q=[ 1 0 3/2
0 0 5/2
3/2 5/2 10];
>> [y,x]=check_Q(Q)
y =
x1^2/2 + (3*x1*x3)/2 + 5*x3^2 + (5*x2*x3)/2
x =
x1
x2
x3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment