]>

Approximating data with polynomials using Matlab

Approximating data points involves selecting a function y = f ( x ) MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaamyEaiabg2da9iaadAgadaqadaqaaiaadIhaaiaawIcacaGLPaaaaaa@3B62@ such that f ( x k ) MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaamOzamaabmaabaGaamiEamaaBaaaleaacaWGRbaabeaaaOGaayjkaiaawMcaaaaa@3A84@ comes close to y k MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaamyEamaaBaaaleaacaWGRbaabeaaaaa@3807@ Common functions to use are polynomials.

a 1 x n + a 2 x n 1 + a 3 x n 2 + + a n 1 x 2 + a n x + a n + 1 MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaamyyamaaBaaaleaacaaIXaaabeaakiaadIhadaahaaWcbeqaaiaad6gaaaGccqGHRaWkcaWGHbWaaSbaaSqaaiaaikdaaeqaaOGaamiEamaaCaaaleqabaGaamOBaiabgkHiTiaaigdaaaGccqGHRaWkcaWGHbWaaSbaaSqaaiaaiodaaeqaaOGaamiEamaaCaaaleqabaGaamOBaiabgkHiTiaaikdaaaGccqGHRaWkcqWIVlctcqGHRaWkcaWGHbWaaSbaaSqaaiaad6gacqGHsislcaaIXaaabeaakiaadIhadaahaaWcbeqaaiaaikdaaaGccqGHRaWkcaWGHbWaaSbaaSqaaiaad6gaaeqaaOGaamiEaiabgUcaRiaadggadaWgaaWcbaGaamOBaiabgUcaRiaaigdaaeqaaaaa@58CA@

If there are n+1 data points then this nth-degree polynomial will interpolate the data that is the function will pass through each point. For approximation more commonly the polynomial will be of low degree, such as a linear function, and there are many data points. In this case the approximating polynomial is meant to come close to the data, which might be statistically scattered around a linear trend.

Matlab conveniently computes the coefficients of a polynomial that comes close to a set of data points in the least squares sense. If x and y are vectors of n+1 ( x k , y k ) MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaWaaeWaaeaacaWG4bWaaSbaaSqaaiaadUgaaeqaaOGaaiilaiaadMhadaWgaaWcbaGaam4AaaqabaaakiaawIcacaGLPaaaaaa@3C6D@ data values entered into Matlab, then the function polyfit

Matlab Screenshot

returns the coefficients of a polynomial of degree m that approximates the n+1 data points in vectors x and y. The variable a is a vector of length m+1 that holds the coefficients

a 1 , a 2 , a n , a m + 1 MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaamyyamaaBaaaleaacaaIXaaabeaakiaacYcacaWGHbWaaSbaaSqaaiaaikdaaeqaaOGaaiilaiablAciljaadggadaWgaaWcbaGaamOBaaqabaGccaGGSaGaamyyamaaBaaaleaacaWGTbGaey4kaSIaaGymaaqabaaaaa@427E@

With these coefficients a linear polynomial for instance could be written in Matlab as

Matlab Screenshot

However, Matlab has a more convenient way to evaluate a polynomial with coefficients computed by polyfit. This is with a companion function called polyval.

Matlab Screenshot

If x_values is a vector of x values, then the polyval function uses the coefficients in the a vector to compute the value of the polynomial at each of the x values and returns these results to the vector y_values.