# Polynomials

MATLAB has some commands for working with polynomials in one variable. An -th degree polynomial in one variable has to be represented in MATLAB by means of a row vector containing the coefficients of the polynomial, where the first component of the row vector is the highest coefficient. For example, the polynomials , , are represented by respectively the row vectors [4 2 -4], [6 0 -3 0 5] and [1 0 -2 0].

If is a row vector defining the coefficients of a polynomial and is a number, then the command
>> polyval(v,x)

calculates the value of the polynomial represented by in the point . can also be a vector. In this case, the values of the polynomial in the points represented by are calculated and stored in a row vector. For example, we have
>> polyval([1 -2 0 1],[1 2 3 4])

ans =
[0 1 10 33]

i.e., 0, 1, 10, 33 are the values of the polynomial in the points respectively.

Very often polynomial data fitting is used to fit a polynomial function through measurement data. The data fitting is done by means of the so-called least squares method. In this method, one attempts to generate a -degree polynomial

 (2.1)

where the coefficients are calculated in such a way that the line is as close as possible to the measurement points. In MATLAB, these coefficients may be calculated with the function polyfit(x,y,n). Here, and are arrays of measurement points and is the degree of the polynomial: is linear, is quadratic, etc. The output is an array of coefficients. For example,

>> coeff = polyfit(y,x,1)

results in a linear approximation of the function , with coefficients:

>> a0 = coeff(1)
>> a1 = coeff(2).

To evaluate this data fitting, you can use the command polyval again. For example, calculate some points of the line for in steps of , i.e. :
xp = 0:0.1:7;
yp = polyval(coeff,xp);


Esteur 2010-03-22