<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252985</link>
    <title>MATLAB Central Newsreader - How to plot 2 fitting lines in 1 dataset?</title>
    <description>Feed for thread: How to plot 2 fitting lines in 1 dataset?</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>Fri, 05 Jun 2009 17:21:02 -0400</pubDate>
      <title>How to plot 2 fitting lines in 1 dataset?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252985#654939</link>
      <author>Kuo-Hsien </author>
      <description>Hi all,&lt;br&gt;
&lt;br&gt;
My dataset can be categorized into 2 groups by eyes, so can I plot 2 fitting lines for this dataset? Right now, I just use polyfit function, but it fits for all data points. Thanks.&lt;br&gt;
&lt;br&gt;
Michael  </description>
    </item>
    <item>
      <pubDate>Fri, 05 Jun 2009 20:18:02 -0400</pubDate>
      <title>Re: How to plot 2 fitting lines in 1 dataset?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252985#654971</link>
      <author>Lorenzo Guerrasio</author>
      <description>&lt;br&gt;
you can use logical indexing to divide your data set and make a fit for each group.&lt;br&gt;
&lt;br&gt;
regards&lt;br&gt;
&quot;Kuo-Hsien&quot; &amp;lt;mchangks@hotmail.com&amp;gt; wrote in message &amp;lt;h0bk5u$cha$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi all,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; My dataset can be categorized into 2 groups by eyes, so can I plot 2 fitting lines for this dataset? Right now, I just use polyfit function, but it fits for all data points. Thanks.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Michael  </description>
    </item>
    <item>
      <pubDate>Sat, 06 Jun 2009 22:13:01 -0400</pubDate>
      <title>Re: How to plot 2 fitting lines in 1 dataset?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252985#655125</link>
      <author>Kuo-Hsien </author>
      <description>Hi Lorenzo,&lt;br&gt;
What do you just mean logical indexing? My data point is spread out from 0-50 with temperature, but two group of data points increase with me in different rate with temperature.  </description>
    </item>
    <item>
      <pubDate>Sun, 07 Jun 2009 12:31:01 -0400</pubDate>
      <title>Re: How to plot 2 fitting lines in 1 dataset?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252985#655174</link>
      <author>John D'Errico</author>
      <description>&quot;Kuo-Hsien&quot; &amp;lt;mchangks@hotmail.com&amp;gt; wrote in message &amp;lt;h0epld$lkf$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi Lorenzo,&lt;br&gt;
&amp;gt; What do you just mean logical indexing? My data point is spread out from 0-50 with temperature, but two group of data points increase with me in different rate with temperature.  &lt;br&gt;
&lt;br&gt;
Do you know where the break occurs? &lt;br&gt;
&lt;br&gt;
If you do, then split this into two problems. In&lt;br&gt;
fact they can be solved in one estimation step.&lt;br&gt;
&lt;br&gt;
If not then you need to use a tool that can find the&lt;br&gt;
location of the break. This is sometimes known as a&lt;br&gt;
free knot spline, here a piecewise linear spline. Either&lt;br&gt;
problem can be solved easily enough, but the solution&lt;br&gt;
depends on your answer.&lt;br&gt;
&lt;br&gt;
So, do you know the location of the break or must&lt;br&gt;
it be estimated?&lt;br&gt;
&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Sun, 07 Jun 2009 13:01:02 -0400</pubDate>
      <title>Re: How to plot 2 fitting lines in 1 dataset?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252985#655178</link>
      <author>John D'Errico</author>
      <description>&quot;John D'Errico&quot; &amp;lt;woodchips@rochester.rr.com&amp;gt; wrote in message &amp;lt;h0gbu5$ssj$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Kuo-Hsien&quot; &amp;lt;mchangks@hotmail.com&amp;gt; wrote in message &amp;lt;h0epld$lkf$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Hi Lorenzo,&lt;br&gt;
&amp;gt; &amp;gt; What do you just mean logical indexing? My data point is spread out from 0-50 with temperature, but two group of data points increase with me in different rate with temperature.  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Do you know where the break occurs? &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If you do, then split this into two problems. In&lt;br&gt;
&amp;gt; fact they can be solved in one estimation step.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If not then you need to use a tool that can find the&lt;br&gt;
&amp;gt; location of the break. This is sometimes known as a&lt;br&gt;
&amp;gt; free knot spline, here a piecewise linear spline. Either&lt;br&gt;
&amp;gt; problem can be solved easily enough, but the solution&lt;br&gt;
&amp;gt; depends on your answer.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So, do you know the location of the break or must&lt;br&gt;
&amp;gt; it be estimated?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; John&lt;br&gt;
&lt;br&gt;
Let me be more explicit, since I know you will ask&lt;br&gt;
for more information.&lt;br&gt;
&lt;br&gt;
x = rand(100,1);&lt;br&gt;
y = (1 + x).*(x&amp;lt;0.4) + (-2*x + 2.2).*(x&amp;gt;=0.4) + randn(size(x))/20;&lt;br&gt;
plot(x,y,'o')&lt;br&gt;
&lt;br&gt;
Here, you and I know the break occurs at 0.4.&lt;br&gt;
Can we estimate the coefficients of the lines?&lt;br&gt;
&lt;br&gt;
IFF we knew the break location, then we would do this:&lt;br&gt;
&lt;br&gt;
k = (x &amp;lt;= 0.4); % A logical varable&lt;br&gt;
coef = [k,x.*k,~k,x.*~k]\y&lt;br&gt;
&lt;br&gt;
coef = [k,x.*k,~k,x.*~k]\y&lt;br&gt;
coef =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.981257860141531&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1.07669216114181&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2.20227048054644&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;-2.01520597681586&lt;br&gt;
&lt;br&gt;
With no error, the coefficients should have been&lt;br&gt;
[1; 1; 2.2; -2], so we did rather well here.&lt;br&gt;
&lt;br&gt;
Can I do as well if I do not assume the location of&lt;br&gt;
the break is known? In fact, I cannot do quite as&lt;br&gt;
well in general, since we must estimate an extra&lt;br&gt;
parameter. We can do well enough though.&lt;br&gt;
&lt;br&gt;
coefest = @(B) [(x&amp;lt;B),x.*(x&amp;lt;B),(x&amp;gt;=B),x.*(x&amp;gt;=B)]\y;&lt;br&gt;
preds = @(B) [(x&amp;lt;B),x.*(x&amp;lt;B),(x&amp;gt;=B),x.*(x&amp;gt;=B)]*coefest(B);&lt;br&gt;
&lt;br&gt;
BreakPoint = fminbnd(@(B) sum((y - preds(B)).^2),0,1)&lt;br&gt;
BreakPoint =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.397098484667164&lt;br&gt;
&lt;br&gt;
coefest(BreakPoint)&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.98340320488404&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1.06085977689174&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2.20816686087013&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;-2.0226000312523&lt;br&gt;
&lt;br&gt;
See that I've used the limits of the data itself to&lt;br&gt;
define the range over which fminbnd will be allowed&lt;br&gt;
to search for the break point.&lt;br&gt;
&lt;br&gt;
HTH,&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Sun, 07 Jun 2009 16:54:01 -0400</pubDate>
      <title>Re: How to plot 2 fitting lines in 1 dataset?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252985#655205</link>
      <author>Lorenzo Guerrasio</author>
      <description>I'm not sure whether the data of the guy had a kink...it seems more that he has two data set, i.e. x1 and x2, with two different relation, i.e. y=x1 and y=3*x2, intermingled in one data set y,x&lt;br&gt;
If that is the case, you can find (in a more or less mathematical way) a line dividing the two data set, i.e. y=2*x2.&lt;br&gt;
Then you define the two data set as&lt;br&gt;
D1=y./x&amp;lt;=2;&lt;br&gt;
D2=y./x&amp;gt;=2;&lt;br&gt;
&lt;br&gt;
That is what I got from the problem&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&quot;John D'Errico&quot; &amp;lt;woodchips@rochester.rr.com&amp;gt; wrote in message &amp;lt;h0gdme$lhi$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;John D'Errico&quot; &amp;lt;woodchips@rochester.rr.com&amp;gt; wrote in message &amp;lt;h0gbu5$ssj$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &quot;Kuo-Hsien&quot; &amp;lt;mchangks@hotmail.com&amp;gt; wrote in message &amp;lt;h0epld$lkf$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Hi Lorenzo,&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; What do you just mean logical indexing? My data point is spread out from 0-50 with temperature, but two group of data points increase with me in different rate with temperature.  &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Do you know where the break occurs? &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; If you do, then split this into two problems. In&lt;br&gt;
&amp;gt; &amp;gt; fact they can be solved in one estimation step.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; If not then you need to use a tool that can find the&lt;br&gt;
&amp;gt; &amp;gt; location of the break. This is sometimes known as a&lt;br&gt;
&amp;gt; &amp;gt; free knot spline, here a piecewise linear spline. Either&lt;br&gt;
&amp;gt; &amp;gt; problem can be solved easily enough, but the solution&lt;br&gt;
&amp;gt; &amp;gt; depends on your answer.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; So, do you know the location of the break or must&lt;br&gt;
&amp;gt; &amp;gt; it be estimated?&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; John&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Let me be more explicit, since I know you will ask&lt;br&gt;
&amp;gt; for more information.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; x = rand(100,1);&lt;br&gt;
&amp;gt; y = (1 + x).*(x&amp;lt;0.4) + (-2*x + 2.2).*(x&amp;gt;=0.4) + randn(size(x))/20;&lt;br&gt;
&amp;gt; plot(x,y,'o')&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Here, you and I know the break occurs at 0.4.&lt;br&gt;
&amp;gt; Can we estimate the coefficients of the lines?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; IFF we knew the break location, then we would do this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; k = (x &amp;lt;= 0.4); % A logical varable&lt;br&gt;
&amp;gt; coef = [k,x.*k,~k,x.*~k]\y&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; coef = [k,x.*k,~k,x.*~k]\y&lt;br&gt;
&amp;gt; coef =&lt;br&gt;
&amp;gt;          0.981257860141531&lt;br&gt;
&amp;gt;           1.07669216114181&lt;br&gt;
&amp;gt;           2.20227048054644&lt;br&gt;
&amp;gt;          -2.01520597681586&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; With no error, the coefficients should have been&lt;br&gt;
&amp;gt; [1; 1; 2.2; -2], so we did rather well here.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Can I do as well if I do not assume the location of&lt;br&gt;
&amp;gt; the break is known? In fact, I cannot do quite as&lt;br&gt;
&amp;gt; well in general, since we must estimate an extra&lt;br&gt;
&amp;gt; parameter. We can do well enough though.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; coefest = @(B) [(x&amp;lt;B),x.*(x&amp;lt;B),(x&amp;gt;=B),x.*(x&amp;gt;=B)]\y;&lt;br&gt;
&amp;gt; preds = @(B) [(x&amp;lt;B),x.*(x&amp;lt;B),(x&amp;gt;=B),x.*(x&amp;gt;=B)]*coefest(B);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; BreakPoint = fminbnd(@(B) sum((y - preds(B)).^2),0,1)&lt;br&gt;
&amp;gt; BreakPoint =&lt;br&gt;
&amp;gt;          0.397098484667164&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; coefest(BreakPoint)&lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt;           0.98340320488404&lt;br&gt;
&amp;gt;           1.06085977689174&lt;br&gt;
&amp;gt;           2.20816686087013&lt;br&gt;
&amp;gt;           -2.0226000312523&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; See that I've used the limits of the data itself to&lt;br&gt;
&amp;gt; define the range over which fminbnd will be allowed&lt;br&gt;
&amp;gt; to search for the break point.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; HTH,&lt;br&gt;
&amp;gt; John</description>
    </item>
    <item>
      <pubDate>Mon, 08 Jun 2009 01:46:02 -0400</pubDate>
      <title>Re: How to plot 2 fitting lines in 1 dataset?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252985#655245</link>
      <author>Kuo-Hsien </author>
      <description>To John,&lt;br&gt;
Yes, I think I probably can plot an exponential line to separate this dataset into two groups (an exponential between these two groups). What exactly should I do?&lt;br&gt;
&lt;br&gt;
Thx,&lt;br&gt;
Michael &lt;br&gt;
&lt;br&gt;
&amp;gt; Do you know where the break occurs? &lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; If you do, then split this into two problems. In&lt;br&gt;
&amp;gt; fact they can be solved in one estimation step.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If not then you need to use a tool that can find the&lt;br&gt;
&amp;gt; location of the break. This is sometimes known as a&lt;br&gt;
&amp;gt; free knot spline, here a piecewise linear spline. Either&lt;br&gt;
&amp;gt; problem can be solved easily enough, but the solution&lt;br&gt;
&amp;gt; depends on your answer.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So, do you know the location of the break or must&lt;br&gt;
&amp;gt; it be estimated?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; John</description>
    </item>
    <item>
      <pubDate>Mon, 08 Jun 2009 02:31:01 -0400</pubDate>
      <title>Re: How to plot 2 fitting lines in 1 dataset?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252985#655246</link>
      <author>Kuo-Hsien </author>
      <description>&quot;Kuo-Hsien&quot; &amp;lt;mchangks@hotmail.com&amp;gt; wrote in message &amp;lt;h0hqgq$7s9$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; To John,&lt;br&gt;
&amp;gt; Yes, I think I probably can plot an exponential line to separate this dataset into two groups (an exponential between these two groups). What exactly should I do?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thx,&lt;br&gt;
&amp;gt; Michael &lt;br&gt;
&amp;gt; &lt;br&gt;
I should look down for more information before I reply this post. Nice and clear explanation. Michael </description>
    </item>
  </channel>
</rss>

