<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/117474</link>
    <title>MATLAB Central Newsreader - Constrained least squares/polynomial fitting</title>
    <description>Feed for thread: Constrained least squares/polynomial fitting</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 23 Feb 2006 12:55:14 -0500</pubDate>
      <title>Constrained least squares/polynomial fitting</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/117474#296590</link>
      <author>wegwerp@gmail.com</author>
      <description>Hi Group,&lt;br&gt;
&lt;br&gt;
I am trying to find an offset between two sets of data points. Both&lt;br&gt;
sets of data (which are measurement data, rougly the same size) could&lt;br&gt;
be fitted with a certain polynomial:&lt;br&gt;
&lt;br&gt;
order = 2;&lt;br&gt;
p1 = polyfit(x1,y1,order);&lt;br&gt;
p2 = polyfit(x2,y2,order);&lt;br&gt;
offset = polyval(p2,0) - polyval(p1,0); % or offset = p2(end) - p1(end)&lt;br&gt;
&lt;br&gt;
The offset I am looking needs to be evaluated at x=0&lt;br&gt;
&lt;br&gt;
So far so good. In my application, however, I have the additional&lt;br&gt;
knowledge that both datasets originate from the same function, apart&lt;br&gt;
from the offset. I thus want to do the polyfitting, with the constraint&lt;br&gt;
that the all the higher order terms are equal for both sets, and I am&lt;br&gt;
looking for the difference in the 0-th order coefficient. For example:&lt;br&gt;
&lt;br&gt;
p1 = [a2,a1,a0]&lt;br&gt;
p1 = [b2,b1,b0]&lt;br&gt;
&lt;br&gt;
constraint: a2 = b2, a1=b1 and I am looking for b0-a0. It would be nice&lt;br&gt;
to know a1 and a2 too, but that is not neccessary. The polynomial order&lt;br&gt;
will probably be between 1 and 4&lt;br&gt;
&lt;br&gt;
Any thoughts or any pointers on how to solve this? I know it should be&lt;br&gt;
straightforward to implement this with fminsearch, but since both&lt;br&gt;
datasets could be large this might be really slow. I do have the&lt;br&gt;
gut-feeling that what I am looking for is a linear problem which could&lt;br&gt;
be solved directly. It has been a long time ago that I had those linear&lt;br&gt;
algebra classes ...&lt;br&gt;
&lt;br&gt;
Many thanks in advance,&lt;br&gt;
Bas</description>
    </item>
    <item>
      <pubDate>Thu, 23 Feb 2006 19:32:05 -0500</pubDate>
      <title>Re: Constrained least squares/polynomial fitting</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/117474#296622</link>
      <author>John D'Errico</author>
      <description>wegwerp wrote:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Hi Group,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I am trying to find an offset between two sets of data points. Both&lt;br&gt;
&amp;gt; sets of data (which are measurement data, rougly the same size)&lt;br&gt;
&amp;gt; could&lt;br&gt;
&amp;gt; be fitted with a certain polynomial:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; order = 2;&lt;br&gt;
&amp;gt; p1 = polyfit(x1,y1,order);&lt;br&gt;
&amp;gt; p2 = polyfit(x2,y2,order);&lt;br&gt;
&amp;gt; offset = polyval(p2,0) - polyval(p1,0); % or offset = p2(end) -&lt;br&gt;
&amp;gt; p1(end)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; The offset I am looking needs to be evaluated at x=0&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; So far so good. In my application, however, I have the additional&lt;br&gt;
&amp;gt; knowledge that both datasets originate from the same function,&lt;br&gt;
&amp;gt; apart&lt;br&gt;
&amp;gt; from the offset. I thus want to do the polyfitting, with the&lt;br&gt;
&amp;gt; constraint&lt;br&gt;
&amp;gt; that the all the higher order terms are equal for both sets, and I&lt;br&gt;
&amp;gt; am&lt;br&gt;
&amp;gt; looking for the difference in the 0-th order coefficient. For&lt;br&gt;
&amp;gt; example:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; p1 = [a2,a1,a0]&lt;br&gt;
&amp;gt; p1 = [b2,b1,b0]&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; constraint: a2 = b2, a1=b1 and I am looking for b0-a0. It would be&lt;br&gt;
&amp;gt; nice&lt;br&gt;
&amp;gt; to know a1 and a2 too, but that is not neccessary. The polynomial&lt;br&gt;
&amp;gt; order&lt;br&gt;
&amp;gt; will probably be between 1 and 4&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Any thoughts or any pointers on how to solve this? I know it should&lt;br&gt;
&amp;gt; be&lt;br&gt;
&amp;gt; straightforward to implement this with fminsearch, but since both&lt;br&gt;
&amp;gt; datasets could be large this might be really slow. I do have the&lt;br&gt;
&amp;gt; gut-feeling that what I am looking for is a linear problem which&lt;br&gt;
&amp;gt; could&lt;br&gt;
&amp;gt; be solved directly. It has been a long time ago that I had those&lt;br&gt;
&amp;gt; linear&lt;br&gt;
&amp;gt; algebra classes ...&lt;br&gt;
&lt;br&gt;
Use indicator variables.&lt;br&gt;
&lt;br&gt;
Assume that x1 nd x2 are column vectors, as are&lt;br&gt;
y1 and y2. Also, the order of the poly will be p.&lt;br&gt;
&lt;br&gt;
n1 = length(x1);&lt;br&gt;
n2 = length(x2);&lt;br&gt;
M = zeros(n1+n2,p+2);&lt;br&gt;
x12 = [x1;x2;];&lt;br&gt;
&lt;br&gt;
M(p,:) = x12;&lt;br&gt;
for i = 2:p&lt;br&gt;
&amp;nbsp;&amp;nbsp;M(p-i+1,:) = x12.*M(p-i+2,:);&lt;br&gt;
end&lt;br&gt;
M(p+1,:) = [ones(n1,1);zeros(n2,1)];&lt;br&gt;
M(p+1,:) = [zeros(n1,1);ones(n2,1)];&lt;br&gt;
&lt;br&gt;
poly = M\[y1;y2];&lt;br&gt;
&lt;br&gt;
The last two coefficients in poly are the constant&lt;br&gt;
coefficients for the two polynomials for sets 1&lt;br&gt;
and 2 respectively, but the rest of the terms are&lt;br&gt;
used in common.&lt;br&gt;
&lt;br&gt;
HTH,&lt;br&gt;
John D'Errico</description>
    </item>
    <item>
      <pubDate>Fri, 24 Feb 2006 03:55:30 -0500</pubDate>
      <title>Re: Constrained least squares/polynomial fitting</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/117474#296704</link>
      <author>wegwerp@gmail.com</author>
      <description>Hi John,&lt;br&gt;
&lt;br&gt;
thanks a lot, that was exactly what I was looking for. There were some&lt;br&gt;
minor typos in your version, so I attached my working version below for&lt;br&gt;
completeness sake.&lt;br&gt;
&lt;br&gt;
Cheers,&lt;br&gt;
Bas&lt;br&gt;
&lt;br&gt;
p = 4; %order of polynomial&lt;br&gt;
t1 = t1(:); t2 = t2(:); %make column vector&lt;br&gt;
n1=length(t1); n2=length(t2);&lt;br&gt;
t12 = [t1;t2];&lt;br&gt;
&lt;br&gt;
M = zeros(n1+n2,p+2);&lt;br&gt;
M(:,p) = t12;&lt;br&gt;
for i = p-1:-1:1&lt;br&gt;
&amp;nbsp;&amp;nbsp;M(:,i) = t12.*M(:,i+1);&lt;br&gt;
end&lt;br&gt;
M(:,p+1) = [ones(n1,1);zeros(n2,1)];&lt;br&gt;
M(:,p+2) = [zeros(n1,1);ones(n2,1)];&lt;br&gt;
&lt;br&gt;
poly = M\[y1(:);y2(:)];&lt;br&gt;
poly = poly(:)'; %make row vector&lt;br&gt;
p1 = poly(1:p+1);&lt;br&gt;
p2 = [poly(1:p),poly(p+2)];&lt;br&gt;
offset = poly(p+2)-poly(p+1);&lt;br&gt;
&lt;br&gt;
plot(t1,y1,'.',t1,polyval(p1,t1),t2,y2,'.',t2,polyval(p2,t2))</description>
    </item>
  </channel>
</rss>

