Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Polynomial curve fitting

`p = polyfit(x,y,n)`

```
[p,S] =
polyfit(x,y,n)
```

```
[p,S,mu]
= polyfit(x,y,n)
```

`[`

also
returns `p`

,`S`

,`mu`

]
= polyfit(`x`

,`y`

,`n`

)`mu`

, which is a two-element vector with
centering and scaling values. `mu(1)`

is `mean(x)`

,
and `mu(2)`

is `std(x)`

. Using these
values, `polyfit`

centers `x`

at
zero and scales it to have unit standard deviation

$$\widehat{x}=\frac{x-\overline{x}}{{\sigma}_{x}}\text{\hspace{0.17em}}.$$

This centering and scaling transformation improves the numerical properties of both the polynomial and the fitting algorithm.

In problems with many points, increasing the degree of the polynomial fit using

`polyfit`

does not always result in a better fit. High-order polynomials can be oscillatory between the data points, leading to a*poorer*fit to the data. In those cases, you might use a low-order polynomial fit (which tends to be smoother between points) or a different technique, depending on the problem.Polynomials are unbounded, oscillatory functions by nature. Therefore, they are not well-suited to extrapolating bounded data or monotonic (increasing or decreasing) data.

`polyfit`

uses `x`

to form
Vandermonde matrix `V`

with `n+1`

columns
and `m = length(x)`

rows, resulting in the linear
system

$$\left(\begin{array}{cccc}{x}_{1}^{n}& {x}_{1}^{n-1}& \cdots & 1\\ {x}_{2}^{n}& {x}_{2}^{n-1}& \cdots & 1\\ \vdots & \vdots & \ddots & \vdots \\ {x}_{m}^{n}& {x}_{m}^{n-1}& \cdots & 1\end{array}\right)\left(\begin{array}{c}{p}_{1}\\ {p}_{2}\\ \vdots \\ {p}_{n+1}\end{array}\right)=\left(\begin{array}{c}{y}_{1}\\ {y}_{2}\\ \vdots \\ {y}_{m}\end{array}\right)\text{\hspace{0.17em}}\text{\hspace{0.17em}},$$

which `polyfit`

solves with `p = V\y`

.
Since the columns in the Vandermonde matrix are powers of the vector `x`

,
the condition number of `V`

is often large for high-order
fits, resulting in a singular coefficient matrix. In those cases centering
and scaling can improve the numerical properties of the system to
produce a more reliable fit.

Was this topic helpful?