1
@c Copyright (C) 1996, 1997, 1999, 2000, 2002, 2007 John W. Eaton
3
@c This file is part of Octave.
5
@c Octave is free software; you can redistribute it and/or modify it
6
@c under the terms of the GNU General Public License as published by the
7
@c Free Software Foundation; either version 3 of the License, or (at
8
@c your option) any later version.
10
@c Octave is distributed in the hope that it will be useful, but WITHOUT
11
@c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
@c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15
@c You should have received a copy of the GNU General Public License
16
@c along with Octave; see the file COPYING. If not, see
17
@c <http://www.gnu.org/licenses/>.
19
@node Polynomial Manipulations
20
@chapter Polynomial Manipulations
22
In Octave, a polynomial is represented by its coefficients (arranged
23
in descending order). For example, a vector @var{c} of length
24
@math{N+1} corresponds to the following polynomial of order
29
p (x) = c_1 x^N + \ldots + c_N x + c_{N+1}.
37
p(x) = @var{c}(1) x^@var{N} + ... + @var{c}(@var{N}) x + @var{c}(@var{N}+1).
42
* Evaluating Polynomials::
44
* Products of Polynomials::
45
* Derivatives and Integrals::
46
* Polynomial Interpolation::
47
* Miscellaneous Functions::
50
@node Evaluating Polynomials
51
@section Evaluating Polynomials
53
The value of a polynomial represented by the vector @var{c} can be evaluated
54
at the point @var{x} very easily, as the following example shows.
58
val = dot( x.^(N:-1:0), c );
62
While the above example shows how easy it is to compute the value of a
63
polynomial, it isn't the most stable algorithm. With larger polynomials
64
you should use more elegant algorithms, such as Horner's Method, which
65
is exactly what the Octave function @code{polyval} does.
67
In the case where @var{x} is a square matrix, the polynomial given by
68
@var{c} is still well-defined. As when @var{x} is a scalar the obvious
69
implementation is easily expressed in Octave, but also in this case
70
more elegant algorithms perform better. The @code{polyvalm} function
71
provides such an algorithm.
78
@section Finding Roots
80
Octave can find the roots of a given polynomial. This is done by computing
81
the companion matrix of the polynomial (see the @code{compan} function
82
for a definition), and then finding its eigenvalues.
88
@node Products of Polynomials
89
@section Products of Polynomials
101
@node Derivatives and Integrals
102
@section Derivatives and Integrals
104
Octave comes with functions for computing the derivative and the integral
105
of a polynomial. The functions @code{polyderiv} and @code{polyint}
106
both return new polynomials describing the result. As an example we'll
107
compute the definite integral of @math{p(x) = x^2 + 1} from 0 to 3.
111
integral = polyint(c);
112
area = polyval(integral, 3) - polyval(integral, 0)
116
@DOCSTRING(polyderiv)
122
@node Polynomial Interpolation
123
@section Polynomial Interpolation
125
Octave comes with good support for various kinds of interpolation,
126
most of which are described in @ref{Interpolation}. One simple alternative
127
to the functions described in the aforementioned chapter, is to fit
128
a single polynomial to some given data points. To avoid a highly
129
fluctuating polynomial, one most often wants to fit a low-order polynomial
130
to data. This usually means that it is necessary to fit the polynomial
131
in a least-squares sense, which is what the @code{polyfit} function does.
135
In situations where a single polynomial isn't good enough, a solution
136
is to use several polynomials pieced together. The function @code{mkpp}
137
creates a piece-wise polynomial, @code{ppval} evaluates the function
138
created by @code{mkpp}, and @code{unmkpp} returns detailed information
141
The following example shows how to combine two linear functions and a
142
quadratic into one function. Each of these functions is expressed
143
on adjoined intervals.
151
xi = linspace(-2, 2, 50);
162
@node Miscellaneous Functions
163
@section Miscellaneous Functions
169
@DOCSTRING(polyreduce)