What is the difference between the 1st order polynomial fit and linear regression?
27 views (last 30 days)
Show older comments
For a long time, I always think that the 1st order polynomial fit and linear regression are same, until today I process the attached data.
load xy_ask.mat x y
clf; plot(x,y,'o'); hold on
x0=0:20;
[p,S,mu]=polyfit(x,y,1); plot(x0,polyval(p,x0),'b')
[rr,ar,br]=regression(x,y); plot(x0,ar*x0+br,'g')
legend('data','polyfit','regression'); xlabel('x'); ylabel('y')
Here is what I got. The result from polyfit seems not fitting at all. Does anyone know why?

0 Comments
Accepted Answer
Matt J
on 22 Jan 2024
Edited: Matt J
on 22 Jan 2024
They are the same. You are just plotting the polyfit result incorrectly,
load xy_ask.mat x y
clf; plot(x,y,'o'); hold on
x0=0:20;
[p,S,mu]=polyfit(x,y,1); plot(x0,polyval(p,(x0-mu(1))/mu(2)),'mo','MarkerFace','m')
[rr,ar,br]=regression(x,y); plot(x0,ar*x0+br,'g')
legend('data','polyfit','regression'); xlabel('x'); ylabel('y')
7 Comments
Star Strider
on 22 Jan 2024
The problem is not your understanding of linear algebra. It is instead the problem of not understanding how polyfit and polyval work.
When you ask for the ‘S’ structure and ‘mu’ outputs, polyfit centres and scales the fit. It is necessary to provide both these outputs as inputs to polyval in order to re-create the desired fit to the data.
See the third item under Description and the documentation section on Use Centering and Scaling to Improve Numerical Properties to understand how this works.
Bruno Luong
on 22 Jan 2024
"It is necessary to provide both these outputs as inputs to polyval in order to re-create the desired fit to the data."
Actually only mu is required by polyval for correct evauation for the fit data. S only used for second ouput delta (estimate standard deviation)
More Answers (1)
Image Analyst
on 22 Jan 2024
A linear regression means the equation is linear in the coefficients, not the variable. (Google it.) So this can be a model for a linear regression:
y = a2 * x^2 + a1 * x + a0
None of the "a"s are raised to a power or in any kind of weird formula. All the a's are linear, even though the x are not. So a linear regression is more general than a "1st order polynomial fit".
A "1st order polynomial fit" is where the model is a polynomial where the variable goes only to the first order, in other words just up to x, not any powers of x. So the model for a "1st order polynomial fit" is
y = a1 * x + a0
0 Comments
See Also
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
