/octave-3.6.2/scripts/polynomial/polyval.m

# · Objective C · 152 lines · 137 code · 15 blank · 0 comment · 16 complexity · 3fb602b4beaec113458cfec5820f27d9 MD5 · raw file

  1. ## Copyright (C) 1994-2012 John W. Eaton
  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 3 of the License, or (at
  8. ## your option) 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, see
  17. ## <http://www.gnu.org/licenses/>.
  18. ## -*- texinfo -*-
  19. ## @deftypefn {Function File} {@var{y} =} polyval (@var{p}, @var{x})
  20. ## @deftypefnx {Function File} {@var{y} =} polyval (@var{p}, @var{x}, [], @var{mu})
  21. ## Evaluate the polynomial @var{p} at the specified values of @var{x}. When
  22. ## @var{mu} is present, evaluate the polynomial for
  23. ## (@var{x}-@var{mu}(1))/@var{mu}(2).
  24. ## If @var{x} is a vector or matrix, the polynomial is evaluated for each of
  25. ## the elements of @var{x}.
  26. ##
  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. ## from @code{polyfit}, must be supplied.
  34. ## @seealso{polyvalm, polyaffine, polyfit, roots, poly}
  35. ## @end deftypefn
  36. ## Author: Tony Richardson <arichard@stark.cc.oh.us>
  37. ## Created: June 1994
  38. ## Adapted-By: jwe
  39. function [y, dy] = polyval (p, x, s = [], mu)
  40. if (nargin < 2 || nargin > 4 || (nargout == 2 && nargin < 3))
  41. print_usage ();
  42. endif
  43. if (isempty (x))
  44. y = [];
  45. return;
  46. elseif (isempty (p))
  47. y = zeros (size (x));
  48. return;
  49. elseif (! isvector (p))
  50. error ("polyval: first argument must be a vector");
  51. endif
  52. if (nargin > 3)
  53. x = (x - mu(1)) / mu(2);
  54. endif
  55. n = length (p) - 1;
  56. y = p(1) * ones (size (x));
  57. for i = 2:n+1
  58. y = y .* x + p(i);
  59. endfor
  60. if (nargout == 2)
  61. ## Note: the F-Distribution is generally considered to be single-sided.
  62. ## http://www.itl.nist.gov/div898/handbook/eda/section3/eda3673.htm
  63. ## t = finv (1-alpha, s.df, s.df);
  64. ## dy = t * sqrt (1 + sumsq (A/s.R, 2)) * s.normr / sqrt (s.df)
  65. ## If my inference is correct, then t must equal 1 for polyval.
  66. ## This is because finv (0.5, n, n) = 1.0 for any n.
  67. try
  68. k = numel (x);
  69. A = (x(:) * ones (1, n+1)) .^ (ones (k, 1) * (n:-1:0));
  70. dy = sqrt (1 + sumsq (A/s.R, 2)) * s.normr / sqrt (s.df);
  71. dy = reshape (dy, size (x));
  72. catch
  73. if (isempty (s))
  74. error ("polyval: third input is required.")
  75. elseif (isstruct (s)
  76. && all (ismember ({"R", "normr", "df"}, fieldnames (s))))
  77. error (lasterr ())
  78. elseif (isstruct (s))
  79. error ("polyval: third input is missing the required fields.");
  80. else
  81. error ("polyval: third input is not a structure.");
  82. endif
  83. end_try_catch
  84. endif
  85. endfunction
  86. %!test
  87. %! fail("polyval([1,0;0,1],0:10)");
  88. %!test
  89. %! r = 0:10:50;
  90. %! p = poly (r);
  91. %! p = p / max(abs(p));
  92. %! x = linspace(0,50,11);
  93. %! y = polyval(p,x) + 0.25*sin(100*x);
  94. %! [pf, s] = polyfit (x, y, numel(r));
  95. %! [y1, delta] = polyval (pf, x, s);
  96. %! expected = [0.37235, 0.35854, 0.32231, 0.32448, 0.31328, ...
  97. %! 0.32036, 0.31328, 0.32448, 0.32231, 0.35854, 0.37235];
  98. %! assert (delta, expected, 0.00001)
  99. %!test
  100. %! x = 10 + (-2:2);
  101. %! y = [0, 0, 1, 0, 2];
  102. %! p = polyfit (x, y, numel (x) - 1);
  103. %! [pn, s, mu] = polyfit (x, y, numel (x) - 1);
  104. %! y1 = polyval (p, x);
  105. %! yn = polyval (pn, x, [], mu);
  106. %! assert (y1, y, sqrt(eps))
  107. %! assert (yn, y, sqrt(eps))
  108. %!test
  109. %! p = [0, 1, 0];
  110. %! x = 1:10;
  111. %! assert (x, polyval(p,x), eps)
  112. %! x = x(:);
  113. %! assert (x, polyval(p,x), eps)
  114. %! x = reshape(x, [2, 5]);
  115. %! assert (x, polyval(p,x), eps)
  116. %! x = reshape(x, [5, 2]);
  117. %! assert (x, polyval(p,x), eps)
  118. %! x = reshape(x, [1, 1, 5, 2]);
  119. %! assert (x, polyval(p,x), eps)
  120. %!test
  121. %! p = [1];
  122. %! x = 1:10;
  123. %! y = ones(size(x));
  124. %! assert (y, polyval(p,x), eps)
  125. %! x = x(:);
  126. %! y = ones(size(x));
  127. %! assert (y, polyval(p,x), eps)
  128. %! x = reshape(x, [2, 5]);
  129. %! y = ones(size(x));
  130. %! assert (y, polyval(p,x), eps)
  131. %! x = reshape(x, [5, 2]);
  132. %! y = ones(size(x));
  133. %! assert (y, polyval(p,x), eps)
  134. %! x = reshape(x, [1, 1, 5, 2]);
  135. %!assert (zeros (1, 10), polyval ([], 1:10))
  136. %!assert ([], polyval (1, []))
  137. %!assert ([], polyval ([], []))