# How do I perform a linear least squares fit

조회 수: 448(최근 30일)
Alexander MacFarlane IV 2018년 12월 21일
편집: Tamas Kis 2021년 7월 26일
Hello.
I would like to perform a linear least squares fit to 3 data points.
The help files are very confusing, to the point where i can't figure out whether this is a base function of Matlab, I need the curve fitting toolbox, optimization toolbox, or both.
Thanks,
Alex

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

### 채택된 답변

Star Strider 2018년 12월 21일
You don’t need any toolboxes. Use polyfit (link) and polyval functions, or the mldivide (link) operator.

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

### 추가 답변(2개)

Image Analyst 2018년 12월 21일
편집: Image Analyst 2018년 12월 21일
See attached polyfit demo. Adapt as needed. All you need is base MATLAB - no toolboxes.
For example
coefficients = polyfit(x, y, 1);
yFitted = polyval(coefficients, x); % yFitted will be at the 3 points where x is. There will be 3 yFitted values.
If you want a lot more points, you can pass in more x to polyval():
xFit = linspace(min(x), max(x), 1000); % 1000 points.
yFitted = polyval(coefficients, xFit);
##### 댓글 수: 1표시 없음숨기기 없음
Alexander MacFarlane IV 2018년 12월 21일
Thanks Star Strider and Image Analyst!
You guys are amazingly fast.

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

Tamas Kis 2021년 7월 25일
편집: Tamas Kis 2021년 7월 26일
Function to perform curve fitting using linear least squares:
Linear and polynomial fits are linear least square fits, while the other fits (power, exponential, logarithmic) are approximated by first linearizing the data.

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

### 범주

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

R2018b

### Community Treasure Hunt

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

Start Hunting!