annotate scripts/polynomial/polyval.m @ 561:e79ff1f4df3c

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