help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: fit data - trendline through zero


From: lash
Subject: Re: fit data - trendline through zero
Date: Tue, 15 Dec 1998 12:59:26 -0600 (CST)

> 
> Marcel -
>  
> > I use polyfit to find the trendline in my data. How can I force it to go
> > through zero ?
> 
> Divide your data by x before fitting.  Think about it.
> 
> On a related note, I have a hacked polyfit (polyweightfit) that
> accepts weighting factors for each point.  If that sounds generally
> interesting, I can post or submit it.
> 
>      - Larry Doolittle   address@hidden
> 

I originally thought this as well, but I don't think that the result is
then truly least squared error.  The error for larger values of x will
be weighted less since it is divided by x. For example, if

x=[1,1000000,1000001,1000002]'
and 
y=x+0.5

Using polyfit(x,y./x,0) gives a result of 1.1250 which has pretty large
squared error.  The actual answer should be very close to 1.

After thinking a bit, and looking at polyfit.m, I think that if you
just want to fit y=mx instead of y=mx+b you would want to use the following:

If x and y are column vectors,

m = (x'*x)/(x'*y)


If you wanted to fit y=p[1]*x^2+p[2]x

X = [x,x.^2];
p = (X'*X)/(X'*y)

Someone correct me if I am wrong.

Bill Lash
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]