6778
|
1 @c Copyright (C) 1996, 1997, 2007 John W. Eaton |
3294
|
2 @c This is part of the Octave manual. |
|
3 @c For copying conditions, see the file gpl.texi. |
|
4 |
4167
|
5 @node Polynomial Manipulations |
3294
|
6 @chapter Polynomial Manipulations |
|
7 |
|
8 In Octave, a polynomial is represented by its coefficients (arranged |
6850
|
9 in descending order). For example, a vector @var{c} of length |
|
10 @math{N+1} corresponds to the following polynomial of order |
3294
|
11 @iftex |
|
12 @tex |
|
13 $N$ |
|
14 $$ |
6850
|
15 p (x) = c_1 x^N + \ldots + c_N x + c_{N+1}. |
3294
|
16 $$ |
|
17 @end tex |
|
18 @end iftex |
|
19 @ifinfo |
|
20 @var{N} |
|
21 |
|
22 @example |
|
23 p(x) = @var{c}(1) x^@var{N} + ... + @var{c}(@var{N}) x + @var{c}(@var{N}+1). |
|
24 @end example |
|
25 @end ifinfo |
|
26 |
6850
|
27 @menu |
|
28 * Evaluating Polynomials:: |
|
29 * Finding Roots:: |
|
30 * Products of Polynomials:: |
|
31 * Derivatives and Integrals:: |
|
32 * Polynomial Interpolation:: |
|
33 * Miscellaneous Functions:: |
|
34 @end menu |
|
35 |
|
36 @node Evaluating Polynomials |
|
37 @section Evaluating Polynomials |
|
38 |
|
39 The value of a polynomial represented by the vector @var{c} can be evaluated |
|
40 at the point @var{x} very easily, as the following example shows. |
|
41 |
|
42 @example |
|
43 N = length(c)-1; |
|
44 val = dot( x.^(N:-1:0), c ); |
|
45 @end example |
|
46 |
|
47 @noindent |
|
48 While the above example shows how easy it is to compute the value of a |
|
49 polynomial, it isn't the most stable algorithm. With larger polynomials |
|
50 you should use more elegant algorithms, such as Horner's Method, which |
|
51 is exactly what the Octave function @code{polyval} does. |
|
52 |
|
53 In the case where @var{x} is a square matrix, the polynomial given by |
|
54 @var{c} is still well-defined. As when @var{x} is a scalar the obvious |
|
55 implementation is easily expressed in Octave, but also in this case |
|
56 more elegant algorithms perform better. The @code{polyvalm} function |
|
57 provides such an algorithm. |
|
58 |
|
59 @DOCSTRING(polyval) |
|
60 |
|
61 @DOCSTRING(polyvalm) |
|
62 |
|
63 @node Finding Roots |
|
64 @section Finding Roots |
|
65 |
|
66 Octave can find the roots of a given polynomial. This is done by computing |
|
67 the companion matrix of the polynomial (see the @code{compan} function |
|
68 for a definition), and then finding its eigenvalues. |
|
69 |
|
70 @DOCSTRING(roots) |
|
71 |
3368
|
72 @DOCSTRING(compan) |
3294
|
73 |
6850
|
74 @node Products of Polynomials |
|
75 @section Products of Polynomials |
|
76 |
3368
|
77 @DOCSTRING(conv) |
3294
|
78 |
3368
|
79 @DOCSTRING(deconv) |
3294
|
80 |
6549
|
81 @DOCSTRING(conv2) |
|
82 |
6850
|
83 @DOCSTRING(polygcd) |
|
84 |
|
85 @DOCSTRING(residue) |
|
86 |
|
87 @node Derivatives and Integrals |
|
88 @section Derivatives and Integrals |
|
89 |
|
90 Octave comes with functions for computing the derivative and the integral |
6898
|
91 of a polynomial. The functions @code{polyderiv} and @code{polyint} |
6850
|
92 both return new polynomials describing the result. As an example we'll |
|
93 compute the definite integral of @math{p(x) = x^2 + 1} from 0 to 3. |
|
94 |
|
95 @example |
|
96 c = [1, 0, 1]; |
6898
|
97 integral = polyint(c); |
6850
|
98 area = polyval(integral, 3) - polyval(integral, 0) |
|
99 @result{} 12 |
|
100 @end example |
3294
|
101 |
3368
|
102 @DOCSTRING(polyderiv) |
3294
|
103 |
6502
|
104 @DOCSTRING(polyder) |
|
105 |
6898
|
106 @DOCSTRING(polyint) |
3294
|
107 |
6850
|
108 @node Polynomial Interpolation |
|
109 @section Polynomial Interpolation |
3294
|
110 |
6850
|
111 Octave comes with good support for various kinds of interpolation, |
|
112 most of which are described in @ref{Interpolation}. One simple alternative |
|
113 to the functions described in the aforementioned chapter, is to fit |
|
114 a single polynomial to some given data points. To avoid a highly |
|
115 fluctuating polynomial, one most often wants to fit a low-order polynomial |
|
116 to data. This usually means that it is necessary to fit the polynomial |
|
117 in a least-squares sense, which is what the @code{polyfit} function does. |
3294
|
118 |
6850
|
119 @DOCSTRING(polyfit) |
3294
|
120 |
6850
|
121 In situations where a single polynomial isn't good enough, a solution |
|
122 is to use several polynomials pieced together. The function @code{mkpp} |
|
123 creates a piece-wise polynomial, @code{ppval} evaluates the function |
|
124 created by @code{mkpp}, and @code{unmkpp} returns detailed information |
|
125 about the function. |
|
126 |
|
127 The following example shows how to combine two linear functions and a |
7001
|
128 quadratic into one function. Each of these functions is expressed |
6850
|
129 on adjoined intervals. |
3294
|
130 |
6850
|
131 @example |
|
132 x = [-2, -1, 1, 2]; |
|
133 p = [ 0, 1, 0; |
|
134 1, -2, 1; |
|
135 0, -1, 1 ]; |
|
136 pp = mkpp(x, p); |
|
137 xi = linspace(-2, 2, 50); |
|
138 yi = ppval(pp, xi); |
|
139 plot(xi, yi); |
|
140 @end example |
6502
|
141 |
|
142 @DOCSTRING(ppval) |
|
143 |
|
144 @DOCSTRING(mkpp) |
|
145 |
|
146 @DOCSTRING(unmkpp) |
6850
|
147 |
|
148 @node Miscellaneous Functions |
|
149 @section Miscellaneous Functions |
|
150 |
|
151 @DOCSTRING(poly) |
|
152 |
|
153 @DOCSTRING(polyout) |
|
154 |
|
155 @DOCSTRING(polyreduce) |
|
156 |
|
157 |