Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel Polynomial Curve-Fitting Algorithm

What is the algorithm that Excel uses to calculate a 2nd-order polynomial regression (curve fitting)? Is there sample code or pseudo-code available?

like image 966
user1214135 Avatar asked Feb 19 '23 17:02

user1214135


2 Answers

I found a solution that returns the same formula that Excel gives:

  1. Put together an augmented matrix of values used in a Least-Squares Parabola. See the sum equations in http://www.efunda.com/math/leastsquares/lstsqr2dcurve.cfm

  2. Use Gaussian elimination to solve the matrix. Here is C# code that will do that http://www.codeproject.com/Tips/388179/Linear-Equation-Solver-Gaussian-Elimination-Csharp

  3. After running that, the left-over values in the matrix (M) will equal the coefficients given in Excel.

Maybe I can find the R^2 somehow, but I don't need it for my purposes.

like image 99
user1214135 Avatar answered Feb 26 '23 22:02

user1214135


The polynomial trendlines in charts use least squares based on a QR decomposition method like the LINEST worksheet function ( http://support.microsoft.com/kb/828533 ). A second order or quadratic trend for given (x,y) data could be calculated using =LINEST(y,x^{1,2}).

You can call worksheet formulas from C# using the Worksheet.Evaluate method.

like image 31
lori_m Avatar answered Feb 26 '23 21:02

lori_m