7018
|
1 @c Copyright (C) 1996, 1997, 1999, 2000, 2002, 2007 John W. Eaton |
|
2 @c |
|
3 @c This file is part of Octave. |
|
4 @c |
|
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. |
|
9 @c |
|
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 |
|
13 @c for more details. |
|
14 @c |
|
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/>. |
3294
|
18 |
4167
|
19 @node Polynomial Manipulations |
3294
|
20 @chapter Polynomial Manipulations |
|
21 |
|
22 In Octave, a polynomial is represented by its coefficients (arranged |
6850
|
23 in descending order). For example, a vector @var{c} of length |
|
24 @math{N+1} corresponds to the following polynomial of order |
3294
|
25 @iftex |
|
26 @tex |
|
27 $N$ |
|
28 $$ |
6850
|
29 p (x) = c_1 x^N + \ldots + c_N x + c_{N+1}. |
3294
|
30 $$ |
|
31 @end tex |
|
32 @end iftex |
|
33 @ifinfo |
|
34 @var{N} |
|
35 |
|
36 @example |
|
37 p(x) = @var{c}(1) x^@var{N} + ... + @var{c}(@var{N}) x + @var{c}(@var{N}+1). |
|
38 @end example |
|
39 @end ifinfo |
|
40 |
6850
|
41 @menu |
|
42 * Evaluating Polynomials:: |
|
43 * Finding Roots:: |
|
44 * Products of Polynomials:: |
|
45 * Derivatives and Integrals:: |
|
46 * Polynomial Interpolation:: |
|
47 * Miscellaneous Functions:: |
|
48 @end menu |
|
49 |
|
50 @node Evaluating Polynomials |
|
51 @section Evaluating Polynomials |
|
52 |
|
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. |
|
55 |
|
56 @example |
|
57 N = length(c)-1; |
|
58 val = dot( x.^(N:-1:0), c ); |
|
59 @end example |
|
60 |
|
61 @noindent |
|
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. |
|
66 |
|
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. |
|
72 |
|
73 @DOCSTRING(polyval) |
|
74 |
|
75 @DOCSTRING(polyvalm) |
|
76 |
|
77 @node Finding Roots |
|
78 @section Finding Roots |
|
79 |
|
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. |
|
83 |
|
84 @DOCSTRING(roots) |
|
85 |
3368
|
86 @DOCSTRING(compan) |
3294
|
87 |
6850
|
88 @node Products of Polynomials |
|
89 @section Products of Polynomials |
|
90 |
3368
|
91 @DOCSTRING(conv) |
3294
|
92 |
3368
|
93 @DOCSTRING(deconv) |
3294
|
94 |
6549
|
95 @DOCSTRING(conv2) |
|
96 |
6850
|
97 @DOCSTRING(polygcd) |
|
98 |
|
99 @DOCSTRING(residue) |
|
100 |
|
101 @node Derivatives and Integrals |
|
102 @section Derivatives and Integrals |
|
103 |
|
104 Octave comes with functions for computing the derivative and the integral |
6898
|
105 of a polynomial. The functions @code{polyderiv} and @code{polyint} |
6850
|
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. |
|
108 |
|
109 @example |
|
110 c = [1, 0, 1]; |
6898
|
111 integral = polyint(c); |
6850
|
112 area = polyval(integral, 3) - polyval(integral, 0) |
|
113 @result{} 12 |
|
114 @end example |
3294
|
115 |
3368
|
116 @DOCSTRING(polyderiv) |
3294
|
117 |
6502
|
118 @DOCSTRING(polyder) |
|
119 |
6898
|
120 @DOCSTRING(polyint) |
3294
|
121 |
6850
|
122 @node Polynomial Interpolation |
|
123 @section Polynomial Interpolation |
3294
|
124 |
6850
|
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. |
3294
|
132 |
6850
|
133 @DOCSTRING(polyfit) |
3294
|
134 |
6850
|
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 |
|
139 about the function. |
|
140 |
|
141 The following example shows how to combine two linear functions and a |
7001
|
142 quadratic into one function. Each of these functions is expressed |
6850
|
143 on adjoined intervals. |
3294
|
144 |
6850
|
145 @example |
|
146 x = [-2, -1, 1, 2]; |
|
147 p = [ 0, 1, 0; |
|
148 1, -2, 1; |
|
149 0, -1, 1 ]; |
|
150 pp = mkpp(x, p); |
|
151 xi = linspace(-2, 2, 50); |
|
152 yi = ppval(pp, xi); |
|
153 plot(xi, yi); |
|
154 @end example |
6502
|
155 |
|
156 @DOCSTRING(ppval) |
|
157 |
|
158 @DOCSTRING(mkpp) |
|
159 |
|
160 @DOCSTRING(unmkpp) |
6850
|
161 |
|
162 @node Miscellaneous Functions |
|
163 @section Miscellaneous Functions |
|
164 |
|
165 @DOCSTRING(poly) |
|
166 |
|
167 @DOCSTRING(polyout) |
|
168 |
|
169 @DOCSTRING(polyreduce) |
|
170 |
|
171 |