My polyfit function is displaying NaN
44 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2020-11-21
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.
回答(2 个)
Steven Lord
2020-11-21
编辑:Steven Lord
2020-11-21
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)
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
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)
x = 1:0.1:2;
plot(x, polyval(p, x))
0 个评论
dpb
2020-11-21
>> 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 个评论
dpb
2020-11-21
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...
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Descriptive Statistics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!