MATLAB: Polyfit: Polynomial is badly conditioned

polyfit

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?

Best Answer

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().