我需要在绘图中添加拟合线

时间:2014-07-24 09:04:06

标签: matlab plot linear-regression

我有兴趣仅将拟合线(线性回归)添加到图的线性部分

filename = 'data_250var600.txt' ;
P=load( filename ) ;


f00 = figure;
plot(P(:,1),P(:,2),'-bx',...
     P(:,1),P(:,3),'--mo',...
     P(:,1),P(:,4),'--gs')

xlabel('Time, ms');
ylabel('Variance, mm^2');
hleg1 = legend('07','08','09','location','SouthEast');
title('data250var600') % title


P=[
100     732.7318272 2301.212628
200     2022.799407 4836.512573
300     3790.328395 6307.281793
400     5781.509551 7390.353428
500     7932.264606 7588.719506
600     9583.048356 7602.084801
700     10017.79108 7168.106741
800     10007.80916 7031.052248
900     9359.477015 7747.909733
1000    9256.611609 8373.799535
1100    9824.423253 8755.180075
1200    10281.3222  8729.212879
1300    10421.7682  8824.199107
1400    10908.09722 8872.63802
1500    11590.61608 8993.919216
1600    12056.20738 9198.947131
1700    12785.47621 9261.411574
1800    13027.82142 9242.737465
1900    13109.53144 9764.176206
2000    13806.27964 9963.253109
]

1 个答案:

答案 0 :(得分:2)

那你的实际问题是什么?是如何确定线性部分的开始和结束位置或如何进行回归? 你需要确定线性部分的开始位置和结束位置。确定后,您可以简单地使用polyfit()和polyval()

的组合
% linear part for blue maybe from index 10 to end
f = polyfit(P(10:end,1), P(10:end,2), 1);
y = polyval(f, P(10:end,1));
plot(P(10:end,1),y,'b-')

% linear part for magenta part maybe from index 3 to end
f = polyfit(P(3:end,1), P(3:end,3), 1);
y = polyval(f, P(3:end,1));
plot(P(3:end,1),y,'m-')

最佳, NRAS