Can this be done?
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
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