## 28 Polynomial Manipulations

In Octave, a polynomial is represented by its coefficients (arranged in descending order). For example, a vector $c$ of length N

         p(x) = c(1) x^N + ... + c(N) x + c(N+1).

— Function File: compan (c)

> Compute the companion matrix corresponding to polynomial coefficient vector c.

> The companion matrix is

>                    _                                                        _
>                   |  -c(2)/c(1)   -c(3)/c(1)  ...  -c(N)/c(1)  -c(N+1)/c(1)  |
>                   |       1            0      ...       0             0      |
>                   |       0            1      ...       0             0      |
>               A = |       .            .   .            .             .      |
>                   |       .            .       .        .             .      |
>                   |       .            .           .    .             .      |
>                   |_      0            0      ...       1             0     _|

> The eigenvalues of the companion matrix are equal to the roots of the polynomial.

> See also: poly, roots, residue, conv, deconv, polyval, polyderiv, polyinteg.

— Function File: conv (a, b)

> Convolve two vectors.

> `y = conv (a, b)` returns a vector of length equal to `length (a) + length (b) - 1`. If a and b are polynomial coefficient vectors, `conv` returns the coefficients of the product polynomial.

> See also: deconv, poly, roots, residue, polyval, polyderiv, polyinteg.

— Function File: deconv (y, a)

> Deconvolve two vectors.

> `[b, r] = deconv (y, a)` solves for b and r such that `y = conv (a, b) + r`.

> If y and a are polynomial coefficient vectors, b will contain the coefficients of the polynomial quotient and r will be a remander polynomial of lowest order.

> See also: conv, poly, roots, residue, polyval, polyderiv, polyinteg.

— Loadable Function: y = conv2 (a, b, shape)
— Loadable Function: y = conv2 (v1, v2, M, shape)

> Returns 2D convolution of a and b where the size of c is given by

> shape= 'full'
>   returns full 2-D convolution
> shape= 'same'
>   same size as a. 'central' part of convolution
> shape= 'valid'
>   only parts which do not include zero-padded edges

> By default shape is 'full'. When the third argument is a matrix returns the convolution of the matrix M by the vector v1 in the column direction and by vector v2 in the row direction

— Function File: poly (a)

> If a is a square N-by-N matrix, `poly (`a`)` is the row vector of the coefficients of `det (z * eye (N) - a)`, the characteristic polynomial of a. If x is a vector, `poly (`x`)` is a vector of coefficients of the polynomial whose roots are the elements of x.

— Function File: polyderiv (c)
— Function File: [q] = polyder (b, a)
— Function File: [q, r] = polyder (b, a)

> Return the coefficients of the derivative of the polynomial whose coefficients are given by vector c. If a pair of polynomials is given b and a, the derivative of the product is returned in q, or the quotient numerator in q and the quotient denominator in r.

> See also: poly, polyinteg, polyreduce, roots, conv, deconv, residue, filter, polygcd, polyval, polyvalm.

— Function File: polyder (c)
— Function File: [q] = polyder (b, a)
— Function File: [q, r] = polyder (b, a)

> See polyderiv.

— Function File: [p, s] = polyfit (x, y, n)

> Return the coefficients of a polynomial p(x) of degree n that minimizes `sumsq (p(x(i)) - y(i))`, to best fit the data in the least squares sense.

> The polynomial coefficients are returned in a row vector.

> If two output arguments are requested, the second is a structure containing the following fields:

> `R`
>   The Cholesky factor of the Vandermonde matrix used to compute the polynomial coefficients.
> `X`
>   The Vandermonde matrix used to compute the polynomial coefficients.
> `df`
>   The degrees of freedom.
> `normr`
>   The norm of the residuals.
> `yf`
>   The values of the polynomial for each value of x.

— Function File: q = polygcd (b, a, tol)

> Find greatest common divisor of two polynomials. This is equivalent to the polynomial found by multiplying together all the common roots. Together with deconv, you can reduce a ratio of two polynomials. Tolerance defaults to

>               sqrt(eps).

> Note that this is an unstable algorithm, so don't try it on large polynomials.

> Example

>               polygcd (poly(1:8), poly(3:12)) - poly(3:8)
>                        deconv (poly(1:8), polygcd (poly(1:8), poly(3:12))) - poly(1:2)

> See also: poly, polyinteg, polyderiv, polyreduce, roots, conv, deconv, residue, filter, polyval, and polyvalm.

— Function File: polyinteg (c)

> Return the coefficients of the integral of the polynomial whose coefficients are represented by the vector c.

> The constant of integration is set to zero.

> See also: poly, polyderiv, polyreduce, roots, conv, deconv, residue, filter, polyval, and polyvalm.

— Function File: polyreduce (c)

> Reduces a polynomial coefficient vector to a minimum number of terms by stripping off any leading zeros.

> See also: poly, roots, conv, deconv, residue, filter, polyval, polyvalm, polyderiv, polyinteg.

— Function File: polyval (c, x)

> Evaluate a polynomial.

> `polyval (`c`,` x`)` will evaluate the polynomial at the specified value of x.

> If x is a vector or matrix, the polynomial is evaluated at each of the elements of x.

> See also: polyvalm, poly, roots, conv, deconv, residue, filter, polyderiv, polyinteg.

— Function File: polyvalm (c, x)

> Evaluate a polynomial in the matrix sense.

> `polyvalm (`c`,` x`)` will evaluate the polynomial in the matrix sense, i.e. matrix multiplication is used instead of element by element multiplication as is used in polyval.

> The argument x must be a square matrix.

> See also: polyval, poly, roots, conv, deconv, residue, filter, polyderiv, and polyinteg.

— Function File: residue (b, a, tol)

> If b and a are vectors of polynomial coefficients, then residue calculates the partial fraction expansion corresponding to the ratio of the two polynomials. The function `residue` returns r, p, k, and e, where the vector r contains the residue terms, p contains the pole values, k contains the coefficients of a direct polynomial term (if it exists) and e is a vector containing the powers of the denominators in the partial fraction terms.

> Assuming b and a represent polynomials P (s) and Q(s) we have:

>                P(s)    M       r(m)         N
>                ---- = SUM -------------  + SUM k(i)*s^(N-i)
>                Q(s)   m=1 (s-p(m))^e(m)    i=1

> where M is the number of poles (the length of the r, p, and e vectors) and N is the length of the k vector.

> The argument tol is optional, and if not specified, a default value of 0.001 is assumed. The tolerance value is used to determine whether poles with small imaginary components are declared real. It is also used to determine if two poles are distinct. If the ratio of the imaginary part of a pole to the real part is less than tol, the imaginary part is discarded. If two poles are farther apart than tol they are distinct. For example,

>                b = [1, 1, 1];
>                a = [1, -5, 8, -4];
>                [r, p, k, e] = residue (b, a);
>               => r = [-2, 7, 3]
>               => p = [2, 2, 1]
>               => k = [](0x0)
>               => e = [1, 2, 1]

> which implies the following partial fraction expansion

>                       s^2 + s + 1       -2        7        3
>                  ------------------- = ----- + ------- + -----
>                  s^3 - 5s^2 + 8s - 4   (s-2)   (s-2)^2   (s-1)

> See also: poly, roots, conv, deconv, polyval, polyderiv, and polyinteg.

— Function File: roots (v)

> For a vector v with N components, return the roots of the polynomial

>               v(1) * z^(N-1) + ... + v(N-1) * z + v(N)

— Function File: polyout (c, x)

> Write formatted polynomial

>                  c(x) = c(1) * x^n + ... + c(n) x + c(n+1)

> and return it as a string or write it to the screen (if nargout is zero). x defaults to the string `"s"`.

> See also: polyval, polyvalm, poly, roots, conv, deconv, residue, filter, polyderiv, and polyinteg.

— Function File: yi = ppval (pp, xi)

> Evaluate piece-wise polynomial pp at the points xi. If pp`.d` is a scalar greater than 1, or an array, then the returned value yi will be an array that is `d1, d1, ..., dk, length (`xi`)]`.

> See also: mkpp, unmkpp, spline.

— Function File: pp = mkpp (x, p)
— Function File: pp = mkpp (x, p, d)

> Construct a piece-wise polynomial structure from sample points x and coefficients p. The ith row of p, p`(`i`,:)`, contains the coefficients for the polynomial over the i-th interval, ordered from highest to lowest. There must be one row for each interval in x, so `rows (`p`) == length (`x`) - 1`.

> You can concatenate multiple polynomials of the same order over the same set of intervals using p`= [` p1`;` p2`; ...;` pd`]`. In this case, `rows (`p`) ==` d`* (length (`x`) - 1)`.

> d specifies the shape of the matrix p for all except the last dimension. If d is not specified it will be computed as `round (rows (`p`) / (length (`x`) - 1))` instead.

> See also: unmkpp, ppval, spline.

— Function File: [x, p, n, k, d] = unmkpp (pp)

> Extract the components of a piece-wise polynomial structure pp. These are as follows:

> x
>   Samples points.
> p
>   Polynomial coefficients for points in sample interval. p`(`i`, :)` contains the coefficients for the polynomial over interval i ordered from highest to lowest. If d`> 1`, p`(`r`,` i`, :)` contains the coeffients for the r-th polynomial defined on interval i. However, this is stored as a 2-D array such that c`= reshape (`p`(:,` j`),` n`,` d`)` gives c`(`i`,` r`)` is the j-th coefficient of the r-th polynomial over the i-th interval.
> n
>   Number of polynomial pieces.
> k
>   Order of the polynomial plus 1.
> d
>   Number of polynomials defined for each interval.

> See also: mkpp, ppval, spline.