Polyfit: Polynomial is badly conditioned

97 次查看(过去 30 天)
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
Image Analyst 2013-9-28
编辑:Image Analyst 2013-9-28
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().
  4 个评论
T
T 2013-9-28
编辑:T 2013-9-29
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.

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2013-9-28
编辑:Jan 2013-9-28
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.

类别

Help CenterFile Exchange 中查找有关 Polynomials 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by