フィルターのクリア

Polyfit: Polynomial is badly conditioned

78 ビュー (過去 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 Center および File ExchangePolynomials についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by