/************************************************** * Chebyshev Approximation of a user defined real * * function FUNC(X) in double precision. * * ----------------------------------------------- * * SAMPLE RUN: * * (Approximate integral of sin(x) from x=0 to * * x=PI). * * * * X Chebyshev Eval. -COS(X)+1 * * of integral * * ---------------------------------------------- * * 0.00000000 -0.00000000 0.00000000 * * 0.15707963 0.01231166 0.01231166 * * 0.31415927 0.04894348 0.04894348 * * 0.47123890 0.10899348 0.10899348 * * 0.62831853 0.19098301 0.19098301 * * 0.78539816 0.29289321 0.29289322 * * 0.94247780 0.41221474 0.41221475 * * 1.09955743 0.54600950 0.54600950 * * 1.25663706 0.69098301 0.69098301 * * 1.41371669 0.84356554 0.84356553 * * 1.57079633 1.00000000 1.00000000 * * 1.72787596 1.15643446 1.15643447 * * 1.88495559 1.30901699 1.30901699 * * 2.04203522 1.45399050 1.45399050 * * 2.19911486 1.58778526 1.58778525 * * 2.35619449 1.70710679 1.70710678 * * 2.51327412 1.80901699 1.80901699 * * 2.67035376 1.89100652 1.89100652 * * 2.82743339 1.95105651 1.95105652 * * 2.98451302 1.98768834 1.98768834 * * 3.14159265 2.00000000 2.00000000 * * * * C++ Release By J-P Moreau, Paris. * * (www.jpmoreau.fr) * **************************************************/ // To link with Chebyshe.cpp #include #include #define NMAX 51 #define HALF 0.5 #define ONE 1.0 #define TWO 2.0 #define ZERO 0.0 #define PI 4.0*atan(1.0) void CHEBFT(double, double, double *, int); double CHEBEV(double, double, double *, int, double); void CHINT(double,double,double *,double *,int); double X0,X1, COEFF[NMAX], CINT[NMAX]; double DX, X; int I,N,NPTS; // user defined function double FUNC(double x) { return (sin(x)); } void main() { N=10; X0=ZERO; X1=PI; // calculate Chebyshev coefficients of FUNC(x) CHEBFT(X0,X1,COEFF,N); // calculate Chebyshev coefficients of integral of FUNC(x) CHINT(X0,X1,COEFF,CINT,N); NPTS=2*N+1; DX=(X1-X0)/(NPTS-1); X=X0-DX; printf(" X Chebyshev Eval. -COS(X)+1 \n"); printf(" of integral \n"); printf(" --------------------------------------------\n"); for (I=1; I<=NPTS; I++) { X += DX; printf("%11.8f %15.8f %15.8f\n", X, CHEBEV(X0,X1,CINT,N,X), -cos(X)+ONE); } } // end of file Tchebint.cpp