Fit a line to data using regress

5 ビュー (過去 30 日間)
Nicholas Deosaran
Nicholas Deosaran 2020 年 9 月 25 日
編集済み: Adam Danz 2020 年 9 月 28 日
Hey all, I am trying to use a new matlab function "regress" and would like some help on how to use it to fit a line.
below is my code that I have so far, but lost on how to use the "regress" function.
x = 0:0.1:10;
n = 0;
noise = n*rand(1,length(x));
y = 2*x+1+noise;
figure(1);
plot(x,y,'d')

回答 (1 件)

Adam Danz
Adam Danz 2020 年 9 月 25 日
編集済み: Adam Danz 2020 年 9 月 25 日
  2 件のコメント
Nicholas Deosaran
Nicholas Deosaran 2020 年 9 月 25 日
Oh okay, I see.
and to plot that line on the axis..will it be
x = 0:0.1:10;
n = 0;
noise = n*rand(1,length(x));
y = 2*x+1+noise;
b = regress(y(:),x(:));
plot(x,y,'d') %plot as diamonds
hold on
plot(b,y)
hold off
is that correct?
Adam Danz
Adam Danz 2020 年 9 月 25 日
編集済み: Adam Danz 2020 年 9 月 28 日
No, b is just the slope if you've used the syntax from my answer.
If you want to plot the regression line you'll need the y-intercept term. To get the y-intercept term you need to add a column of 1s to x.
b = regress(y(:),[ones(size(x(:))),x(:)]);
b is a 2x1 vector containing the [intercept; slope];
Use refline(b(2),b(1)) to add the regression line.
Or, check out lsline.
Or, check out polyfit(x,y,1) along with refline.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeRegression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by