필터 지우기
필터 지우기

Producing a line of best fit with equation

조회 수: 112 (최근 30일)
badfish66
badfish66 2016년 4월 24일
댓글: Image Analyst 2021년 12월 10일
Hi
I have been using lsline to produce a linear line of bext fit for two datasets. I was wondering if there was a similar command that produced the line of best fit and provided an equation (in the form y=mx+c) for said line.
Many Thanks

채택된 답변

Sebastian Castro
Sebastian Castro 2016년 4월 24일
There is also the polyfit function in MATLAB. If you specify an order of 1, the output of polyfit will have the two components m and c as you specified.
- Sebastian

추가 답변 (2개)

Image Analyst
Image Analyst 2016년 4월 24일
See attached demo of polyfit.

Kylie Hansen
Kylie Hansen 2017년 2월 16일
편집: Image Analyst 2021년 12월 10일
Thought I'd share an alternate answer from this github repository:
The code below prints a 1x2 matrix where the first value is the slope of the line and the second is the y-int.
Just plug into slope intercept form (y = mx+ b) and you've got the equation.
h = lsline ;
p2 = polyfit(get(h,'xdata'),get(h,'ydata'),1)
  댓글 수: 2
Ryan Kunen
Ryan Kunen 2021년 12월 10일
h = lsline ;
p2 = polyfit(get(h,'xdata'),get(h,'ydata'),1)
Image Analyst
Image Analyst 2021년 12월 10일
@Ryan Kunen, this is just the code @Kylie Hansen gave. Did you forget to add some comment about it?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by