Polyfit: Polynomial is badly conditioned

조회 수: 97 (최근 30일)
T
T 2013년 9월 28일
댓글: T 2013년 9월 29일
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.
T
T 2013년 9월 29일
Was I not clear?

댓글을 달려면 로그인하십시오.

추가 답변 (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