http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681
MATLAB Central Newsreader  Quadratic Cost Function x^T Q x
Feed for thread: Quadratic Cost Function x^T Q x
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Thu, 20 May 2010 15:50:21 +0000
Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747192
Jason
Hello,<br>
<br>
I have a (quadratic) nonlinear problem of the type x^T Q x.<br>
<br>
Specifically the nonconvex objective function is given by<br>
<br>
J = SUM x^T Qi x^2<br>
<br>
for i <= 3.<br>
<br>
My goal is to find arg min x, i.e. the values for the vector x.<br>
<br>
Q is a symmetric 3 x 3 matrix and x is a vector of length 3. This is why i <= 3 since we have 3 parameters (unknowns) in x.<br>
<br>
I am currently using lsqnonlin to find a solution. However I am getting trapped in local minima if I don't use a good starting guess for vector x. <br>
<br>
My question is, can I use something more 'robust' than lsqnonlin?<br>
Furthermore is there actually a global minimum in my problem? <br>
<br>
What would be the best way to find it?<br>
<br>
Many thanks.

Thu, 20 May 2010 16:39:04 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747211
Matt J
"Jason" <jf203@ic.ac.uk> wrote in message <ht3lnt$92i$1@fred.mathworks.com>...<br>
> Hello,<br>
> <br>
> I have a (quadratic) nonlinear problem of the type x^T Q x.<br>
> <br>
> Specifically the nonconvex objective function is given by<br>
> <br>
> J = SUM x^T Qi x^2<br>
> <br>
> for i <= 3.<br>
> <br>
> My goal is to find arg min x, i.e. the values for the vector x.<br>
===============<br>
<br>
The sum is over i? If so, it is trivial to see that there is a global minimum at x=0.

Thu, 20 May 2010 18:05:20 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747236
Jason
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <ht3oj8$gpr$1@fred.mathworks.com>...<br>
> "Jason" <jf203@ic.ac.uk> wrote in message <ht3lnt$92i$1@fred.mathworks.com>...<br>
> > Hello,<br>
> > <br>
> > I have a (quadratic) nonlinear problem of the type x^T Q x.<br>
> > <br>
> > Specifically the nonconvex objective function is given by<br>
> > <br>
> > J = SUM x^T Qi x^2<br>
> > <br>
> > for i <= 3.<br>
> > <br>
> > My goal is to find arg min x, i.e. the values for the vector x.<br>
> ===============<br>
> <br>
> The sum is over i? If so, it is trivial to see that there is a global minimum at x=0.<br>
<br>
Why is there a global minimum at x=0 ??<br>
<br>
Maybe I haven't expressed myself right. Qi is a matrix of 3x3, x = column vector of length 3. The result should be 0 if the correct values for x are chosen.<br>
<br>
What I mean with the sum is... x^T (Q_1 + Q_2 + ... Q_i) x<br>
<br>
Let me show you the code to avoid confusion:<br>
<br>
(Note that I need to feed in Q from another function since it changes, Q in this case is a cell of matrices Q.)<br>
<br>
<br>
function F = opt_routine(x,sns,Q);<br>
for i = 1:sns<br>
F(i) = (x.')*Q{i}*x;<br>
end<br>
end<br>
<br>
We want to find the values for x so:<br>
variable1 = @(x)opt_routine(x,sns,Q);<br>
<br>
[x,res] = lsqnonlin(variable1,[] ....)<br>
<br>
Ok ?

Thu, 20 May 2010 18:43:06 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747257
Matt J
"Jason" <jf203@ic.ac.uk> wrote in message <ht3tl0$mbi$1@fred.mathworks.com>...<br>
<br>
> Why is there a global minimum at x=0 ??<br>
> <br>
> Maybe I haven't expressed myself right. Qi is a matrix of 3x3, x = column vector of length 3. The result should be 0 if the correct values for x are chosen.<br>
> <br>
> What I mean with the sum is... x^T (Q_1 + Q_2 + ... Q_i) x<br>
=====================<br>
<br>
What you wrote here is not consistent with the code you show below. If the function you are trying to minimize is<br>
<br>
f(x) = x^T (Q_1 + Q_2 + ... Q_i) x<br>
<br>
Then with A=(Q_1 + Q_2 + ... Q_i), this is a very basic quadratic minimization<br>
<br>
f(x)= x'*A*x<br>
<br>
If A is positive definite and x is unconstrained, then this function is minimized trivially at x=0. If A is not positive definite and x is unconstrained, this function is not bounded from below, i.e., no global minimum exists. All of this so far is standard theory.<br>
<br>
The code below, on the other hand, is as I originally understood your post. According to the code, you want to minimize the function<br>
<br>
f(x) = ( x'*Q_1*x )^2 + ( x'*Q_2*x )^2 + ( x'*Q_3*x )^2 <br>
<br>
This function satisfies f(0)=0. It is also clear that this is a global minimum because <br>
f(x)>=0 for all x (because f(x) is the sum of positive terms).<br>
<br>
==================<br>
> Let me show you the code to avoid confusion:<br>
> <br>
> (Note that I need to feed in Q from another function since it changes, Q in this case is a cell of matrices Q.)<br>
> <br>
> <br>
> function F = opt_routine(x,sns,Q);<br>
> for i = 1:sns<br>
> F(i) = (x.')*Q{i}*x;<br>
> end<br>
> end<br>
> <br>
> We want to find the values for x so:<br>
> variable1 = @(x)opt_routine(x,sns,Q);<br>
> <br>
> [x,res] = lsqnonlin(variable1,[] ....)<br>
> <br>
> Ok ?

Thu, 20 May 2010 18:44:05 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747258
Roger Stafford
"Jason" <jf203@ic.ac.uk> wrote in message <ht3tl0$mbi$1@fred.mathworks.com>...<br>
> <br>
> Why is there a global minimum at x=0 ??<br>
> <br>
> Maybe I haven't expressed myself right. Qi is a matrix of 3x3, x = column vector of length 3. The result should be 0 if the correct values for x are chosen.<br>
> <br>
> What I mean with the sum is... x^T (Q_1 + Q_2 + ... Q_i) x<br>
> <br>
> Let me show you the code to avoid confusion:<br>
> <br>
> (Note that I need to feed in Q from another function since it changes, Q in this case is a cell of matrices Q.)<br>
> <br>
> <br>
> function F = opt_routine(x,sns,Q);<br>
> for i = 1:sns<br>
> F(i) = (x.')*Q{i}*x;<br>
> end<br>
> end<br>
> <br>
> We want to find the values for x so:<br>
> variable1 = @(x)opt_routine(x,sns,Q);<br>
> <br>
> [x,res] = lsqnonlin(variable1,[] ....)<br>
> <br>
> Ok ?<br>
<br>
You will have to explain yourself better than you have, Jason.<br>
<br>
As things stand, setting all the three elements of x equal to zero guarantees that x'*Qi*x will be the scalar zero, and therefore each component of the sum in J would be zero. Hence J would be zero and it can't be less than zero, so zero would be the minimum. This will remain true until you place some constraint on acceptable x vectors that rules out having all its elements equal to zero.<br>
<br>
If you really mean what you say in x^T (Q_1 + Q_2 + ... Q_i) x, then the eigenvector technique I gave you would still work as applied to the sum of the Q's. However, if you actually mean J is the sum of the squares of the x'*Qi*x scalars, that is an entirely different matter. Which is it you mean?<br>
<br>
Roger Stafford

Thu, 20 May 2010 19:08:04 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747273
Jason
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <ht3vtl$o1d$1@fred.mathworks.com>...<br>
> "Jason" <jf203@ic.ac.uk> wrote in message <ht3tl0$mbi$1@fred.mathworks.com>...<br>
> > <br>
> > Why is there a global minimum at x=0 ??<br>
> > <br>
> > Maybe I haven't expressed myself right. Qi is a matrix of 3x3, x = column vector of length 3. The result should be 0 if the correct values for x are chosen.<br>
> > <br>
> > What I mean with the sum is... x^T (Q_1 + Q_2 + ... Q_i) x<br>
> > <br>
> > Let me show you the code to avoid confusion:<br>
> > <br>
> > (Note that I need to feed in Q from another function since it changes, Q in this case is a cell of matrices Q.)<br>
> > <br>
> > <br>
> > function F = opt_routine(x,sns,Q);<br>
> > for i = 1:sns<br>
> > F(i) = (x.')*Q{i}*x;<br>
> > end<br>
> > end<br>
> > <br>
> > We want to find the values for x so:<br>
> > variable1 = @(x)opt_routine(x,sns,Q);<br>
> > <br>
> > [x,res] = lsqnonlin(variable1,[] ....)<br>
> > <br>
> > Ok ?<br>
> <br>
> You will have to explain yourself better than you have, Jason.<br>
> <br>
> As things stand, setting all the three elements of x equal to zero guarantees that x'*Qi*x will be the scalar zero, and therefore each component of the sum in J would be zero. Hence J would be zero and it can't be less than zero, so zero would be the minimum. This will remain true until you place some constraint on acceptable x vectors that rules out having all its elements equal to zero.<br>
> <br>
> If you really mean what you say in x^T (Q_1 + Q_2 + ... Q_i) x, then the eigenvector technique I gave you would still work as applied to the sum of the Q's. However, if you actually mean J is the sum of the squares of the x'*Qi*x scalars, that is an entirely different matter. Which is it you mean?<br>
> <br>
> Roger Stafford<br>
<br>
Dear Roger,<br>
<br>
Thanks for your reply.<br>
<br>
I think it was quite clear from the description, and please see the code. lsqnonlin does compute the sum of the squares.<br>
<br>
This is the reason why I wrote J = sum  x^T Qi x ^2<br>
<br>
I am referring indeed to the last case you outlined.<br>
Do you have any suggestions for that?<br>
<br>
Many thanks

Thu, 20 May 2010 19:25:21 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747285
Matt J
"Jason" <jf203@ic.ac.uk> wrote in message <ht41ak$at$1@fred.mathworks.com>...<br>
<br>
> I think it was quite clear from the description, and please see the code. lsqnonlin does compute the sum of the squares.<br>
> <br>
> This is the reason why I wrote J = sum  x^T Qi x ^2<br>
> <br>
> I am referring indeed to the last case you outlined.<br>
> Do you have any suggestions for that?<br>
============<br>
<br>
As both Roger and I have suggested already, this function is globally minimized by x=0, so long as x is unconstrained. This is because, by direct evaluation, J=0 for x=0. Also, it is clear that there is no x for which J<0 because J is the sum of positive terms for all x.<br>
<br>
It appears as though some constraints are needed to make this problem nontrivial.

Thu, 20 May 2010 19:33:04 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747292
Roger Stafford
"Jason" <jf203@ic.ac.uk> wrote in message <ht41ak$at$1@fred.mathworks.com>...<br>
> I think it was quite clear from the description, ......<br>
<br>
No, I disagree with you; your explanation is not at all clear, Jason! You have yet to address the problem of placing constraints on x. Three times that question has been posed to you. Why are you so reticent about that matter? I can make no more suggestions until receiving some statement on your part as to this question.<br>
<br>
Roger Stafford

Thu, 20 May 2010 21:22:04 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747350
Jason
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <ht42pg$8he$1@fred.mathworks.com>...<br>
> "Jason" <jf203@ic.ac.uk> wrote in message <ht41ak$at$1@fred.mathworks.com>...<br>
> > I think it was quite clear from the description, ......<br>
> <br>
> No, I disagree with you; your explanation is not at all clear, Jason! You have yet to address the problem of placing constraints on x. Three times that question has been posed to you. Why are you so reticent about that matter? I can make no more suggestions until receiving some statement on your part as to this question.<br>
> <br>
> Roger Stafford<br>
<br>
Dear Roger,<br>
<br>
Without placing constraints on x lsqnonlin runs, and *might* give you the correct result, i.e. the global minimum.<br>
<br>
One way to address the problem was to place a constraint on x such that x(1) and x(2) lie on a unit circle. There is no constraint on x(3).<br>
<br>
This would reduce the problem to two variables.<br>
<br>
if x(1) = cos(alpha) and x(2) = sin(alpha) then the vector x would be:<br>
<br>
x = [alpha x(3)].<br>
<br>
This works fine using lsqnonlin, however it still does not guarantee that I won't get trapped in a local (suboptimal) minimum.<br>
<br>
Think of the vector x as a line in geometrical 2D space. I want to estimate the parameters of this line, i.e. x(1)*x + x(2)*y + x(3).<br>
<br>
Pardon my ignorance, but even without placing any constraints on x you get a nontrivial solution (i.e. not x = 0).<br>
<br>
This is why I reformulated my statement, I am not reticent about the matter.<br>
<br>
By the way, I have tried fmincon using the nonlinear constraint x(1)^2 + x(2)^2 1 = 0 but (barring any programming error from my side) did not provide correct results.<br>
<br>
<br>
My main question is if I can use something different than lsqnonlin which takes a very long time to run and is prone to fall into suboptimal minima.<br>
<br>
Best regards,<br>
Jason

Thu, 20 May 2010 21:47:14 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747364
Matt J
"Jason" <jf203@ic.ac.uk> wrote in message <ht495s$eq2$1@fred.mathworks.com>...<br>
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <br>
<br>
> Think of the vector x as a line in geometrical 2D space. I want to estimate the parameters of this line, i.e. x(1)*x + x(2)*y + x(3).<br>
><br>
> Pardon my ignorance, but even without placing any constraints on x you get a nontrivial solution (i.e. not x = 0).<br>
===============<br>
<br>
But the cost function for this estimation problem bears no resemblance to the one you posed.<br>
<br>
<br>
> <br>
> My main question is if I can use something different than lsqnonlin which takes a very long time to run and is prone to fall into suboptimal minima.<br>
=================<br>
<br>
You might try running lsqnonlin with the Algorithm option to 'levenbergmarquardt'. My intuition about the default trustregion method is that, even if you initialize in the correct capture basin, the algorithm can crawl out of it into something less optimal. Conversely, LevenbergMarquardt is, according to my intuition, may be more basinpreserving. Also, if you supply an analytical gradient then, according to doc lsqnonlin, it could be more computationally cheap per iteration. <br>
<br>
All of this assumes that you have at least a reasonable guess of the initial solution, but there's no escaping that when it comes to nonconvex minimization.

Thu, 20 May 2010 22:07:05 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747368
Jason
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <ht4al2$jcc$1@fred.mathworks.com>...<br>
> "Jason" <jf203@ic.ac.uk> wrote in message <ht495s$eq2$1@fred.mathworks.com>...<br>
> > "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <br>
> <br>
> > Think of the vector x as a line in geometrical 2D space. I want to estimate the parameters of this line, i.e. x(1)*x + x(2)*y + x(3).<br>
> ><br>
> > Pardon my ignorance, but even without placing any constraints on x you get a nontrivial solution (i.e. not x = 0).<br>
> ===============<br>
> <br>
> But the cost function for this estimation problem bears no resemblance to the one you posed.<br>
> <br>
> <br>
> > <br>
> > My main question is if I can use something different than lsqnonlin which takes a very long time to run and is prone to fall into suboptimal minima.<br>
> =================<br>
> <br>
> You might try running lsqnonlin with the Algorithm option to 'levenbergmarquardt'. My intuition about the default trustregion method is that, even if you initialize in the correct capture basin, the algorithm can crawl out of it into something less optimal. Conversely, LevenbergMarquardt is, according to my intuition, may be more basinpreserving. Also, if you supply an analytical gradient then, according to doc lsqnonlin, it could be more computationally cheap per iteration. <br>
> <br>
> All of this assumes that you have at least a reasonable guess of the initial solution, but there's no escaping that when it comes to nonconvex minimization.<br>
<br>
I will try the levenbergmarquardt option, thanks!<br>
<br>
As for your inquiry, what do you mean?<br>
<br>
The cost function is still the same.<br>
<br>
As I said J = min x ( x' Q x).<br>
<br>
I am following a principle from computer vision where Q is the matrix representation of a conic (an ellipse). If you set Q = adj(Q) (where adj means adjoint) then x' Q x = 0 means that if this equation is satisfied, then the line parametrized by x is tangent to the ellipse. The additional constraint i have gaven, namely that x(1) and x(2) lie on a unit circle only helps but is not necessary.<br>
<br>
Regards,<br>
Jason

Thu, 20 May 2010 22:56:04 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747387
Matt J
"Jason" <jf203@ic.ac.uk> wrote in message <ht4bq9$4t9$1@fred.mathworks.com>...<br>
<br>
> <br>
> I will try the levenbergmarquardt option, thanks!<br>
> <br>
> As for your inquiry, what do you mean?<br>
> <br>
> The cost function is still the same.<br>
> <br>
> As I said J = min x ( x' Q x).<br>
> <br>
> I am following a principle from computer vision where Q is the matrix representation of a conic (an ellipse). If you set Q = adj(Q) (where adj means adjoint) then x' Q x = 0 means that if this equation is satisfied, then the line parametrized by x is tangent to the ellipse. The additional constraint i have gaven, namely that x(1) and x(2) lie on a unit circle only helps but is not necessary.<br>
=================<br>
<br>
You mean x is a vector of homogeneous coordinates!?!<br>
<br>
This seems like a really dubious idea. If all this is really just to fit a line, why don't you just use polyfit? It would be much faster and more robust.<br>
<br>
At any rate, if you don't constrain homogeneous coordinates in some way, your minimization problem is virtually guaranteed to be illconditioned. In the case of the line<br>
<br>
x(1)*X+x(2)*Y+x(3)<br>
<br>
for any solution x, another solution is c*x for any scalar c. This tends to create problems for optimization algorithms.

Thu, 20 May 2010 23:09:04 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747391
Jason
> You mean x is a vector of homogeneous coordinates!?!<br>
> <br>
> This seems like a really dubious idea. If all this is really just to fit a line, why don't you just use polyfit? It would be much faster and more robust.<br>
> <br>
> At any rate, if you don't constrain homogeneous coordinates in some way, your minimization problem is virtually guaranteed to be illconditioned. In the case of the line<br>
> <br>
> x(1)*X+x(2)*Y+x(3)<br>
> <br>
> for any solution x, another solution is c*x for any scalar c. This tends to create problems for optimization algorithms. <br>
<br>
Yes, x is a vector of homogeneous coordinates!<br>
<br>
I agree about what you say: "for any solution x, another solution is c*x for any scalar c." I experienced this, but what can I do about it?<br>
<br>
I am reading about how to do this using polyfit, I have no idea how to start, since as I said I want to compute the common tangent to a set of ellipses which is solved if x'Qx = 0.<br>
<br>
Say I have a set of ellipses in either matrix form, or of course I can write it out as a polynomial, how do I know if the line I am trying to fit is tangent to these ellipses?

Thu, 20 May 2010 23:30:24 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747399
Jason
I have given this some thought, I don't think polyfit is applicable for my purposes.<br>
<br>
I want an analytical solution first of all, and secondly I don't know how to compute the common tangent to say 4 ellipses using a method other than the one I have outlined.<br>
<br>
Many thanks though.

Fri, 21 May 2010 03:32:04 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747439
Matt J
"Jason" <jf203@ic.ac.uk> wrote in message <ht4feg$q4b$1@fred.mathworks.com>...<br>
> > You mean x is a vector of homogeneous coordinates!?!<br>
<br>
> I agree about what you say: "for any solution x, another solution is c*x for any scalar c." I experienced this, but what can I do about it?<br>
> <br>
======================<br>
<br>
Well, you've already answered that question. By applying the constraint<br>
x(1)^2+x(2)^2=1 for example, not only do you get rid of the continuous 1D space of nonunique solutions c*x, but also the degenerate solution x=0 is excluded from the search. <br>
<br>
I'm emphasizing this because you still seem to think the constraint is unnecessary, when in fact it has very important consequences for the performance of the minimization algorithm.<br>
<br>
<br>
> I am reading about how to do this using polyfit, I have no idea how to start, since as I said I want to compute the common tangent to a set of ellipses which is solved if x'Qx = 0.<br>
===============<br>
<br>
No, this is the first time you've explicitly said anything about multiple ellipses. However, now that I understand the problem, I have a few more options to recommend.<br>
<br>
First, you can divide the problem into a minimization over two separate regions<br>
<br>
(1) The region where we constrain x(3)=0, i.e. where the tangent line passes through the origin.<br>
<br>
(2) The region where we constrain x(3)=1, i.e., the region where the tangent line does not pass through the origin. Note that this is equivalent to minimizing over the entire region x(3)~=0 because in homogeneous coordinates x(3)~=0 and x(3)=1 form an equivalence class.<br>
<br>
Once you've found the minimum in each case, you can pick the one with the least value of J.<br>
<br>
Once we've constrained x(3) either to 0 or 1, there are a few consequences<br>
<br>
(I) The cost function J(x) reduces to a 2D function of x(1) and x(2)<br>
<br>
(II) It becomes relatively easy to search the 2D space for the global solution by parametrizing the space as <br>
x(1) = t*cosd(alpha) <br>
x(2) = t*sind(alpha)<br>
<br>
For each fixed alpha, the cost function J reduces to a 4th order 1D polynomial in t, which can therefore be minimized rapidly, analytically, and globally in t. I can do this in a loop over a finely sampled range of angles alpha, say alpha=0:0.1:180, until I find a good approximation of the global minimum over all alpha and t and hence over all <br>
x(1) and x(2). This should be fairly quick, considering how easy it is to minimize 1D polynomials<br>
<br>
A related approach, but slightly less robust, would be to code your own version of the coordinate descent algorithm. In this algorithm, you alternatingly minimize over each <br>
x(i) while the other x(j) are fixed. Here again J reduces to a quartic polynomial in x(i) which can be globally/analytically minimized in each iteration. Because you are globally minimizing at each step, you have a better than average chance of not getting stuck in a local min. This approach can be applied either to the original 3D problem or one of the above reductions to 2D, although the latter would be better for reasons already discussed.

Fri, 21 May 2010 05:48:04 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747460
Jason
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <ht4urj$p85$1@fred.mathworks.com>...<br>
> "Jason" <jf203@ic.ac.uk> wrote in message <ht4feg$q4b$1@fred.mathworks.com>...<br>
> > > You mean x is a vector of homogeneous coordinates!?!<br>
> <br>
> > I agree about what you say: "for any solution x, another solution is c*x for any scalar c." I experienced this, but what can I do about it?<br>
> > <br>
> ======================<br>
> <br>
> Well, you've already answered that question. By applying the constraint<br>
> x(1)^2+x(2)^2=1 for example, not only do you get rid of the continuous 1D space of nonunique solutions c*x, but also the degenerate solution x=0 is excluded from the search. <br>
> <br>
> I'm emphasizing this because you still seem to think the constraint is unnecessary, when in fact it has very important consequences for the performance of the minimization algorithm.<br>
> <br>
> <br>
> > I am reading about how to do this using polyfit, I have no idea how to start, since as I said I want to compute the common tangent to a set of ellipses which is solved if x'Qx = 0.<br>
> ===============<br>
> <br>
> No, this is the first time you've explicitly said anything about multiple ellipses. However, now that I understand the problem, I have a few more options to recommend.<br>
> <br>
> First, you can divide the problem into a minimization over two separate regions<br>
> <br>
> (1) The region where we constrain x(3)=0, i.e. where the tangent line passes through the origin.<br>
> <br>
> (2) The region where we constrain x(3)=1, i.e., the region where the tangent line does not pass through the origin. Note that this is equivalent to minimizing over the entire region x(3)~=0 because in homogeneous coordinates x(3)~=0 and x(3)=1 form an equivalence class.<br>
> <br>
> Once you've found the minimum in each case, you can pick the one with the least value of J.<br>
> <br>
> Once we've constrained x(3) either to 0 or 1, there are a few consequences<br>
> <br>
> (I) The cost function J(x) reduces to a 2D function of x(1) and x(2)<br>
> <br>
> (II) It becomes relatively easy to search the 2D space for the global solution by parametrizing the space as <br>
> x(1) = t*cosd(alpha) <br>
> x(2) = t*sind(alpha)<br>
> <br>
> For each fixed alpha, the cost function J reduces to a 4th order 1D polynomial in t, which can therefore be minimized rapidly, analytically, and globally in t. I can do this in a loop over a finely sampled range of angles alpha, say alpha=0:0.1:180, until I find a good approximation of the global minimum over all alpha and t and hence over all <br>
> x(1) and x(2). This should be fairly quick, considering how easy it is to minimize 1D polynomials<br>
> <br>
> A related approach, but slightly less robust, would be to code your own version of the coordinate descent algorithm. In this algorithm, you alternatingly minimize over each <br>
> x(i) while the other x(j) are fixed. Here again J reduces to a quartic polynomial in x(i) which can be globally/analytically minimized in each iteration. Because you are globally minimizing at each step, you have a better than average chance of not getting stuck in a local min. This approach can be applied either to the original 3D problem or one of the above reductions to 2D, although the latter would be better for reasons already discussed.<br>
<br>
Dear Matt, <br>
<br>
Your reply was excellent. This is exactly what I have implemented now.<br>
<br>
Many thanks.<br>
<br>
Kind regards,<br>
Jason

Fri, 21 May 2010 05:55:22 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747462
Bruno Luong
"Jason" <jf203@ic.ac.uk> wrote in message <ht4bq9$4t9$1@fred.mathworks.com>...<br>
><br>
> <br>
> The cost function is still the same.<br>
> <br>
> As I said J = min x ( x' Q x).<br>
> <br>
> I am following a principle from computer vision where Q is the matrix representation of a conic (an ellipse). If you set Q = adj(Q) (where adj means adjoint) then x' Q x = 0 means that if this equation is satisfied, then the line parametrized by x is tangent to the ellipse. The additional constraint i have gaven, namely that x(1) and x(2) lie on a unit circle only helps but is not necessary.<br>
<br>
Can you parametrize the "lines" with the constraints x(1)^2+x^2+x(3)^3 = 1 Rather than just the first two? Seem like it make more symmetry to me.<br>
<br>
In any case, such Quadratic programming with norm constraint can be solved by the similar approach I used in my FEX submission:<br>
<br>
<a href="http://www.mathworks.com/matlabcentral/fileexchange/27596leastsquarewith2normconstraint">http://www.mathworks.com/matlabcentral/fileexchange/27596leastsquarewith2normconstraint</a><br>
<br>
I believe it is possible to extend the method of norm constraint to seminorm constraint.<br>
<br>
Bruno

Fri, 21 May 2010 10:33:03 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747517
Matt J
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ht578a$n72$1@fred.mathworks.com>...<br>
<br>
> Can you parametrize the "lines" with the constraints x(1)^2+x^2+x(3)^3 = 1 Rather than just the first two? Seem like it make more symmetry to me.<br>
> <br>
> In any case, such Quadratic programming with norm constraint can be solved by the similar approach I used in my FEX submission:<br>
> <br>
> <a href="http://www.mathworks.com/matlabcentral/fileexchange/27596leastsquarewith2normconstraint">http://www.mathworks.com/matlabcentral/fileexchange/27596leastsquarewith2normconstraint</a><br>
> <br>
> I believe it is possible to extend the method of norm constraint to seminorm constraint.<br>
=========<br>
<br>
But it is not a quadratic program, Bruno. The objective function is quartic.

Fri, 21 May 2010 11:09:04 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747536
Bruno Luong
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <ht5ngv$mot$1@fred.mathworks.com>...<br>
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <br>
> <br>
> But it is not a quadratic program, Bruno. The objective function is quartic.<br>
<br>
No, in fact they are the same problem: in both cases a quadratic functional is minimized. The formulation in QEP is derived from the Euler Lagrange condition. AFAICS, the calculation *should* be transposed more or less straight forward.<br>
<br>
I let OP work on it first, if he get stuck I can give a hand.<br>
<br>
Bruno

Fri, 21 May 2010 11:23:05 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747543
Bruno Luong
Well, actually I'm confused. The reason I think it's quadratic because I quote (from OP):<br>
<br>
'As I said J = min x ( x' Q x).'<br>
<br>
OR in another thread<br>
<br>
[ If I have a quadratic optimization problem of the type<br>
<br>
min(x) x' Q x<br>
<br>
Where Q is symmetric but indefinite (3x3) matrix, and x is column vector of length 3.<br>
<br>
Fmincon and quadprog could solve this. ]<br>
<br>
So what exactly he wants to minimize? It seems Jason confused many people, and even Roger has left the thread.<br>
<br>
Bruno

Fri, 21 May 2010 11:59:05 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747551
Jason
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ht5qeo$26u$1@fred.mathworks.com>...<br>
> Well, actually I'm confused. The reason I think it's quadratic because I quote (from OP):<br>
> <br>
> 'As I said J = min x ( x' Q x).'<br>
> <br>
> OR in another thread<br>
> <br>
> [ If I have a quadratic optimization problem of the type<br>
> <br>
> min(x) x' Q x<br>
> <br>
> Where Q is symmetric but indefinite (3x3) matrix, and x is column vector of length 3.<br>
> <br>
> Fmincon and quadprog could solve this. ]<br>
> <br>
> So what exactly he wants to minimize? It seems Jason confused many people, and even Roger has left the thread.<br>
> <br>
> Bruno <br>
<br>
Bruno, it is quite clearly quadratic.<br>
And it's quite clear that you minimize x, i.e. find the values of x that minimize x' Q x.<br>
<br>
If something is not clear in your mind then don't blame me.<br>
<br>
Using Matt's approach you can find a solution quickly and efficiently.<br>
<br>
He proposed constraining x_3, but you might just consider the subspace x_1 = 1 (which excludes the solution x_1 = 0) so you can combine it with the minimization on a different hyperplane that includes x_1 = 0.<br>
<br>
In any case it is possible to obtain a closed form solution and in order to find the global minimum you can find the stationary points of the two cost functions created for each subspace.<br>
<br>
Thanks for your help, but I don't think it's in my scope to try it out, I want something from scratch and not hacking a ready code, which I am not even sure is applicable for my purposes.<br>
<br>
Best regards,<br>
Jason

Fri, 21 May 2010 12:03:04 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747552
Matt J
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ht5qeo$26u$1@fred.mathworks.com>...<br>
<br>
> So what exactly he wants to minimize? It seems Jason confused many people, and even Roger has left the thread.<br>
==========<br>
<br>
This is the objective function<br>
<br>
f(x) = ( x'*Q_1*x )^2 + ( x'*Q_2*x )^2 + ( x'*Q_3*x )^2 <br>
<br>
with some constraint to normalize the magnitude of x, e.g. we have looked at<br>
<br>
norm(x)=1<br>
norm(x(1:2))=1<br>
x(3)=1<br>
<br>
etc...

Fri, 21 May 2010 12:24:04 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747557
Bruno Luong
"Jason" <jf203@ic.ac.uk> wrote in message <ht5si9$ipb$1@fred.mathworks.com>...<br>
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ht5qeo$26u$1@fred.mathworks.com>...<br>
> > Well, actually I'm confused. The reason I think it's quadratic because I quote (from OP):<br>
> > <br>
> > 'As I said J = min x ( x' Q x).'<br>
> > <br>
> > OR in another thread<br>
> > <br>
> > [ If I have a quadratic optimization problem of the type<br>
> > <br>
> > min(x) x' Q x<br>
> > <br>
> > Where Q is symmetric but indefinite (3x3) matrix, and x is column vector of length 3.<br>
> > <br>
> > Fmincon and quadprog could solve this. ]<br>
> > <br>
> > So what exactly he wants to minimize? It seems Jason confused many people, and even Roger has left the thread.<br>
> > <br>
> > Bruno <br>
> <br>
> Bruno, it is quite clearly quadratic.<br>
> And it's quite clear that you minimize x, i.e. find the values of x that minimize x' Q x.<br>
> <br>
> If something is not clear in your mind then don't blame me.<br>
<br>
Well, now Jason, can you tell me the relation between:<br>
<br>
1) minimizing x'*Q*x, and <br>
2) x'*Q*x = 0, I quite [ ... common tangent to a set of ellipses which is solved if x'Qx = 0 ]<br>
<br>
And earlier on you wrote:<br>
<br>
[ ... J = SUM x^T Qi x^2 ... ]<br>
<br>
This is clearly quadric functional, not quadratic.<br>
<br>
And one more quote, you gave a function handle passing to LSQNONLIN defined by:<br>
<br>
[<br>
function F = opt_routine(x,sns,Q);<br>
for i = 1:sns<br>
F(i) = (x.')*Q{i}*x;<br>
end<br>
end<br>
]<br>
<br>
Which LSQNONLIN, will intepret as the cost function as (see <a href="http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/lsqnonlin.html">http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/lsqnonlin.html</a>)<br>
<br>
J(x) = sum f(i)^2<br>
= sum ((x.')*Q{i}*x)^2<br>
<br>
Which is QUADRIC and not QUADRATIC.<br>
<br>
But you state in the last post [ ... it is quite clearly quadratic... ]<br>
<br>
There is clearly a confusion in various posts between QUADRIC and QUADRATIC. Who should I blame Jason?<br>
<br>
> <br>
> Thanks for your help, but I don't think it's in my scope to try it out, I want something from scratch and not hacking a ready code, which I am not even sure is applicable for my purposes.<br>
<br>
If it's quadratic, it's applicable, and FYI it can find ALL the solutions in one shot.<br>
<br>
Bruno

Fri, 21 May 2010 12:28:06 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747560
Bruno Luong
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <ht5spo$4oc$1@fred.mathworks.com>...<br>
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ht5qeo$26u$1@fred.mathworks.com>...<br>
> <br>
> > So what exactly he wants to minimize? It seems Jason confused many people, and even Roger has left the thread.<br>
> ==========<br>
> <br>
> This is the objective function<br>
> <br>
> f(x) = ( x'*Q_1*x )^2 + ( x'*Q_2*x )^2 + ( x'*Q_3*x )^2 <br>
> <br>
<br>
But Jason just tells us [... it is quite clearly quadratic.]!!! And how on earth he can use QUADPROG for this function?<br>
<br>
I can't still figure out what Jason really wanted.<br>
<br>
Bruno

Fri, 21 May 2010 12:40:20 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747568
Bruno Luong
Sorry for the typo, *QUARTIC* and not QUADRIC<br>
<br>
Bruno

Fri, 21 May 2010 13:34:21 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747595
Matt J
"Jason" <jf203@ic.ac.uk> wrote in message <ht5si9$ipb$1@fred.mathworks.com>...<br>
<br>
> <br>
> Bruno, it is quite clearly quadratic.<br>
> And it's quite clear that you minimize x, i.e. find the values of x that minimize x' Q x.<br>
> <br>
> If something is not clear in your mind then don't blame me.<br>
=================<br>
<br>
No it is not quadratic. You are trying to find the simultaneous *root* of 3 quadratic functions (one for each ellipse), not their minimum. To do this, you wrote down a cost function J in which you square all the quadratics for each ellipse and add them up. This leads to a 4th order function polynomial in x, not a 2nd order one

Fri, 21 May 2010 15:28:06 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747647
Jason
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <ht624t$p6b$1@fred.mathworks.com>...<br>
> "Jason" <jf203@ic.ac.uk> wrote in message <ht5si9$ipb$1@fred.mathworks.com>...<br>
> <br>
> > <br>
> > Bruno, it is quite clearly quadratic.<br>
> > And it's quite clear that you minimize x, i.e. find the values of x that minimize x' Q x.<br>
> > <br>
> > If something is not clear in your mind then don't blame me.<br>
> =================<br>
> <br>
> No it is not quadratic. You are trying to find the simultaneous *root* of 3 quadratic functions (one for each ellipse), not their minimum. To do this, you wrote down a cost function J in which you square all the quadratics for each ellipse and add them up. This leads to a 4th order function polynomial in x, not a 2nd order one<br>
<br>
Well it depends on how you define your cost function. lsqnonlin will automatically sum of squares. other methods might not.<br>
<br>
In any case, Bruno, I wanted to ask you something else.<br>
<br>
I did what you said, i.e. setting x_3 = 1 and x_3 = 0 and then compute two different cost functions.<br>
<br>
Also you said we can set x_1 = t * cosd (alpha) and x_2 = t * sind (alpha).<br>
<br>
This means that x_1 = x_2 cosd(alpha)/sind(alpha) right ? So if we set beta = cosd(alpha)/sind(alpha) then we have in the first case (x_3 = 0):<br>
<br>
J_1 (x_2) = [beta*x_2 x_2 0]*Q*[[beta*x_2 x_2 0]^T<br>
<br>
If I do the multiplications correctly then that gives me a polynomial of degree 2 ?! Of course lsqnonlin will sum the squares which would make this degree 4, but what I am saying is correct right?<br>
<br>
Also you said I should compute J_1 for each value of alpha from 0:0.1:180 which would make the problem about 1800 iterations for finding x_2. I can then simply find x_1 by computing beta*x_2 and we know that x_3 in this case is 0. Repeat all this for the case that x_3 is 1. Which means 3600 iterations in total.<br>
<br>
Have I understood you correctly?<br>
<br>
The part which troubles me a bit is when you say: " I can do this in a loop over a finely sampled range of angles alpha, say alpha=0:0.1:180, until I find a good approximation of the global minimum over all alpha and t and hence over all <br>
x(1) and x(2)."<br>
<br>
Over all alpha, ok, but over all t ? Haven't I taken t out of the picture here?<br>
<br>
Kind regards,<br>
Jason

Sat, 22 May 2010 15:41:04 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747879
Matt J
"Jason" <jf203@ic.ac.uk> wrote in message <ht68q6$mcu$1@fred.mathworks.com>...<br>
<br>
> Well it depends on how you define your cost function. lsqnonlin will automatically sum of squares. other methods might not.<br>
=====================<br>
<br>
That's true, but the cost function you've defined here, and the only one we've been talking about in this thread, is the one used by lsqnonlin. This is for good reason. It would be highly nonstandard and take considerable ingenuity to find a simpler cost function for this rootfinding problem than what lsqnonlin uses. <br>
<br>
<br>
> In any case, Bruno, I wanted to ask you something else.<br>
==============<br>
<br>
Not Bruno. Me.<br>
<br>
<br>
> I did what you said, i.e. setting x_3 = 1 and x_3 = 0 and then compute two different cost functions.<br>
><br>
> Also you said we can set x_1 = t * cosd (alpha) and x_2 = t * sind (alpha).<br>
> <br>
> This means that x_1 = x_2 cosd(alpha)/sind(alpha) right ?<br>
================<br>
<br>
This manipulation requires a division operation that is only valid for t~=0 and alpha~=0. <br>
<br>
It was not my intention that you do this. My intention was that you substitute both<br>
x_1 = t * cosd (alpha) and x_2 = t * sind (alpha)<br>
into the cost function so that, with x_3 and alpha held fixed, the cost function would become a function of t, not x_2.<br>
<br>
The result is virtually the same  you end up with a 1D 4th order polynomial to minimize.<br>
However, in your approach, you have to worry about beta being infinite...<br>
<br>
<br>
So if we set beta = cosd(alpha)/sind(alpha) then we have in the first case (x_3 = 0):<br>
> <br>
> J_1 (x_2) = [beta*x_2 x_2 0]*Q*[[beta*x_2 x_2 0]^T<br>
> <br>
> If I do the multiplications correctly then that gives me a polynomial of degree 2 ?! Of course lsqnonlin will sum the squares which would make this degree 4, but what I am saying is correct right?<br>
===================<br>
<br>
You shouldn't be using lsqnonlin for this. lsqnonlin does not know how to minimize anything analytically, even a simple 1D polynomial. You should be writing your own routine to do this.<br>
<br>
To keep this discussion clear, it would also be a good idea if you stop treating lsqnonlin as the thing that's defining the cost function. WE are defining the cost function for this problem and it is the multivariable 4th order polynomial<br>
<br>
J(x)=sum_i (x' * Q{i} *x)^2<br>
<br>
There is no other obvious and more tractable alternative to this cost function for the task you've described.<br>
<br>
<br>
> Also you said I should compute J_1 for each value of alpha from 0:0.1:180 which would make the problem about 1800 iterations for finding x_2. I can then simply find x_1 by computing beta*x_2 and we know that x_3 in this case is 0. Repeat all this for the case that x_3 is 1. Which means 3600 iterations in total.<br>
===============<br>
<br>
One other thing. It has occured to me that you don't really have to do this for the case x_3=0<br>
<br>
In this case, the remaining unknowns x_1 and x_2 can also be normalized with impunity so that x_1=1 or x_2=1 as long as one of these is nonzero. So, you can subdivide the case x_3=0 into 2 more subcases<br>
<br>
(1) x_1=1, x_3=0<br>
(2) x_2=1, x_3=0<br>
<br>
In each of these subcases, the cost function reduces to a 1D polynomial again, which you can minimize trivially.

Sat, 22 May 2010 23:42:04 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#747930
Jason
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <ht8tug$sjj$1@fred.mathworks.com>...<br>
> "Jason" <jf203@ic.ac.uk> wrote in message <ht68q6$mcu$1@fred.mathworks.com>...<br>
> <br>
> > Well it depends on how you define your cost function. lsqnonlin will automatically sum of squares. other methods might not.<br>
> =====================<br>
> <br>
> That's true, but the cost function you've defined here, and the only one we've been talking about in this thread, is the one used by lsqnonlin. This is for good reason. It would be highly nonstandard and take considerable ingenuity to find a simpler cost function for this rootfinding problem than what lsqnonlin uses. <br>
> <br>
> <br>
> > In any case, Bruno, I wanted to ask you something else.<br>
> ==============<br>
> <br>
> Not Bruno. Me.<br>
> <br>
> <br>
> > I did what you said, i.e. setting x_3 = 1 and x_3 = 0 and then compute two different cost functions.<br>
> ><br>
> > Also you said we can set x_1 = t * cosd (alpha) and x_2 = t * sind (alpha).<br>
> > <br>
> > This means that x_1 = x_2 cosd(alpha)/sind(alpha) right ?<br>
> ================<br>
> <br>
> This manipulation requires a division operation that is only valid for t~=0 and alpha~=0. <br>
> <br>
> It was not my intention that you do this. My intention was that you substitute both<br>
> x_1 = t * cosd (alpha) and x_2 = t * sind (alpha)<br>
> into the cost function so that, with x_3 and alpha held fixed, the cost function would become a function of t, not x_2.<br>
> <br>
> The result is virtually the same  you end up with a 1D 4th order polynomial to minimize.<br>
> However, in your approach, you have to worry about beta being infinite...<br>
> <br>
> <br>
> So if we set beta = cosd(alpha)/sind(alpha) then we have in the first case (x_3 = 0):<br>
> > <br>
> > J_1 (x_2) = [beta*x_2 x_2 0]*Q*[[beta*x_2 x_2 0]^T<br>
> > <br>
> > If I do the multiplications correctly then that gives me a polynomial of degree 2 ?! Of course lsqnonlin will sum the squares which would make this degree 4, but what I am saying is correct right?<br>
> ===================<br>
> <br>
> You shouldn't be using lsqnonlin for this. lsqnonlin does not know how to minimize anything analytically, even a simple 1D polynomial. You should be writing your own routine to do this.<br>
> <br>
> To keep this discussion clear, it would also be a good idea if you stop treating lsqnonlin as the thing that's defining the cost function. WE are defining the cost function for this problem and it is the multivariable 4th order polynomial<br>
> <br>
> J(x)=sum_i (x' * Q{i} *x)^2<br>
> <br>
> There is no other obvious and more tractable alternative to this cost function for the task you've described.<br>
> <br>
> <br>
> > Also you said I should compute J_1 for each value of alpha from 0:0.1:180 which would make the problem about 1800 iterations for finding x_2. I can then simply find x_1 by computing beta*x_2 and we know that x_3 in this case is 0. Repeat all this for the case that x_3 is 1. Which means 3600 iterations in total.<br>
> ===============<br>
> <br>
> One other thing. It has occured to me that you don't really have to do this for the case x_3=0<br>
> <br>
> In this case, the remaining unknowns x_1 and x_2 can also be normalized with impunity so that x_1=1 or x_2=1 as long as one of these is nonzero. So, you can subdivide the case x_3=0 into 2 more subcases<br>
> <br>
> (1) x_1=1, x_3=0<br>
> (2) x_2=1, x_3=0<br>
> <br>
> In each of these subcases, the cost function reduces to a 1D polynomial again, which you can minimize trivially.<br>
<br>
Matt, yes, sorry about this!<br>
<br>
Re: My intention was that you substitute both<br>
> x_1 = t * cosd (alpha) and x_2 = t * sind (alpha)<br>
> into the cost function<br>
<br>
Yes, just realized that before you answered. Got you now!<br>
<br>
However I do not understand your last point:<br>
<br>
> One other thing. It has occured to me that you don't really have to do this for the case x_3=0<br>
> <br>
> In this case, the remaining unknowns x_1 and x_2 can also be normalized with impunity so that x_1=1 or x_2=1 as long as one of these is nonzero. So, you can subdivide the case x_3=0 into 2 more subcases.<br>
<br>
Could you elaborate this a little bit please, I'm not sure I get you. You are basically saying than when x_3 = 0 then x_1 and x_2 ~= 0 ?<br>
<br>
I don't really understand.<br>
<br>
Thanks!

Sun, 23 May 2010 14:24:03 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#748007
Matt J
"Jason" <jf203@ic.ac.uk> wrote in message <ht9q4c$jbj$1@fred.mathworks.com>...<br>
<br>
> Could you elaborate this a little bit please, I'm not sure I get you. You are basically saying than when x_3 = 0 then x_1 and x_2 ~= 0 ?<br>
> <br>
> I don't really understand.<br>
=========<br>
<br>
It's the same equivalence class consideration associated with homogeneous coordinates that we already said made the space of solutions with x3~=0 equivalent to the space of solutions with x3=1.<br>
<br>
Once you restrict the search to the space x_3=0, it means you are searching for a solution of the form (x1, x2, 0). Suppose we restrict this search further to the subspace of solutions of the form (x1,x2,0) and for which x1~=0. Each of the solutions in this subspace are equivalent to a solution of the form<br>
(1,x2/x1,0) or more simply (1,z,0). Therefore, you can just as well restrict the search to the subspace (1,x2,0)<br>
<br>
You can do the same thing for the subspace {x2~=0, x3=0}<br>
It is equivalent to the subspace {x2=1, x3=0}

Mon, 19 Jul 2010 18:35:05 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#763763
Jason
I seem to have hit a brickwall, when I initially thought the problem was solved.<br>
<br>
Just to reiterate, we are facing a system of the type (x^T Q x)^2 for which x = [x1,x2,x3] and Q is a 3x3 matrix representing a conic (or more specifically an ellipse). Note that we are dealing with homogenous coordinates.<br>
<br>
The idea was to find the common tangent to a set of ellipses Q1, Q2, Q3, Q4 ... which is done by computing the adjoint of sum{Q1,...,Q4} and then solving for the parameters x1,x2,x3.<br>
<br>
We did this (as proposed by Matt) by decomposing the problem into 3 cases:<br>
<br>
Case 1: Set x = [x1,x2,1]<br>
<br>
Case 2: Set x = [x1,1,0]<br>
<br>
Case 3: Set x = [1,x2,0]<br>
<br>
Obtaining the solutions for Cases 2 and 3 is trivial as we are dealing with a 1D polynomial for which we can compute the roots and hence obtain our solution.<br>
<br>
Case 1 is a bit more complex since we are dealing with a fourth order polynomial in both x1 and x2.<br>
<br>
For this case we have our initial function f = ( [x1,x2,1]^T * Q * [x1,x2,1] )^2.<br>
Note that Q = [a,b,d;b,c,e;d,e,f].<br>
<br>
Hence f = (a*x1^2 + 2b*x1*x2 + 2d*x1 + c*x2^2 + 2e*x2 + f)^2<br>
<br>
What I do next is compute the partial derivative of f with respect to x1 and the partial derivative of f with respect to x2. I then set the first partial derivative to 0 to compute the roots:<br>
<br>
df/dx1 = 0.<br>
<br>
I am left with some roots (potentially complex) which I substitute for x1 into the partial derivative of f with respect to x2, i.e. into df/dx2.<br>
<br>
I set this to zero as well to get my solutions for x2, i.e. I do<br>
<br>
df/dx2 (x1,1,...,x1,N) = 0.<br>
<br>
I am left with an expression for x2 which is only dependent on Q, i.e. on known parameters a,b,c,d,e,f !<br>
<br>
These solutions are potentially complex as well.<br>
<br>
I then take these results for x2 and resubstitute into the original roots i found for df/dx1 = 0 to compute values x1.<br>
<br>
This is pretty standard practice for computing the global minima of a function depending on two variables right?<br>
<br>
<br>
The problem is, my results are wrong. And I don't know why.<br>
<br>
Is there something wrong with my thinking? Did I omit something. Is something not true when dealing with potentially complex roots?<br>
<br>
I can be more specific if you like!<br>
<br>
Many thanks

Mon, 19 Jul 2010 20:17:05 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#763818
Matt J
"Jason" <jf203@ic.ac.uk> wrote in message <i225sp$gej$1@fred.mathworks.com>...<br>
<br>
> The idea was to find the common tangent to a set of ellipses Q1, Q2, Q3, Q4 ... which is done by computing the adjoint of sum{Q1,...,Q4} and then solving for the parameters x1,x2,x3.<br>
==========<br>
<br>
No, you cannot approach the problem by combining all the Q{i} into one matrix. The problem as we've posed it is to minimize,<br>
<br>
J(x)=sum_i (x' * adjoint(Q{i}) *x)^2<br>
<br>
This is NOT equivalent to the function (x' * sum_i (adjoint(Q{i}) *x)^2<br>
<br>
If that had been true, it would mean that the problem of finding a line uniquely tangent to multiple ellipses is equivalent to finding a line intersecting the single ellipse sum_i (Q{i}). But since a single ellipse never has a unique tangent line, you know it can't be equivalent to a multiellipse case in which the solution is unique.<br>
<br>
<br>
> What I do next is compute the partial derivative of f with respect to x1 and the partial derivative of f with respect to x2. I then set the first partial derivative to 0 to compute the roots:<br>
> <br>
> df/dx1 = 0.<br>
> <br>
> I am left with some roots (potentially complex) <br>
=================<br>
<br>
You wouldn't get numerical values for these roots. At best, you would get VERY complicated expressions for the roots in terms of the unknown x2.<br>
<br>
<br>
<br>
<br>
>which I substitute for x1 into the partial derivative of f with respect to x2, i.e. into df/dx2.<br>
><br>
> I set this to zero as well to get my solutions for x2, i.e. I do<br>
> <br>
> df/dx2 (x1,1,...,x1,N) = 0.<br>
> <br>
> I am left with an expression for x2 which is only dependent on Q, i.e. on known parameters a,b,c,d,e,f !<br>
===================<br>
<br>
But because the expressions substituted in for x1 are very complicated functions of x2, this equation should be a VERY hard root finding problem. How did you solve it?<br>
<br>
<br>
> These solutions are potentially complex as well.<br>
> <br>
> I then take these results for x2 and resubstitute into the original roots i found for df/dx1 = 0 to compute values x1.<br>
><br>
> This is pretty standard practice for computing the global minima of a function depending on two variables right?<br>
===============<br>
<br>
If you can simultaneously solve the equations df/dxi=0, then yes, but often those equations are too complicated to solve analytically (as they are here) and need to be solved by more indirect means. That's why the approaches I recommended to you in earlier posts were more elaborate.

Mon, 19 Jul 2010 20:29:35 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#763825
Matt J
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i22bs1$bsh$1@fred.mathworks.com>...<br>
>The problem as we've posed it is to minimize,<br>
> <br>
> J(x)=sum_i (x' * adjoint(Q{i}) *x)^2<br>
> <br>
> This is NOT equivalent to the function (x' * sum_i (adjoint(Q{i}) *x)^2<br>
=======<br>
<br>
Should have been ( x' * adjoint(sum_i Q{i}) * x )^2

Mon, 19 Jul 2010 21:04:06 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#763834
Jason
No, you cannot approach the problem by combining all the Q{i} into one matrix. The problem as we've posed it is to minimize,<br>
<br>
J(x)=sum_i (x' * adjoint(Q{i}) *x)^2<br>
<br>
This is NOT equivalent to the function (x' * sum_i (adjoint(Q{i}) *x)^2<br>
<br>
==============================================<br>
<br>
Point taken!<br>
<br>
You wouldn't get numerical values for these roots. At best, you would get VERY complicated expressions for the roots in terms of the unknown x2.<br>
<br>
===============================================<br>
<br>
Yes, exactly. It will be a complicated expression in terms of the unknown x2. However you can solve it using symlinks and the matlab 'solve' command.<br>
<br>
For example (NOTE THAT x1 and x2 are replaced by l1 and l2 in my code!):<br>
<br>
J1 = (a*l1^2 + 2*b*l1*l2 + 2*d*l1 + c*l2^2 + 2*e*l2 + f)^2<br>
<br>
The first derivative in terms of l1:<br>
dJl1 = 4*(d + a*l1 + b*l2)*(a*l1^2 + 2*b*l1*l2 + 2*d*l1 + c*l2^2 + 2*e*l2 + f)<br>
<br>
The roots of this:<br>
<br>
root1 = (d + b*l2)/a<br>
root2 = (d + b*l2  (b^2*l2^2 + 2*b*d*l2 + d^2  a*c*l2^2  2*a*e*l2  a*f)^(1/2))/a<br>
root3 = (d + b*l2 + (b^2*l2^2 + 2*b*d*l2 + d^2  a*c*l2^2  2*a*e*l2  a*f)^(1/2))/a<br>
<br>
Solutions for l2 based on the first root:<br>
<br>
l2_1 = (b*d  a*e + a*((f*b^2  2*b*d*e + c*d^2 + a*e^2  a*c*f)/a)^(1/2))/(a*c  b^2)<br>
l2_2 = (a*e  b*d)/(a*c  b^2)<br>
l2_3 = (a*e  b*d + a*((f*b^2  2*b*d*e + c*d^2 + a*e^2  a*c*f)/a)^(1/2))/(a*c  b^2)<br>
<br>
Solutions for l1 by substituting back into root1:<br>
<br>
l1_1 = (d + (b*(b*d  a*e + a*((f*b^2  2*b*d*e + c*d^2 + a*e^2  a*c*f)/a)^(1/2)))/(a*c  b^2))/a<br>
l1_2 = (d  (b*(a*e  b*d))/(a*c  b^2))/a<br>
l1_3 = (d  (b*(a*e  b*d + a*((f*b^2  2*b*d*e + c*d^2 + a*e^2  a*c*f)/a)^(1/2)))/(a*c  b^2))/a<br>
<br>
etc...<br>
<br>
Is there anything wrong with that?<br>
<br>
How else could I approach the problem then?<br>
<br>
And wouldn't it be possible to just correct the first inconsistency you presented by summing over i in all the cases but following the same methodology ?<br>
<br>
Regards,<br>
Jason

Tue, 20 Jul 2010 15:04:05 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#764054
Matt J
"Jason" <jf203@ic.ac.uk> wrote in message <i22ek6$90o$1@fred.mathworks.com>...<br>
<br>
> Yes, exactly. It will be a complicated expression in terms of the unknown x2. However you can solve it using symlinks and the matlab 'solve' command.<br>
> <br>
> For example (NOTE THAT x1 and x2 are replaced by l1 and l2 in my code!):<br>
> <br>
> J1 = (a*l1^2 + 2*b*l1*l2 + 2*d*l1 + c*l2^2 + 2*e*l2 + f)^2<br>
> <br>
> The first derivative in terms of l1:<br>
> dJl1 = 4*(d + a*l1 + b*l2)*(a*l1^2 + 2*b*l1*l2 + 2*d*l1 + c*l2^2 + 2*e*l2 + f)<br>
> <br>
> The roots of this:<br>
> <br>
> root1 = (d + b*l2)/a<br>
> root2 = (d + b*l2  (b^2*l2^2 + 2*b*d*l2 + d^2  a*c*l2^2  2*a*e*l2  a*f)^(1/2))/a<br>
> root3 = (d + b*l2 + (b^2*l2^2 + 2*b*d*l2 + d^2  a*c*l2^2  2*a*e*l2  a*f)^(1/2))/a<br>
> <br>
> Solutions for l2 based on the first root:<br>
> <br>
> l2_1 = (b*d  a*e + a*((f*b^2  2*b*d*e + c*d^2 + a*e^2  a*c*f)/a)^(1/2))/(a*c  b^2)<br>
> l2_2 = (a*e  b*d)/(a*c  b^2)<br>
> l2_3 = (a*e  b*d + a*((f*b^2  2*b*d*e + c*d^2 + a*e^2  a*c*f)/a)^(1/2))/(a*c  b^2)<br>
> <br>
> Solutions for l1 by substituting back into root1:<br>
> <br>
> l1_1 = (d + (b*(b*d  a*e + a*((f*b^2  2*b*d*e + c*d^2 + a*e^2  a*c*f)/a)^(1/2)))/(a*c  b^2))/a<br>
> l1_2 = (d  (b*(a*e  b*d))/(a*c  b^2))/a<br>
> l1_3 = (d  (b*(a*e  b*d + a*((f*b^2  2*b*d*e + c*d^2 + a*e^2  a*c*f)/a)^(1/2)))/(a*c  b^2))/a<br>
> <br>
> etc...<br>
> <br>
> Is there anything wrong with that?<br>
===========<br>
<br>
It is strange to me that for each fixed root1,2,3 the symbolic solver would obtain only a single corresponding l1_1,2,3. <br>
<br>
Prior to substituting root1,2,3 into dJl2, you have a cubic polynomial in l2 with potentially multiple real roots for each fixed l1. Are you saying the substitution of root1,2,3 reduced the number of potential roots to one?<br>
<br>
<br>
> How else could I approach the problem then?<br>
=========<br>
<br>
<br>
We already discussed an alternative in Message #15 and #28<br>
<br>
<br>
> And wouldn't it be possible to just correct the first inconsistency you presented by summing over i in all the cases but following the same methodology ?<br>
========<br>
<br>
Yes, even after the correction, you would obtain 4th order polynomials for J(l1,l2) and 3rd order polynomials for its derivatives. Any method applicable to that scenario should still be applicable after the correction.

Wed, 21 Jul 2010 13:25:05 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#764366
Jason
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i24dt5$3op$1@fred.mathworks.com>...<br>
> "Jason" <jf203@ic.ac.uk> wrote in message <i22ek6$90o$1@fred.mathworks.com>...<br>
> <br>
> > Yes, exactly. It will be a complicated expression in terms of the unknown x2. However you can solve it using symlinks and the matlab 'solve' command.<br>
> > <br>
> > For example (NOTE THAT x1 and x2 are replaced by l1 and l2 in my code!):<br>
> > <br>
> > J1 = (a*l1^2 + 2*b*l1*l2 + 2*d*l1 + c*l2^2 + 2*e*l2 + f)^2<br>
> > <br>
> > The first derivative in terms of l1:<br>
> > dJl1 = 4*(d + a*l1 + b*l2)*(a*l1^2 + 2*b*l1*l2 + 2*d*l1 + c*l2^2 + 2*e*l2 + f)<br>
> > <br>
> > The roots of this:<br>
> > <br>
> > root1 = (d + b*l2)/a<br>
> > root2 = (d + b*l2  (b^2*l2^2 + 2*b*d*l2 + d^2  a*c*l2^2  2*a*e*l2  a*f)^(1/2))/a<br>
> > root3 = (d + b*l2 + (b^2*l2^2 + 2*b*d*l2 + d^2  a*c*l2^2  2*a*e*l2  a*f)^(1/2))/a<br>
> > <br>
> > Solutions for l2 based on the first root:<br>
> > <br>
> > l2_1 = (b*d  a*e + a*((f*b^2  2*b*d*e + c*d^2 + a*e^2  a*c*f)/a)^(1/2))/(a*c  b^2)<br>
> > l2_2 = (a*e  b*d)/(a*c  b^2)<br>
> > l2_3 = (a*e  b*d + a*((f*b^2  2*b*d*e + c*d^2 + a*e^2  a*c*f)/a)^(1/2))/(a*c  b^2)<br>
> > <br>
> > Solutions for l1 by substituting back into root1:<br>
> > <br>
> > l1_1 = (d + (b*(b*d  a*e + a*((f*b^2  2*b*d*e + c*d^2 + a*e^2  a*c*f)/a)^(1/2)))/(a*c  b^2))/a<br>
> > l1_2 = (d  (b*(a*e  b*d))/(a*c  b^2))/a<br>
> > l1_3 = (d  (b*(a*e  b*d + a*((f*b^2  2*b*d*e + c*d^2 + a*e^2  a*c*f)/a)^(1/2)))/(a*c  b^2))/a<br>
> > <br>
> > etc...<br>
> > <br>
> > Is there anything wrong with that?<br>
> ===========<br>
> <br>
> It is strange to me that for each fixed root1,2,3 the symbolic solver would obtain only a single corresponding l1_1,2,3. <br>
> <br>
> Prior to substituting root1,2,3 into dJl2, you have a cubic polynomial in l2 with potentially multiple real roots for each fixed l1. Are you saying the substitution of root1,2,3 reduced the number of potential roots to one?<br>
> <br>
<br>
I have only outlined the case of substituting root1 into dJl2 and setting to zero.<br>
<br>
In fact if you do the same for root2 and root3 you obtain 9 roots in total (from which 2 can be excluded) meaning you are left with 7 in total, i.e. 7 estimates for l1 and consequently 7 estimates for l2.<br>
<br>
Since I wrongly placed the sum inside rather than outside I am missing some terms which I need to update now. But the solution should still be straightforward, and most importantly analytical and closed form.<br>
<br>
I know we have discussed about parametrizing l1 and l2 using cos and sin. However this is not an option I'm afraid.<br>
<br>
Do you see any problems with the way I am about to proceed ?

Wed, 21 Jul 2010 13:37:04 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#764373
Matt J
"Jason" <jf203@ic.ac.uk> wrote in message <i26sfh$spl$1@fred.mathworks.com>...<br>
<br>
> <br>
> I know we have discussed about parametrizing l1 and l2 using cos and sin. However this is not an option I'm afraid.<br>
> <br>
> Do you see any problems with the way I am about to proceed ?<br>
<br>
Not if the symbolic toolbox is able to solve the optimality equations, as you say. The only problem I see is the unclear reasoning why parametrizing l1 and l2 with sines and cosines is "not an option".

Wed, 21 Jul 2010 13:54:05 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#764384
Jason
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i26t60$f5i$1@fred.mathworks.com>...<br>
> "Jason" <jf203@ic.ac.uk> wrote in message <i26sfh$spl$1@fred.mathworks.com>...<br>
> <br>
> > <br>
> > I know we have discussed about parametrizing l1 and l2 using cos and sin. However this is not an option I'm afraid.<br>
> > <br>
> > Do you see any problems with the way I am about to proceed ?<br>
> <br>
> Not if the symbolic toolbox is able to solve the optimality equations, as you say.<br>
<br>
This I am not 100% sure of, but am testing it now. I did come across some problems for which I had to do some workarounds.<br>
<br>
<br>
<br>
Parametrizing l1 and l2 is not an option because I would have to do an exhaustive search over all angles. Furthermore this will only be an estimate, and yes, it can be improved by using a finer grid once a solution has been found.<br>
<br>
I am not adverse to this, and might actually give it a try if everything else fails. However I am worried about speed, and also not having an exact solution, when theoretically you can obtain a perfect solution, i.e. analytically obtain the global minimum.<br>
<br>
That's why!<br>
<br>
Many thanks for your insightful feedback, I will let you know how it went!

Wed, 21 Jul 2010 14:08:05 +0000
Re: Quadratic Cost Function x^T Q x
http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#764396
Matt J
"Jason" <jf203@ic.ac.uk> wrote in message <i26u5t$jk6$1@fred.mathworks.com>...<br>
<br>
> <br>
> Parametrizing l1 and l2 is not an option because I would have to do an exhaustive search over all angles. Furthermore this will only be an estimate, and yes, it can be improved by using a finer grid once a solution has been found.<br>
=============<br>
<br>
No, that's not the way I was intending you go about it. You would search on a grid of angles that is both coarse enough to make the search very fast search, but fine enough that the search gets you very close to the solution. Depending on the size of your ellipses, the grid size might be 1 or 2 degrees or even more. <br>
<br>
Once you have the result of this search, you would use it to initialize lsqnonlin and converge to a more exact result. The problems you were initially having with lsqnonlin would be greatly diminished here because now you have a very good initial guess of the global minimum. That will make the residual work done by lsqnonlin both fast and robust against stuck at local minima.<br>
<br>
> I am not adverse to this, and might actually give it a try if everything else fails. However I am worried about speed, and also not having an exact solution, when theoretically you can obtain a perfect solution, i.e. analytically obtain the global minimum.<br>
================<br>
<br>
I'm still rather astounded that this can be solved exactly, but yes, an analytical solution is better.