Polyfit: Polynomial is badly conditioned
93 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 28 Sep. 2013
Bearbeitet: Image Analyst
am 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().
4 Kommentare
Weitere Antworten (1)
Jan
am 28 Sep. 2013
Bearbeitet: Jan
am 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Polynomials finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!