What is wrong with my polyfit variable?
Show older comments
I am trying to write code to modele the best fit line of a data set using a power function, however whenever I try to find the equation of said line it always presents me with an error. What appears to be wrong with my code?
file = load(datafile);
x = file(:,1);
y = file(:,2);
lny = log(y);
lnx = log(x);
coeff1 = polyfit(x,lny,1);
line1 = polyval(coeff1,x);
coeff2 = polyfit(lnx,lny,1)
When it runs it displays coeff2 to be equal to
coeff2 =
NaN NaN
Answers (1)
Steven Lord
on 29 Sep 2020
Most likely at least one element of your x data is non-positive.
x = 0:10;
y = x;
p1 = polyfit(log(x), log(y), 1) % Warning and NaN values
p2 = polyfit(log(x(2:end)), log(y(2:end)), 1) % No warning, no NaN values
Categories
Find more on Resizing and Reshaping Matrices 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!