# L_POLYFIT

=L_POLYFIT(known_xs, known_ys, n)

Argument | Description | Example |
---|---|---|

known_xs | A vector of known values for x | {2;2.5;4;5.1} |

known_ys | A vector of known values for y | {22.5;13;7;16} |

n | Degree of the polynomial to fit | 2 |

## Description

**L_POLYFIT** uses linear regression via the LINEST function to return the coefficients for a polynomial function p(x) of degree *n* that best fits the data provided in the x and y vectors.

The n+1 coefficients are listed in order of decreasing power {β_{n}, β_{n-1}, ... β_{2}, β_{1}, β_{0}} corresponding to {x^{n}, x^{n-1}, ... x^{2}, x^{1}, 1}.

Note: **Linear** Regression refers to solving for the coefficients of an equation of *additive terms* that takes this form:
**Y** = β_{n}***X**_{n} + β_{n-1}***X**_{n-1} + … + β_{2}***X**_{2} + β_{1}***X**_{1} + β_{0}

When using LINEST for degrees higher than 1, it is necessary to assemble the **X** matrix so that each column represents a power of the original vector **x**. So, the L_POLYFIT is mostly just a convenient way of using LINEST without having to build up the X matrix yourself.

L_POLYFIT(known_ys,known_xs,n) = LINEST(known_ys,known_xs^SEQUENCE(1,n))

The L_POLYFIT function is used as the basis for many other polynomial related functions, which is another reason for defining it the way it is commonly used in software such as Matlab. The order of the coefficients in consistent with the LINEST function as well as the polyfit Matlab function.

## Lambda Formula

This code for using L_POLYFIT in Excel is provided under the License as part of the LAMBDA Library, but to use just this function, you may copy the following code directly into your spreadsheet.

### Code to Create Function via the Name Manager

Name: L_POLYFIT Comment: Returns the coefficients for the nth-degree polynomial fit using LINEST Refers To: =LAMBDA(known_xs,known_ys,n, LET(doc,"https://www.vertex42.com/lambda/polyfit.html", LINEST(known_ys,known_xs^SEQUENCE(1,n)) ))

### Code for AFE Workbook Module (Excel Labs Add-in)

/** * Returns the coefficients for the nth-degree polynomial fit using LINEST */ L_POLYFIT = LAMBDA(known_xs,known_ys,n, LET(doc,"https://www.vertex42.com/lambda/polyfit.html", LINEST(known_ys,known_xs^SEQUENCE(1,n)) ));

### Named Function for Google Sheets

Name: L_POLYFIT Description: Returns the coefficients for the nth-degree polynomial fit using LINEST Arguments: known_xs, known_ys, n (see above for descriptions and example values) Function: LET(doc,"https://www.vertex42.com/lambda/polyfit.html", ARRAYFORMULA(LINEST(known_ys,known_xs^SEQUENCE(1,n))) )

## L_POLYFIT Examples

Test: Copy and Paste this LET function into a cell =LET( known_xs, {2;2.5;4;4.5;5.1;5.7}, known_ys, {22.5;13;7;14;16;22}, n, 3, L_POLYFIT(known_xs,known_ys,n) ) Result: {-1.384, 20.251, -89.910, 132.519} p(x) = -1.384x^3 + 20.251x^2 + -89.910x^1 + 132.519x^0

After finding the coefficients with L_POLYFIT, the L_POLYVAL function can be used to evaluate p(x) for other values of x. The image below shows an example of fitting a 3rd-order polynomial.

### See Also

**Disclaimer**: This article is meant for educational purposes only. See the License regarding the LAMBDA code, and the site Terms of Use for the documentation.