1 ## Copyright (C) 1994-2012 John W. Eaton
3 ## This file is part of Octave.
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 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {@var{y} =} polyval (@var{p}, @var{x})
21 ## @deftypefnx {Function File} {@var{y} =} polyval (@var{p}, @var{x}, [], @var{mu})
22 ## Evaluate the polynomial @var{p} at the specified values of @var{x}. When
23 ## @var{mu} is present, evaluate the polynomial for
24 ## (@var{x}-@var{mu}(1))/@var{mu}(2).
25 ## If @var{x} is a vector or matrix, the polynomial is evaluated for each of
26 ## the elements of @var{x}.
28 ## @deftypefnx {Function File} {[@var{y}, @var{dy}] =} polyval (@var{p}, @var{x}, @var{s})
29 ## @deftypefnx {Function File} {[@var{y}, @var{dy}] =} polyval (@var{p}, @var{x}, @var{s}, @var{mu})
30 ## In addition to evaluating the polynomial, the second output
31 ## represents the prediction interval, @var{y} +/- @var{dy}, which
32 ## contains at least 50% of the future predictions. To calculate the
33 ## prediction interval, the structured variable @var{s}, originating
34 ## from @code{polyfit}, must be supplied.
35 ## @seealso{polyvalm, polyaffine, polyfit, roots, poly}
38 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
42 function [y, dy] = polyval (p, x, s = [], mu)
44 if (nargin < 2 || nargin > 4 || (nargout == 2 && nargin < 3))
54 elseif (! isvector (p))
55 error ("polyval: first argument must be a vector");
59 x = (x - mu(1)) / mu(2);
63 y = p(1) * ones (size (x));
69 ## Note: the F-Distribution is generally considered to be single-sided.
70 ## http://www.itl.nist.gov/div898/handbook/eda/section3/eda3673.htm
71 ## t = finv (1-alpha, s.df, s.df);
72 ## dy = t * sqrt (1 + sumsq (A/s.R, 2)) * s.normr / sqrt (s.df)
73 ## If my inference is correct, then t must equal 1 for polyval.
74 ## This is because finv (0.5, n, n) = 1.0 for any n.
77 A = (x(:) * ones (1, n+1)) .^ (ones (k, 1) * (n:-1:0));
78 dy = sqrt (1 + sumsq (A/s.R, 2)) * s.normr / sqrt (s.df);
79 dy = reshape (dy, size (x));
82 error ("polyval: third input is required.")
84 && all (ismember ({"R", "normr", "df"}, fieldnames (s))))
87 error ("polyval: third input is missing the required fields.");
89 error ("polyval: third input is not a structure.");
97 %! fail("polyval([1,0;0,1],0:10)");
102 %! p = p / max(abs(p));
103 %! x = linspace(0,50,11);
104 %! y = polyval(p,x) + 0.25*sin(100*x);
105 %! [pf, s] = polyfit (x, y, numel(r));
106 %! [y1, delta] = polyval (pf, x, s);
107 %! expected = [0.37235, 0.35854, 0.32231, 0.32448, 0.31328, ...
108 %! 0.32036, 0.31328, 0.32448, 0.32231, 0.35854, 0.37235];
109 %! assert (delta, expected, 0.00001)
113 %! y = [0, 0, 1, 0, 2];
114 %! p = polyfit (x, y, numel (x) - 1);
115 %! [pn, s, mu] = polyfit (x, y, numel (x) - 1);
116 %! y1 = polyval (p, x);
117 %! yn = polyval (pn, x, [], mu);
118 %! assert (y1, y, sqrt(eps))
119 %! assert (yn, y, sqrt(eps))
124 %! assert (x, polyval(p,x), eps)
126 %! assert (x, polyval(p,x), eps)
127 %! x = reshape(x, [2, 5]);
128 %! assert (x, polyval(p,x), eps)
129 %! x = reshape(x, [5, 2]);
130 %! assert (x, polyval(p,x), eps)
131 %! x = reshape(x, [1, 1, 5, 2]);
132 %! assert (x, polyval(p,x), eps)
137 %! y = ones(size(x));
138 %! assert (y, polyval(p,x), eps)
140 %! y = ones(size(x));
141 %! assert (y, polyval(p,x), eps)
142 %! x = reshape(x, [2, 5]);
143 %! y = ones(size(x));
144 %! assert (y, polyval(p,x), eps)
145 %! x = reshape(x, [5, 2]);
146 %! y = ones(size(x));
147 %! assert (y, polyval(p,x), eps)
148 %! x = reshape(x, [1, 1, 5, 2]);
150 %!assert (zeros (1, 10), polyval ([], 1:10))
151 %!assert ([], polyval (1, []))
152 %!assert ([], polyval ([], []))