MATLAB Answers

Which method is MATLAB use in poly2 to do a curve fitting?

2 views (last 30 days)
Yaser Khojah
Yaser Khojah on 11 Jun 2020
Edited: John D'Errico on 11 Jun 2020
I'm using fit function to conduct a curve fitting for my data and estimate its equation as below. Now I want to know which method is the MATLAB using. Is this based on lest square error or interpolation? I need more explanation to include this in my thesis. Anyone can help please. Thanks in advance.
[population2,gof] = fit(x,y,'poly2');

  0 Comments

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 11 Jun 2020
Edited: John D'Errico on 11 Jun 2020
The 'poly2' option for fit will use a simple linear least squares solver. (I did verify this fact, as could you have done. Be VERY CAREFUL, if you edit the code to view it, as it can be a dangerous thing. Far too many people have editted code from MATLAB, and then mistakenly introduced bugs into the code, and then saved the file by mistake. Use type instead to view provided code.) As well, you could use the debugger to step down into the code to see exactly what fit does and where it goes for the fit.
Specifically, it creates the appropriate matrix problem for a quadratic model, then uses MATLAB's economy sized QR decomposition to decompose the matrix in an efficient and numerically well posed form. Why a QR? Because this also allows the computation of variances on the parameters, as will be computed later on. Essentially, R then provides a Cholesky factor for the covariance matrix. QR is a numerically stable solver for this class of linear agebra problems.
No column pivoting is performed in the QR. This is the only part that mildly surprised me, in that column pivoting could be performed to make the solve somewhat more stable. However, if your model is that close to the edge of instability that pivoting would help here, then you are trying to fit too high order a polynomial model in the first place. As such, I can accept the choice of no column pivoting done in the solve. (By way of comparison, when I wrote my own polyfitn utility, I did use a column pivoted QR.)
As I said, this is just simple linear least squares though. I'm not sure what you are asking, if it is beyond what I've said here. You can ask if you have some further question on what is done, though I won't be willing to teach a complete course on numerical linear algebra.

  2 Comments

Yaser Khojah
Yaser Khojah on 11 Jun 2020
You answered my question thank you so much John. Another question, for the 'cubicinterp', the interpolation is used instead of the least squares solver?
John D'Errico
John D'Errico on 11 Jun 2020
'cubicinterp' refers to interpolation as opposed to approximation. No least squares is involved for cubicinterp. That is generally true for any interpolation. The model will always predict the original data points exactly (to within floating point trash in the least significant bits, something which enters into any numerical computation.)
The only spline inside fit where any form of approximation to your data arises is in the smoothing spline, and you have not asked about that one.
But remember that ANYTHING that is explicitly called interpolation will always predict the data with essentially no error. An interpolant is only worried about what it will do BETWEEN the data points. Be careful if you might download tools from the file exchange, which sometimes seem to incorrectly use terms like interpolation with a great deal of license. You can trust the curve fitting toolbox in this respect. Of course, if you download my tools from the FEX, I would claim you can trust me too. But then, I might want to sell you a used car one day... ;-)

Sign in to comment.

More Answers (2)

David Hill
David Hill on 11 Jun 2020
See Polynomial Models in Matlab documentation

Rafael Hernandez-Walls
Rafael Hernandez-Walls on 11 Jun 2020
% -- FT is a string or a FITTYPE specifying the model to fit.
%
% If FT is a string, then it may be:
%
% FITTYPE DESCRIPTION
% 'poly1' Linear polynomial curve
% 'poly11' Linear polynomial surface
% 'poly2' Quadratic polynomial curve
% 'linearinterp' Piecewise linear interpolation
% 'cubicinterp' Piecewise cubic interpolation
% 'smoothingspline' Smoothing spline (curve)
% 'lowess' Local linear regression (surface)
%

  1 Comment

Yaser Khojah
Yaser Khojah on 11 Jun 2020
Dear Refael, I have already seen this and I know i'm using 'poly2' which is the Quadratic polynomial curve but I'm not sure if that is enough. Is there anyway, I can get more informaiton. I have seen some other places in goolge that MATLAB is using methods as lest square error or interpolation. However, I do not know here how to get more details?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by