annotate doc/interpreter/poly.txi @ 7001:8b0cfeb06365

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