function y = polyvaln(p, varargin) % polyvaln -- Evalate an ND polynomial. % polyvaln(p, loc) evaluates polynomial p % at locations loc, a matrix whose columns % represent [x y z ...]. The ND polynomial % is arranged as in "polyfitn". % polyvaln(p, x, y, z, ...) is an alternative % syntax. % % Also see: polyfitn. % Copyright (C) 2003 Dr. Charles R. Denham, ZYDECO. % All Rights Reserved. % Disclosure without explicit written consent from the % copyright owner does not constitute publication. % Version of 22-Apr-2003 14:51:04. % Updated 22-Apr-2003 15:05:48. if nargin < 2, help(mfilename), return, end loc = [varargin{:}]; [m, n] = size(loc); order = size(p, 2) - 1; dims = length(size(p)); sz = size(p); % Exponents for expansion. sub = cell(1, dims); [sub{:}] = ind2sub(sz, 1:prod(sz)); for i = 1:length(sub) sub{i} = sub{i}(:); end expon = [sub{:}] - 1; % Trim exponents to requested order. for k = 1:length(expon) if sum(expon(k, :)) > order expon(k, :) = NaN; end end y = zeros(m, 1); k = 0; for i = 1:prod(sz) k = k+1; ex = expon(k, :); if all(isfinite(ex)) ex = repmat(ex, [m 1]); x = loc .^ ex; x = prod(x, 2); x = p(k) * x; y = y + x; end end