/********************************************************* * This program calculates the Fourier coefficients * * of a periodic function F(x) by using the procedure * * AnalyticFourierHn of unit Fourier. * * ------------------------------------------------------ * * SAMPLE RUN: * * * * Example 1: F(x) has the following shape: * * * * 1 ---- 2 pi/4 * * ! ! * * ! ! * * ------!-------------------------------------------> x * * -pi ! 0 ! pi * * ! ! * * 3 ---------------- 4 -pi/4 * * * * Calculate the Fourier coefficients of a periodic * * function F(x): * * * * Begin x of period: -3.1415927 * * End of x period: 3.1415927 * * * * Lowest harmonic: 0 * * Highest harmonic: 10 * * * * a0 = -0.392684481 * * b0 = 0.000000000 * * * * a1 = -0.500000000 * * b1 = -0.500029201 * * * * a2 = -0.000029201 * * b2 = 0.5000000000 * * ----------------- * * a9 = -0.055555556 * * b9 = -0.055584756 * * * * a10 = -0.000029201 * * b10 = 0.100000000 * * * * Note that for this kind of sharp angles function, the * * method for discreet functions is far better. * * * * * * Example 2: F(x) equals 0 from -pi to 0 and sin(x) * * from 0 to pi. * * * * Calculate the Fourier coefficients of a periodic * * function F(x): * * * * Lowest harmonic: 0 * * Highest harmonic: 10 * * * * a0 = 0.318309886 * * b0 = 0.000000000 * * * * a1 = 0.000000012 * * b1 = 0.500000000 * * * * a2 = -0.212206591 * * b2 = 0.000000000 * * ----------------- * * a9 = -0.000000000 * * b9 = -0.000000000 * * * * a10 = -0.006430503 * * b10 = 0.000000000 * * * * C++ version by J-P Moreau, Paris. * * (www.jpmoreau.fr) * ********************************************************** (to link with file fourier.cpp). */ #include #include void AnalyticFourierHn(double,double,int,double *,double *); void main() { int count,h1,h2,i; double x1,x2, ai,bi; char answer[2]; printf("\n Calculate the Fourier coefficients of a periodic function F(x):\n"); printf(" (Function F(x) must be defined in file fourier.cpp).\n\n"); printf(" Begin x of period: "); scanf("%lf", &x1); printf(" End x of period : "); scanf("%lf", &x2); printf("\n Lowest harmonic: "); scanf("%d", &h1); printf("\n Highest harmonic: "); scanf("%d", &h2); count=1; for (i=h1; i<=h2; i++) { AnalyticFourierHn(x1,x2,i,&ai,&bi); printf("\n a%d = %12.9f", i, ai); printf("\n b%d = %12.9f\n", i, bi); if ((count % 5)==0) { count=0; scanf("%s", answer); } count++; } printf("\n\n"); } //end of file analfour.cpp