From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Best fit line with constrained coefficients
Date: Mon, 10 May 2010 23:30:27 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 14
Message-ID: <hsa4uj$6mv$>
References: <hs9pck$618$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1273534227 6879 (10 May 2010 23:30:27 GMT)
NNTP-Posting-Date: Mon, 10 May 2010 23:30:27 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:634420

"Nathan " <> wrote in message <hs9pck$618$>...
> I am trying to fit a line to my data points, and while polyfit and regstats will easily fit a line, it may not be physically relevant.  How do I edit these functions so they will fit a regression line with a positive slope.
> If you care, here is some sample data.
> x=[282.2540  285.8649  253.2350  271.8654  293.8727  293.8727  106.1968  226.1100];
> y=[104.8101  116.0248  112.0172  106.1792  117.0507   64.0306  115.3988  102.3172]
> And I know the line should be positive, but polyfit generates a line with negative slope.
- - - - - - - -
  Nathan, if you are using least sum of squares as a criterion for best fit, the data you have given is best fit using a line with negative slope.  It is a fact that is very easily demonstrated mathematically.  If you wish to constrain the slope to non-negative values, then the best slope in that least squares sense would be a slope of zero.  That is the value you would get if you were to minimize the objective function Matt described while restricting slope m to real values.

  If these results are not in accordance with your needs, it would be necessary to define a different criterion for best fit.

Roger Stafford