What is wrong with my polyfit variable?

1 visualización (últimos 30 días)
Ikenna Nebolisa
Ikenna Nebolisa el 29 de Sept. de 2020
Respondida: Steven Lord el 29 de Sept. de 2020
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

Respuestas (1)

Steven Lord
Steven Lord el 29 de Sept. de 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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by