# Polyfit: Polynomial is badly conditioned

69 views (last 30 days)
T on 28 Sep 2013
Commented: T on 29 Sep 2013
I have the following:
N = 5;
for i=1:N
p(i,:) = polyfit(time(:,3),values(:,5),i);
end
What is wrong with the above statement?

Image Analyst on 28 Sep 2013
Edited: Image Analyst on 28 Sep 2013
I get "Subscripted assignment dimension mismatch." polyfit() gives you back coefficients, and the number of those is different depending on what order you choose: 2 for linear, 3 for quadratic, 6 for 5th order. So how are you going to stick those all (a varying number) into a column of p? What is the size of P? Try it like this:
N = 5;
% Create some sample data.
time = rand(20,3);
values = rand(20,5);
p = zeros(N, 6); % Preallocate the largest you think you'll need
for k=1:N
[p(k,1:k+1), S, mu] = polyfit(time(:,3),values(:,5),k);
end
If it still complains, you'll have to use S and mu when you go to use polyval().
T on 28 Sep 2013
Edited: T on 29 Sep 2013
Right. But if I want to interpolate the data and plot polyval, how come what I wrote above gives me the error? That is,
for k=1:N
[p(k,1:k+1), S, mu] = polyfit(time(:,3),values(:,5),k);
plot = polyval(polyfit(time(:,3),values(:,5),k),time(:,3));
end
If I remove (k,1:k+1) from f(k,1:k+1) and plot using 'hold on' it works but I still get the 'Polynomial is badly conditioned' message.
T on 29 Sep 2013
Was I not clear?

Jan on 28 Sep 2013
Edited: Jan on 28 Sep 2013
Nothing is wrong. "Badly conditioned" means that the solution of the system of linear equations critically depends on rounding errors due to the limited precision.
A valid solution (not a workaround only) is the scaling: Transform the polynomial such that the X-values have a mean of 0 and a standard deviation (or range) of 1. Fortunately polyfit does this for you, when you obtain the 3rd output also, see doc polyfit.
Example:
x = 1000:1004;
y = rand(1, 5);
p1 = polyfit(x, y, 3); % Warning appears: Badly conditioned
p2 = polyfit(x - 1000, y, 3); % No warning
[p3, S, mu] = polyfit(x, y, 3); % No warning
But of course P2 and p3 differ from p1 now. You can easily apply the transformation to the inputs manually, or automatically in polyval.