~ubuntu-branches/debian/sid/octave3.0/sid

« back to all changes in this revision

Viewing changes to doc/interpreter/poly.txi

  • Committer: Bazaar Package Importer
  • Author(s): Rafael Laboissiere
  • Date: 2007-12-23 16:04:15 UTC
  • Revision ID: james.westby@ubuntu.com-20071223160415-n4gk468dihy22e9v
Tags: upstream-3.0.0
ImportĀ upstreamĀ versionĀ 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/>.
 
18
 
 
19
@node Polynomial Manipulations
 
20
@chapter Polynomial Manipulations
 
21
 
 
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
 
25
@iftex
 
26
@tex
 
27
 $N$
 
28
$$
 
29
 p (x) = c_1 x^N + \ldots + c_N x + c_{N+1}.
 
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
 
 
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
 
 
86
@DOCSTRING(compan)
 
87
 
 
88
@node Products of Polynomials
 
89
@section Products of Polynomials
 
90
 
 
91
@DOCSTRING(conv)
 
92
 
 
93
@DOCSTRING(deconv)
 
94
 
 
95
@DOCSTRING(conv2)
 
96
 
 
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
 
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.
 
108
 
 
109
@example
 
110
c = [1, 0, 1];
 
111
integral = polyint(c);
 
112
area = polyval(integral, 3) - polyval(integral, 0)
 
113
@result{} 12
 
114
@end example
 
115
 
 
116
@DOCSTRING(polyderiv)
 
117
 
 
118
@DOCSTRING(polyder)
 
119
 
 
120
@DOCSTRING(polyint)
 
121
 
 
122
@node Polynomial Interpolation
 
123
@section Polynomial Interpolation
 
124
 
 
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.
 
132
 
 
133
@DOCSTRING(polyfit)
 
134
 
 
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
 
142
quadratic into one function.  Each of these functions is expressed
 
143
on adjoined intervals.
 
144
 
 
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
 
155
 
 
156
@DOCSTRING(ppval)
 
157
 
 
158
@DOCSTRING(mkpp)
 
159
 
 
160
@DOCSTRING(unmkpp)
 
161
 
 
162
@node Miscellaneous Functions
 
163
@section Miscellaneous Functions
 
164
 
 
165
@DOCSTRING(poly)
 
166
 
 
167
@DOCSTRING(polyout)
 
168
 
 
169
@DOCSTRING(polyreduce)
 
170
 
 
171