What is wrong with my polyfit variable?

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

回答(1 个)

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

类别

帮助中心File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by