# include<iostream.h> # include<math.h> # include<iomanip.h> void main() { //program d2r7 //driver for routine polcoe int nfunc,i,j,np = 5; double x,f,sum,xa[6], ya[6], coeff[6],pi = 3.141593; for (nfunc = 1; nfunc<=2; nfunc++) { if (nfunc == 1) { cout<<endl; cout<< "Sine function from 0 to pi"<<endl; for (i = 1; i<=np; i++) { xa[i] = i * pi /double( np); ya[i] = sin(xa[i]); } } else { if (nfunc ==2) { cout<<endl; cout<< "Exponential function from 0 to 1"<<endl; for (i = 1; i<=np; i++) { xa[i] = 1.0 * i / double(np); ya[i] = exp(xa[i]); } } else return; } polcoe(xa, ya, np, coeff); cout<<"coefficients"<<endl; for (i = 1; i<=np; i++) cout<<setw(12)<<coeff[i]; cout<<endl; cout<< " x f[x] polynomial"<<endl; cout<<setprecision(6)<<setiosflags(ios::fixed); for (i = 1; i<=10; i++) { if (nfunc == 1 ) { x = (-0.05 + i / 10.0) * pi; f = sin(x); } else { if (nfunc == 2) { x = (-0.05 + double(i) / 10.0); f = exp(x); } } sum = coeff[np]; for (j = np - 1; j>=1; j--) { sum = coeff[j] + sum * x; } cout<<setw(14)<<x; cout<<setw(14)<<f; cout<<setw(14)<<sum<<endl; } cout<< "**********************************"<<endl; } }