2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
904
|
19 |
2311
|
20 ## usage: polyval (c, x) |
|
21 ## |
|
22 ## Evaluate a polynomial. |
2325
|
23 ## |
2311
|
24 ## In octave, a polynomial is represented by it's coefficients (arranged |
|
25 ## in descending order). For example a vector c of length n+1 corresponds |
|
26 ## to the following nth order polynomial |
2325
|
27 ## |
2311
|
28 ## p(x) = c(1) x^n + ... + c(n) x + c(n+1). |
2325
|
29 ## |
2311
|
30 ## polyval(c,x) will evaluate the polynomial at the specified value of x. |
2325
|
31 ## |
2311
|
32 ## If x is a vector or matrix, the polynomial is evaluated at each of the |
|
33 ## elements of x. |
2325
|
34 ## |
2311
|
35 ## SEE ALSO: polyvalm, poly, roots, conv, deconv, residue, filter, |
|
36 ## polyderiv, polyinteg |
1025
|
37 |
3202
|
38 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
2312
|
39 ## Created: June 1994 |
|
40 ## Adapted-By: jwe |
561
|
41 |
2312
|
42 function y = polyval (c, x) |
561
|
43 |
1025
|
44 if (nargin != 2) |
|
45 usage ("polyval (c, x)"); |
561
|
46 endif |
|
47 |
2716
|
48 if (! (is_vector (c) || isempty (c))) |
3085
|
49 error ("polyval: first argument must be a vector."); |
561
|
50 endif |
|
51 |
2716
|
52 if (isempty (x)) |
|
53 y = []; |
|
54 return; |
|
55 endif |
|
56 |
1025
|
57 if (length (c) == 0) |
561
|
58 y = c; |
|
59 return; |
|
60 endif |
|
61 |
1025
|
62 n = length (c); |
|
63 y = c (1) * ones (rows (x), columns (x)); |
561
|
64 for index = 2:n |
1025
|
65 y = c (index) + x .* y; |
561
|
66 endfor |
1025
|
67 |
561
|
68 endfunction |