%Matlab code polymult.m % function val=polymult(p,x) % %Evaluate a polynomial f at a specified point x %All internal computations are 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)-polymult(p,x)); % disp(e); function val=polymult(p, x) n = size(p,2) - 1; %find the order of the polynomial out=single(0); %initialize output pow = single(1); %start at x^0=1 xs=single(x); ps=single(p); %convert to single precision for i=0:n out = out + ps( (n+1)-i)*pow; %add the appropriate multiple of x^i pow = pow*xs; %compute x^(i+1) end val=double(out); %convert back to double precision end