Path: news.mathworks.com!not-for-mail
From: "Lorenzo Guerrasio" <lorenzo.guerrasio@email.it>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to plot 2 fitting lines in 1 dataset?
Date: Sun, 7 Jun 2009 16:54:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 81
Message-ID: <h0grb9$a0n$1@fred.mathworks.com>
References: <h0bk5u$cha$1@fred.mathworks.com> <h0buhq$832$1@fred.mathworks.com> <h0epld$lkf$1@fred.mathworks.com> <h0gbu5$ssj$1@fred.mathworks.com> <h0gdme$lhi$1@fred.mathworks.com>
Reply-To: "Lorenzo Guerrasio" <lorenzo.guerrasio@email.it>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1244393641 10263 172.30.248.37 (7 Jun 2009 16:54:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 7 Jun 2009 16:54:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1371978
Xref: news.mathworks.com comp.soft-sys.matlab:545351


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
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.
Then you define the two data set as
D1=y./x<=2;
D2=y./x>=2;

That is what I got from the problem


"John D'Errico" <woodchips@rochester.rr.com> wrote in message <h0gdme$lhi$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <h0gbu5$ssj$1@fred.mathworks.com>...
> > "Kuo-Hsien" <mchangks@hotmail.com> wrote in message <h0epld$lkf$1@fred.mathworks.com>...
> > > Hi Lorenzo,
> > > 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.  
> > 
> > Do you know where the break occurs? 
> > 
> > If you do, then split this into two problems. In
> > fact they can be solved in one estimation step.
> > 
> > If not then you need to use a tool that can find the
> > location of the break. This is sometimes known as a
> > free knot spline, here a piecewise linear spline. Either
> > problem can be solved easily enough, but the solution
> > depends on your answer.
> > 
> > So, do you know the location of the break or must
> > it be estimated?
> > 
> > John
> 
> Let me be more explicit, since I know you will ask
> for more information.
> 
> x = rand(100,1);
> y = (1 + x).*(x<0.4) + (-2*x + 2.2).*(x>=0.4) + randn(size(x))/20;
> plot(x,y,'o')
> 
> Here, you and I know the break occurs at 0.4.
> Can we estimate the coefficients of the lines?
> 
> IFF we knew the break location, then we would do this:
> 
> k = (x <= 0.4); % A logical varable
> coef = [k,x.*k,~k,x.*~k]\y
> 
> coef = [k,x.*k,~k,x.*~k]\y
> coef =
>          0.981257860141531
>           1.07669216114181
>           2.20227048054644
>          -2.01520597681586
> 
> With no error, the coefficients should have been
> [1; 1; 2.2; -2], so we did rather well here.
> 
> Can I do as well if I do not assume the location of
> the break is known? In fact, I cannot do quite as
> well in general, since we must estimate an extra
> parameter. We can do well enough though.
> 
> coefest = @(B) [(x<B),x.*(x<B),(x>=B),x.*(x>=B)]\y;
> preds = @(B) [(x<B),x.*(x<B),(x>=B),x.*(x>=B)]*coefest(B);
> 
> BreakPoint = fminbnd(@(B) sum((y - preds(B)).^2),0,1)
> BreakPoint =
>          0.397098484667164
> 
> coefest(BreakPoint)
> ans =
>           0.98340320488404
>           1.06085977689174
>           2.20816686087013
>           -2.0226000312523
> 
> See that I've used the limits of the data itself to
> define the range over which fminbnd will be allowed
> to search for the break point.
> 
> HTH,
> John