Error finding Linear Regression with polyfit and \
Show older comments
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.
Accepted Answer
More Answers (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)
Categories
Find more on Linear and Nonlinear Regression 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!