Mercurial > hg > octave-lyh
comparison scripts/polynomial/polyval.m @ 7500:2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Tue, 19 Feb 2008 07:28:40 -0500 |
parents | 83a8781b529d |
children | 83cce070104f |
comparison
equal
deleted
inserted
replaced
7499:94d0cdd60dda | 7500:2df882e69f13 |
---|---|
16 ## You should have received a copy of the GNU General Public License | 16 ## You should have received a copy of the GNU General Public License |
17 ## along with Octave; see the file COPYING. If not, see | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | 18 ## <http://www.gnu.org/licenses/>. |
19 | 19 |
20 ## -*- texinfo -*- | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {} polyval (@var{c}, @var{x}) | 21 ## @deftypefn {Function File} {@var{y}=} polyval (@var{p}, @var{x}) |
22 ## Evaluate a polynomial. | 22 ## @deftypefnx {Function File} {@var{y}=} polyval (@var{p}, @var{x}, [], @var{mu}) |
23 ## | 23 ## Evaluate the polynomial at of the specified values for @var{x}. When @var{mu} |
24 ## @code{polyval (@var{c}, @var{x})} will evaluate the polynomial at the | 24 ## is present evaluate the polynomial for (@var{x}-@var{mu}(1))/@var{mu}(2). |
25 ## specified value of @var{x}. | 25 ## If @var{x} is a vector or matrix, the polynomial is evaluated for each of |
26 ## | |
27 ## If @var{x} is a vector or matrix, the polynomial is evaluated at each of | |
28 ## the elements of @var{x}. | 26 ## the elements of @var{x}. |
29 ## @seealso{polyvalm, poly, roots, conv, deconv, residue, filter, | 27 ## @deftypefnx {Function File} {[@var{y}, @var{dy}] =} polyval (@var{p}, @var{x}, @var{S}) |
28 ## @deftypefnx {Function File} {[@var{y}, @var{dy}] =} polyval (@var{p}, @var{x}, @var{S}, @var{mu}) | |
29 ## In addition to evaluating the polynomial, the second output | |
30 ## represents the prediction interval, @var{y} +/- @var{dy}, which | |
31 ## contains at least 50% of the future predictions. To calculate the | |
32 ## prediction interval, the structured variable @var{s}, originating | |
33 ## form `polyfit', must be present. | |
34 ## @seealso{polyfit, polyvalm, poly, roots, conv, deconv, residue, filter, | |
30 ## polyderiv, polyinteg} | 35 ## polyderiv, polyinteg} |
31 ## @end deftypefn | 36 ## @end deftypefn |
32 | 37 |
33 ## Author: Tony Richardson <arichard@stark.cc.oh.us> | 38 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
34 ## Created: June 1994 | 39 ## Created: June 1994 |
35 ## Adapted-By: jwe | 40 ## Adapted-By: jwe |
36 | 41 |
37 function y = polyval (c, x) | 42 function [y, dy] = polyval (p, x, s, mu) |
38 | 43 |
39 if (nargin != 2) | 44 if (nargin < 2 || nargin > 4 || (nargout == 2 && nargin < 3)) |
40 print_usage (); | 45 print_usage (); |
41 endif | 46 endif |
42 | 47 |
43 if (! (isvector (c) || isempty (c))) | 48 if (nargin < 3) |
49 s = []; | |
50 endif | |
51 | |
52 if (! (isvector (p) || isempty (p))) | |
44 error ("polyval: first argument must be a vector"); | 53 error ("polyval: first argument must be a vector"); |
54 endif | |
55 | |
56 if (nargin < 4) | |
57 mu = [0, 1]; | |
45 endif | 58 endif |
46 | 59 |
47 if (isempty (x)) | 60 if (isempty (x)) |
48 y = []; | 61 y = []; |
49 return; | 62 return; |
50 endif | 63 endif |
51 | 64 |
52 if (length (c) == 0) | 65 if (length (p) == 0) |
53 y = c; | 66 y = p; |
54 return; | 67 return; |
55 endif | 68 endif |
56 | 69 |
57 n = length (c); | 70 n = length (p) - 1; |
58 y = c (1) * ones (rows (x), columns (x)); | 71 k = numel (x); |
59 for index = 2:n | 72 x = (x - mu(1)) / mu(2); |
60 y = c (index) + x .* y; | 73 A = (x(:) * ones (1, n+1)) .^ (ones (k, 1) * (n:-1:0)); |
61 endfor | 74 y(:) = A * p(:); |
75 y = reshape (y, size (x)); | |
76 | |
77 if (nargout == 2) | |
78 ## The line below is *not* the result of a conceptual grasp of statistics. | |
79 ## Instead, after reading the links below and comparing to the output of Matlab's polyval.m, | |
80 ## http://www.mathworks.com/access/helpdesk/help/toolbox/stats/index.html?/access/helpdesk/help/toolbox/stats/finv.html | |
81 ## http://www.mathworks.com/access/helpdesk/help/toolbox/curvefit/index.html?/access/helpdesk/help/toolbox/curvefit/bq_5ka6-1_1.html | |
82 ## Note: the F-Distribution is generally considered to be single-sided. | |
83 ## http://www.itl.nist.gov/div898/handbook/eda/section3/eda3673.htm | |
84 ## t = finv (1-alpha, s.df, s.df); | |
85 ## dy = t * sqrt (1 + sumsq (A/s.R, 2)) * s.normr / sqrt (s.df) | |
86 ## If my inference is correct, then t must equal 1 for polyval. | |
87 ## This is because finv (0.5, n, n) = 1.0 for any n. | |
88 dy = sqrt (1 + sumsq (A/s.R, 2)) * s.normr / sqrt (s.df); | |
89 dy = reshape (dy, size (x)); | |
90 endif | |
62 | 91 |
63 endfunction | 92 endfunction |
64 | 93 |
65 %!assert(polyval ([1, 1, 1], 2) == 7); | 94 %!test |
95 %! fail("polyval([1,0;0,1],0:10)"); | |
66 | 96 |
67 %!assert(all (all (polyval ([1, 1, 1], [0; 1; 2]) == [1; 3; 7]))); | 97 %!test |
98 %! r = 0:10:50; | |
99 %! p = poly (r); | |
100 %! p = p / max(abs(p)); | |
101 %! x = linspace(0,50,11); | |
102 %! y = polyval(p,x) + 0.25*sin(100*x); | |
103 %! [pf, s] = polyfit (x, y, numel(r)); | |
104 %! [y1, delta] = polyval (pf, x, s); | |
105 %! expected = [0.37235, 0.35854, 0.32231, 0.32448, 0.31328, ... | |
106 %! 0.32036, 0.31328, 0.32448, 0.32231, 0.35854, 0.37235]; | |
107 %! assert (delta, expected, 0.00001) | |
68 | 108 |
69 %!assert(isempty (polyval ([1, 1, 1], []))); | 109 %!test |
110 %! x = 10 + (-2:2); | |
111 %! y = [0, 0, 1, 0, 2]; | |
112 %! p = polyfit (x, y, numel (x) - 1); | |
113 %! [pn, s, mu] = polyfit (x, y, numel (x) - 1); | |
114 %! y1 = polyval (p, x); | |
115 %! yn = polyval (pn, x, [], mu); | |
116 %! assert (y1, y, sqrt(eps)) | |
117 %! assert (yn, y, sqrt(eps)) | |
70 | 118 |
71 %!assert(all (all (polyval ([1, 1, 1], [-1, 0; 1, 2]) == [1, 1; 3, 7]))); | 119 %!test |
120 %! p = [0, 1, 0]; | |
121 %! x = 1:10; | |
122 %! assert (x, polyval(p,x), eps) | |
123 %! x = x(:); | |
124 %! assert (x, polyval(p,x), eps) | |
125 %! x = reshape(x, [2, 5]); | |
126 %! assert (x, polyval(p,x), eps) | |
127 %! x = reshape(x, [5, 2]); | |
128 %! assert (x, polyval(p,x), eps) | |
129 %! x = reshape(x, [1, 1, 5, 2]); | |
130 %! assert (x, polyval(p,x), eps) | |
72 | 131 |
73 %!error polyval ([1, 2; 3, 4], [-1, 0; 1, 2]); | |
74 | |
75 %!assert(isempty (polyval ([], [-1, 0; 1, 2]))); | |
76 |