function [y, delta] = polyconf(p,x,S,alpha,mu)
%POLYCONF Polynomial evaluation and confidence interval estimation.
%   Y = POLYCONF(P,X) returns the value of a polynomial P evaluated at X. P
%   is a vector of length N+1 whose elements are the coefficients of the
%   polynomial in descending powers.
%
%       Y = P(1)*X^N + P(2)*X^(N-1) + ... + P(N)*X + P(N+1)
%
%   If X is a matrix or vector, the polynomial is evaluated at all points
%   in X.  See also POLYVALM for evaluation in a matrix sense.
%
%   [Y,DELTA] = POLYCONF(P,X,S) uses the optional output, S, created by
%   POLYFIT to generate 95% prediction intervals.  If the coefficients in P
%   are least squares estimates computed by POLYFIT, and the errors in the
%   data input to POLYFIT were independent, normal, with constant variance,
%   then there is a 95% probability that Y +/- DELTA will contain a future
%   observation at X.
%
%   [Y,DELTA] = POLYCONF(P,X,S,ALPHA) generates 100(1-ALPHA)% prediction
%   intervals.
%
%   [...] = POLYCONF(P,X,S,ALPHA,MU) uses (X-MU(1))/MU(2) in place of X.
%   The centering and scaling parameters MU are optional output computed by
%   POLYFIT.
%
%   See also POLYFIT, POLYTOOL, POLYVAL, POLYVALM.

%   Copyright 1993-2004 The MathWorks, Inc.
%   $Revision: 2.9.2.2 $  $Date: 2004/07/05 17:03:07 $

if nargout > 1
    if nargin < 3, S = []; end % this is an error; let polyval handle it
    if nargin < 4 || isempty(alpha)
        alpha = 0.05;
    elseif ~isscalar(alpha)
        error('stats:polyconf:BadAlpha','ALPHA must be a scalar.');
    end
    if nargin < 5
        [y,delta] = polyval(p,x,S);
    else
        [y,delta] = polyval(p,x,S,mu);
    end
    delta = delta * tinv(1-alpha/2,S.df);
else
    if nargin < 5
        y = polyval(p,x);
    else
        y = polyval(p,x,[],mu);
    end
end
