Error finding Linear Regression with polyfit and \
Mostrar comentarios más antiguos
I am trying to plot the linear regression of my data throughout time however I have tried a few different methods but none of them having been working for me.
load data.mat;
y=data.MEAN_TEMPERATURE;
x=[1:numel(y)].';
p=polyfit(x,y,1)
b1=x\y
plot(x,y);
When I try using the plotfit or the \ method I get NaN as outputs. However when I plot it in a graph and then use the basic fitting tool I am able to get:
p1 = 0.00011312
p2 = 5.0801
for
y = p1*x + p2
So I am not sure what I am doing wrong here because clearly a linear regression can be found.
Respuesta aceptada
Más respuestas (1)
Removing the NaNs will do the trick. Although I'm not sure this data should have a linear fit.
fn=websave('data.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/703197/data.mat');
S=load(fn);
data=S.data;
y=data.MEAN_TEMPERATURE;
y(isnan(y))=[];
x=[1:numel(y)].';
p=polyfit(x,y,1)
b1=x\y
plot(x,y)
1 comentario
JMG
el 5 de Ag. de 2021
Categorías
Más información sobre Linear and Nonlinear Regression en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
