Error finding Linear Regression with polyfit and \
显示 更早的评论
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.
采纳的回答
更多回答(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)
类别
在 帮助中心 和 File Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
