561
|
1 function y = polyval(c,x) |
|
2 #Evaluate a polynomial. |
|
3 # |
|
4 #In octave, a polynomial is represented by it's coefficients (arranged |
|
5 #in descending order). For example a vector c of length n+1 corresponds |
|
6 #to the following nth order polynomial |
|
7 # |
|
8 # p(x) = c(1) x^n + ... + c(n) x + c(n+1). |
|
9 # |
|
10 #polyval(c,x) will evaluate the polynomial at the specified value of x. |
|
11 # |
|
12 #If x is a vector or matrix, the polynomial is evaluated at each of the |
|
13 #elements of x. |
|
14 # |
|
15 #SEE ALSO: polyvalm, poly, roots, conv, deconv, residue, filter, |
|
16 # polyderiv, polyinteg |
|
17 |
|
18 # Author: |
|
19 # Tony Richardson |
|
20 # amr@mpl.ucsd.edu |
|
21 # June 1994 |
|
22 |
|
23 if(nargin != 2) |
|
24 error("usage: polyval(c,x)"); |
|
25 endif |
|
26 |
|
27 if(is_matrix(c)) |
|
28 error("poly: first argument must be a vector."); |
|
29 endif |
|
30 |
|
31 if(length(c) == 0) |
|
32 y = c; |
|
33 return; |
|
34 endif |
|
35 |
|
36 n = length(c); |
|
37 y = c(1)*ones(rows(x),columns(x)); |
|
38 for index = 2:n |
|
39 y = c(index) + x .* y; |
|
40 endfor |
|
41 endfunction |