%Matlab code polynest.m
%   function val=polymult(p,x)
%
%Evaluate a polynomial f at a specified point x
%All internal computations are done in single-precision
%
%Inputs:
%   p      coefficients of the polynomial.  
%          f(x)=p(1)x^n + ... + +p(n)x + p(n+1)
%          this must be a row vector
%   x      point at which to evaluate f
%
%Outputs:
%   val    the value of f(x)
%          this is double-precision
%
%   matlab function POLYVAL(p,x) gives a more precise answer
%   abs(polyval(p,x)-polymult(p,x)) gives the error
%
%   sample code to test:
%   p=rand(1,100);
%   x=1+2*rand;
%   e=abs(polyval(p,x)-polynest(p,x));
%   disp(e);

function val=polynest(p,x)

n = size(p,2) - 1;
%find the order of the polynomial

out=single(0);
%initialize output

xs=single(x);
ps=single(p);
%convert to single precision

for i=0:n
   out=out*x;
   %multiply current result by x

   out = out + ps(i+1);
   %add the next monomial term
end

val=double(out);
%convert back to double precision

end