{ prog3a.pas -- Tabulate a Polynomial Using Nested Multiplication Written Feb 20, 2001 by Eric Olson for Mathematics 483 For more information see Step 5 in First Steps in Numerical Analysis, 2nd Edition, by Hosking, Joe, Joyce and Turner. } program main; const MAXn=10; var a:array [0..MAXn+1] of real; n:integer; procedure outpoly; var i:integer; begin writeln; writeln; writeln('# P(x) = ',a[0]); for i:=1 to n do begin writeln('# + ',a[i],' x ^',i:2) end end; function cR:real; var i:integer; m,r:real; begin m:=0.0; for i:=0 to n-1 do begin r:=abs(a[i]); if r>m then m:=r end; cR:=m/abs(a[n])+1.0; end; function cP(const x:real;var DcP:real):real; var i:integer; p,q:real; begin p:=a[n]; q:=0.0; for i:=1 to n do begin q:=q*x+p; p:=p*x+a[n-i] end; DcP:=q; cP:=p end; var i,imax:integer; r,x,p,dp:real; begin repeat read(n); if n<0 then halt(0); if n>MAXn then begin writeln('Capacity exceeded!'); halt(1) end; for i:=0 to n do begin read(a[i]) end; outpoly; writeln('#'); write('#'); write(' x '); write(' P(x) '); writeln(' P''(x)'); r:=cR; imax:=20*n-1; for i:=0 to imax do begin x:=r*i*(2.0/imax)-r; p:=cP(x,dp); writeln(' ',x:20,' ',p:20,' ',dp:20) end until(FALSE); end.