Fit a line to data using regress
조회 수: 3 (최근 30일)
이전 댓글 표시
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')
댓글 수: 0
답변 (1개)
Adam Danz
2020년 9월 25일
편집: Adam Danz
2020년 9월 25일
If x and y are vectors, they must be column vectors
regress(y(:),x(:))
For alternatives, see
댓글 수: 2
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];
Or, check out lsline.
Or, check out polyfit(x,y,1) along with refline.
참고 항목
카테고리
Help Center 및 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!