MATLAB Answers

My polyfit function is displaying NaN

1 view (last 30 days)
Tertius Poolman
Tertius Poolman on 21 Nov 2020
Commented: dpb on 21 Nov 2020
I have the following data:
displacement = [0,1,2,3,4,5,6,7,8,9,10]
TensileStress = [0,10,14,16,18,19,20,21,22,23,23]
I am trying to find the function that best fits the data, but going through the steps, I am battling with fitting a straight line to the transformed data. I'm using the polyfit function, but I'm getting a NaN (Not a Number) error. Please assist by pointing out my mistake based on my partial code below.
clc;
% Data Entry.
Displacement = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10];
TensileStress = [ 0 , 10 , 14 , 16 , 18 , 19 , 20 , 21 , 22 , 23 , 23];
% Plot the data on rectilinear scales.
subplot(2,2,1)
plot(TensileStress,Displacement,TensileStress,Displacement,'o')
set(gca, "fontsize", 10)
xlabel('Tensile Stress (GPa)')
ylabel('Displacement (mm)')
% Plot the data on semilog scales.
subplot(2,2,2)
semilogy(TensileStress,Displacement,TensileStress,Displacement,'o')
set(gca, "fontsize", 10)
xlabel('Tensile Stress (GPa)')
ylabel('Displacement (mm)')
% Plot the data on loglog scales.
subplot(2,2,3)
loglog(TensileStress,Displacement,TensileStress,Displacement,'o')
set(gca, "fontsize", 10)
xlabel('Tensile Stress (GPa)')
ylabel('Displacement (mm)')
% Fit a straight line to the transformed data.
p = polyfit(TensileStress,log10(Displacement),15);
m = p(1)
b = 10^p(2)

  1 Comment

Image Analyst
Image Analyst on 21 Nov 2020
You have a zero in Displacement, and log(0) is minus infinity so of course the coefficients p will be undefined. I would advise you to either use nlmfit() instead to fit a log (demos attached - adapt as needed), or just fit using all the other points except the first one that has a zero.

Sign in to comment.

Answers (2)

Steven Lord
Steven Lord on 21 Nov 2020
Edited: Steven Lord on 21 Nov 2020
There are at least three potential difficulties with your code. The first difficulty is that you're passing log10 of the Displacement into polyfit. What are you passing into polyfit as your y coordinates?
Displacement = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10];
log10(Displacement)
ans = 1×11
-Inf 0 0.3010 0.4771 0.6021 0.6990 0.7782 0.8451 0.9031 0.9542 1.0000
The second is that you're trying to fit a 15th degree polynomial. Why such a high order? When you evaluate some of the terms in that polynomial you'll be adding or subtracting numbers on the order of
23^15
ans = 2.6664e+20
A third problem is that the X coordinates in your data are not distinct. This can lead to a problem like:
p = polyfit([1 1 2], [2 3 5], 2)
Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as described in HELP POLYFIT.
p = 1×3
-0.7529 2.2588 -1.5059
x = 1:0.1:2;
plot(x, polyval(p, x))

  0 Comments

Sign in to comment.


dpb
dpb on 21 Nov 2020
>> log10(0)
ans =
-Inf
>>
You can't do that!!!
plot() and friends silently ignore NaN, +/-Inf so you don't see the zero elements on those plots.
I thought I recalled a warning from those functions that those values weren't displayed but didn't get one here w/ R2019b.
But, that's your problem. You either must have some data very near but not at precisely zero or forego trying to use that origin point to fit.

  1 Comment

dpb
dpb on 21 Nov 2020
Not to mention the problem a straight line isn't a degree 15 polynomial that Steven caught. I didn't notice the last argument to polyfit having just read the straight line part in description...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!