Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Anyone know how to polyfit forced through intercept zero?

5,447 views
Skip to first unread message

Chris F

unread,
Sep 12, 2003, 10:57:10 AM9/12/03
to
I can't find any documentation on how to use polyfit, but to force
the intercept through zero (i.e., with polyfit(x,y,1)).


Can this be done?

Steven Lord

unread,
Sep 12, 2003, 12:13:01 PM9/12/03
to

Yes you can fit a polynomial with the intercept forced to be 0, but no you
can't do it directly with POLYFIT. Assuming your X and Y data are in column
vectors, and N is the polynomial degree you want to fit, this will do it:

Nv = repmat(N:-1:1, length(x), 1);
Xm = repmat(x, 1, N);
DataMatrix = Xm.^Nv;
CoefficientVector = DataMatrix\y;

Now, to evaluate this polynomial at x to see how the fit did:

predictedY = polyval([CoefficientVector 0], [x 0]); % The first 0 is the
assumed zero constant term, the second zero is to verify the 0 intercept.

--
Steve Lord
sl...@mathworks.com


Chris F

unread,
Sep 12, 2003, 2:03:09 PM9/12/03
to
It worked! Thanks!

Randy Poe

unread,
Sep 12, 2003, 2:36:59 PM9/12/03
to
"Chris F" <ch...@mesonet.org> wrote in message news:<eec3a...@WebX.raydaftYaTP>...

> I can't find any documentation on how to use polyfit, but to force
> the intercept through zero (i.e., with polyfit(x,y,1)).
>
>
> Can this be done?

Yes and no. I don't think you can get polyfit to do it,
but you can modify the algorithm to do it yourself.

Inside polyfit you will see this code:

% Construct Vandermonde matrix.
V(:,n+1) = ones(length(x),1);
for j = n:-1:1
V(:,j) = x.*V(:,j+1);
end

The last column of this matrix is a column of ones. You
need a matrix which does not have that last column.

For instance, execute that code, then delete the
last column:
V(:,n+1) = [];

Now you can do your least-squares fit:

p = V\y;

This should be the coefficients of a polynomial with
zero constant coefficient, in order of descending power.

- Randy

0 new messages