diff options
Diffstat (limited to 'macros/polyval.sci')
-rwxr-xr-x | macros/polyval.sci | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/macros/polyval.sci b/macros/polyval.sci index bcd5dfb..0ec7a82 100755 --- a/macros/polyval.sci +++ b/macros/polyval.sci @@ -1,15 +1,28 @@ function [y, delta] = polyval(p,x,S,mu) +//y = polyval(p,x) returns the value of a polynomial of degree n evaluated at x. The input argument p is a vector of length n+1 whose elements are the coefficients in descending powers of the polynomial to be evaluated. +//x --can be a matrix or a vector. In either case, polyval evaluates p at each element of x. +//[y,delta] = polyval(p,x,S) uses the optional output structure S generated by polyfit to generate error estimates delta. +//delta --is an estimate of the standard deviation of the error in predicting a future observation at x by p(x). +// If the coefficients in p are least squares estimates computed by polyfit, and the errors in the data input to poly// if it are independent, normal, and have constant variance, then y±delta contains at least 50% of the +// predictions of future observations at x. -// Check input is a vector -if ~(isvector(p) | isempty(p)) - error(message('polyval:InvalidP')); +//y = polyval(p,x,[],mu) or [y,delta] = polyval(p,x,S,mu) use ˆx=(x−μ1)/μ2 in place of x. In this equation, μ1=mean(x) // and μ2=std(x). The centering and scaling parameters mu = [μ1,μ2] is optional + +//EXAMPLES: +//p = [3 2 1]; +//y=polyval(p,[5 7 9]) +//EXPECTED OUTPUT: +//y= 86 162 262 + +if ~(isvector(p) | isempty(p)) // Check input is a vector + error('polyval:InvalidP'); end nc = length(p); -if isscalar(x) & (argn(2) < 3) & nc>0 & isfinite(x) & all(isfinite(p(:))) +if isscalar(x) & (argn(2) < 3) & nc>0 & (abs(x)<%inf) & and(abs(p(:))<%inf) // Make it scream for scalar x. Polynomial evaluation can be // implemented as a recursive digital filter. - y = filter(1,[1 -x],p); + y = filter(1,[1 -real(x)],p); y = y(nc); return end @@ -28,9 +41,9 @@ end if argn(1) > 1 if argn(2) < 3 | isempty(S) - error(message('polyval:RequiresS')); + error('polyval:Requires S'); end - + // Extract parameters from S if isstruct(S), // Use output structure from polyfit. R = S.R; @@ -39,7 +52,7 @@ if argn(1) > 1 else // Use output matrix from previous versions of polyfit. [ms,ns] = size(S); if (ms ~= ns+2) | (nc ~= ns) - error(message('polyval:SizeS')); + error('polyval:SizeS'); end R = S(1:nc,1:nc); df = S(nc+1,1); @@ -59,12 +72,11 @@ if argn(1) > 1 E = V/R; e = sqrt(1+sum(E.*E,2)); if df == 0 - warning(message('polyval:ZeroDOF')); - delta = Inf(size(e)); + warning('polyval:ZeroDOF'); + delta = %inf(size(e)); else delta = normr/sqrt(df)*e; end - delta = reshape(delta,siz_x); + delta = matrix(delta,siz_x); end endfunction - |