Polyfit produces NaN values

26 views (last 30 days)
KK
KK on 10 Apr 2016
Edited: Star Strider on 10 Apr 2016
I'm having an issue using polyfit in an attempt to create a 3rd order line of best fit for some data that I have. I have a 16 x 2 matrix that contains velocity in the first column and power in the second. When I use polyfit(x(:,1),x(:,2),3) I receive NaN NaN NaN NaN. My code looks like
figure(3)
plot(pcurve(:,1),pcurve(:,2),'ro')
BestFit=polyfit(pcurve(:,1),pcurve(:,2),3)
xlabel('\bf Average Velocity (m/s) ');
ylabel('\bf Power ');
titlestr=strcat('\bf\fontsize{14}Velocity Vs. Power ', datafile);
title(titlestr);
Any help or alternate method suggestions are appreciated.

Accepted Answer

Star Strider
Star Strider on 10 Apr 2016
Edited: Star Strider on 10 Apr 2016
That is likely because you have NaN values in your data. There are ways to deal with that, the most obvious being:
x = randi(99, 16, 2);
x(5,1) = NaN;
x(9,2) = NaN; % Data With NaN’ Values
nanx = prod(isfinite(x),2);
xe = x(nanx == 1,:); % ‘Edited’ Data
EDIT — Tweaked ‘nanx’ assignment.
  3 Comments
KK
KK on 10 Apr 2016
How would I compare the accuracy of the best fit line of third order, to the best fit line of second order? Is there a way to calculate the R^2?
Also, is there a way to output the equations of the line of best fit to a legend on the plot?
Star Strider
Star Strider on 10 Apr 2016
Edited: Star Strider on 10 Apr 2016
I apologise for the delay. This took a bit of experimenting to get working:
x = 1:10;
y = x.^4 + randn(1,10); % Create Data
p{2} = polyfit(x, y, 2); % Save As Cell Array
yp{2} = polyval(p{2}, x);
p{3} = polyfit(x, y, 3);
yp{3} = polyval(p{3}, x);
Rp{2} = corrcoef(y, yp{2});
Rp{3} = corrcoef(y, yp{3});
Rpm = permute(cat(3,Rp{2},Rp{3}), [3 1 2]); % Combine ‘corrcoef’ Outputs
[~,pd] = max(Rpm(:,2,1)); % ... & Find Largest
pm = char(43+2*(sign(p{pd+1})<0)); % Plus/Minus Signs
pref_poly = sprintf(' %c%10.2E\\cdotx^{%d}', [pm(1:end-2)' abs(p{pd+1}(1:end-2))' [pd+1:-1:2]']');
pref_poly = [pref_poly sprintf(' %c%10.2E\\cdotx %c%10.2E', [pm(end-1:end)' abs(p{pd+1}(end-1:end))']')];
pref_poly = ['p(x) = ' pref_poly(3:end)];
figure(1)
plot(x, y, 'bp')
hold on
plot(x, yp{pd+1}, '-r')
hold off
grid
legend('Data', pref_poly)
The two ‘Rp’ matrices and the combined matrix ‘Rpm’ are the correlation coefficients.
I didn’t test it rigorously, but it should be reasonably robust. You may want to tweak the ‘pref_poly’ assignment to display ‘x’ and ‘nothing’ instead of displaying ‘x^{1}’ and ‘x^{0}’ as it does now. It does display correctly, even if not perfectly. This should be enough get you started.
Example plot:
EDIT Tweaked ‘pref_poly’ so that it now produces the correctly formatted output. Updated plot to show the result.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation of 2-D Selections in 3-D Grids in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!