Polynomials

MATLAB has some commands for working with polynomials in one variable. An $ n$ -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 $ 4z^2+2z-4$ , $ 6x^4-3x^2+5$ , $ s^3-2s$ are represented by respectively the row vectors [4 2 -4], [6 0 -3 0 5] and [1 0 -2 0].

If $ v$ is a row vector defining the coefficients of a polynomial and $ x$ is a number, then the command
>> polyval(v,x)
calculates the value of the polynomial represented by $ v$ in the point $ x$ . $ x$ can also be a vector. In this case, the values of the polynomial in the points represented by $ x$ 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 $ x^3-2x^2+1$ in the points $ x = 1,2,3,4$ 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 $ n^{\rm th}$ -degree polynomial

$\displaystyle y_n(x) = a_0x^n + a_1x^{n-1} + \cdots + a_{n-1}x + a_n,$ (2.1)

where the $ n+1$ 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, $ x$ and $ y$ are arrays of measurement points and $ n$ is the degree of the polynomial: $ n=1$ is linear, $ n=2$ is quadratic, etc. The output is an array of coefficients. For example,

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

results in a linear approximation $ a_0x+a_1$ of the function $ y(x)$ , 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 $ x \in [0,7]$ in steps of $ \Delta x = 0.1$ , i.e. $ x=[0, 0.1, 0.2,\ldots 6.9, 7.0]$ :
xp = 0:0.1:7;
yp = polyval(coeff,xp);



Previous      Next      Up      Contents


Esteur 2010-03-22