/************************************************** * Chebyshev Approximation of a user defined real * * function FUNC(X) in double precision. * * ----------------------------------------------- * * SAMPLE RUN: * * (Approximate sin(x) from x=0 to x=PI). * * * * Chebyshev coefficients (N=10): * * 0.94400243153647 * * 0.00000000000000 * * -0.49940325827041 * * -0.00000000000000 * * 0.02799207961755 * * -0.00000000000000 * * -0.00059669519580 * * 0.00000000000000 * * 0.00000670417552 * * -0.00000000000000 * * X Chebyshev Eval. SIN(X) * * ------------------------------------------- * * 0.00000000 0.00000005 0.00000000 * * 0.34906585 0.34202018 0.34202014 * * 0.69813170 0.64278757 0.64278761 * * 1.04719755 0.86602545 0.86602540 * * 1.39626340 0.98480773 0.98480775 * * 1.74532925 0.98480773 0.98480775 * * 2.09439510 0.86602545 0.86602540 * * 2.44346095 0.64278757 0.64278761 * * 2.79252680 0.34202018 0.34202014 * * 3.14159265 0.00000005 0.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 TWO 2.0 #define ZERO 0.0 #define PI 4.0*atan(1.0) void CHEBFT(double A,double B, double *C, int N); double CHEBEV(double A,double B, double *C, int M, double X); double X0,X1, COEFF[NMAX]; double DX, X; int I, N; // user defined function double FUNC(double x) { return (sin(x)); } void main() { N=10; X0=ZERO; X1=PI; CHEBFT(X0,X1,COEFF,N); printf(" Chebyshev coefficients (N=%d):\n", N); for (I=1; I<=N; I++) printf(" %17.14f\n", COEFF[I]); DX=(X1-X0)/(N-1); X=X0-DX; printf(" X Chebyshev Eval. SIN(X) \n"); printf(" --------------------------------------------\n"); for (I=1; I<=N; I++) { X += DX; printf("%11.8f %15.8f %15.8f\n", X, CHEBEV(X0,X1,COEFF,N,X), FUNC(X)); } } // end of file Tchebysh.cpp