/* prog3a.c -- 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. */ #include #include #define MAXn 10 static double a[MAXn+1]; static int n; void outpoly(){ int i; printf("\n\n#\tP(x)=%lg+",a[0]); for(i=1;im) m=r; } return m/fabs(a[n])+1.0; } double P(double x,double *DP){ int i; double p=a[n],q=0.0; for(i=1;i<=n;i++){ q=q*x+p; p=p*x+a[n-i]; } if(DP) *DP=q; return p; } main(){ for(;;){ if(scanf("%d",&n)!=1) exit(0); if(n<0) exit(0); if(n>MAXn) { puts("Capacity exceeded!"); exit(1); } { int i; for(i=0;i<=n;i++){ if(scanf("%lg",&a[i])!=1){ puts("Incomplete data!"); exit(2); } } } outpoly(); { int i,imax=20*n-1; double r=R(); printf("#\n#%20s %20s %20s\n","x","P(x)","P'(x)"); for(i=0;i<=imax;i++){ double x=r*i*(2.0/imax)-r; double p,dp; p=P(x,&dp); printf(" %20.16lg %20.16lg %20.16lg\n",x,p,dp); } } } }