# Least Squares Polynomial Fit

Compute polynomial coefficients that best fit input data in least-squares sense

Libraries:
DSP System Toolbox / Math Functions / Polynomial Functions

## Description

The Least Squares Polynomial Fit block computes the coefficients of the nth order polynomial that best fits the input data in the least-squares sense, where n is the value you specify in the Polynomial order parameter. The block computes a distinct set of n+1 coefficients for each column of the M-by-N input u.

## Examples

expand all

Open the ex_leastsquarespolyfit_ref model. The Polynomial Evaluation block uses the second-order polynomial below to generate four values of dependent variable y from four values of independent variable u received at the top port.

The polynomial coefficients are supplied in the vector [-2 0 3] at the bottom port. Note that the coefficient of the first-order term is zero.

The Control points parameter of the Least Squares Polynomial Fit block is configured with the same four values of independent variable u that are used as input to the Polynomial Evaluation block, [1 2 3 4]. The Least Squares Polynomial Fit block uses these values together with the input values of dependent variable y to reconstruct the original polynomial coefficients.

## Ports

### Input

expand all

Specify the input data as a vector or a matrix of size M-by-N. The block treats length-M unoriented vector input as an M-by-1 matrix.

Data Types: single | double

### Output

expand all

Coefficients of the polynomial that best fits the input data in the least-squares sense, returned as a column vector or a matrix of size (n+1)-by-N, where n is the value you specify in the Polynomial order parameter.

Each column of the (n+1)-by-N output matrix c represents a set of n+1 coefficients describing the best-fit polynomial for the corresponding column of the input. The coefficients in each column are arranged in order of descending exponents, c1, c2, ..., cn+1.

For more information on how this block computes the polynomial coefficients, see Algorithms.

Data Types: single | double

## Parameters

expand all

Specify the values of the independent variable to which the data in each input column correspond. For an M-by-N input, this parameter must be a length-M vector.

Tunable: Yes

Specify the order n of the polynomial to be used in constructing the best fit. The number of coefficients is n+1.

## Block Characteristics

 Data Types double | single Direct Feedthrough no Multidimensional Signals no Variable-Size Signals no Zero-Crossing Detection no

## Algorithms

For a given input column, the block computes the set of coefficients, c1, c2, ..., cn+1, that minimizes the quantity $\sum _{i=1}^{M}{\left({u}_{i}-{\stackrel{^}{u}}_{i}\right)}^{2}$, where ui is the ith element in the input column and is given by:

The values of the independent variable, x1, x2, ..., xM are specified as a length-M vector by the Control points parameter. The same M control points are used for all N polynomial fits, and can be equally or unequally spaced.

The equivalent MATLAB® code is shown below.

c = polyfit(x,u,n)

## Version History

Introduced before R2006a