# include # include # include # include # include # include "fresnel.h" int main ( ); void fresnel_cos_plot ( ); void fresnel_cos_values ( int *n_data, double *x, double *fx ); void fresnel_cos_values_test ( ); void fresnel_phase_plot ( ); void fresnel_sin_plot ( ); void fresnel_sin_values ( int *n_data, double *x, double *fx ); void fresnel_sin_values_test ( ); void gnuplot_fx ( int n, double *x, double *y, char *header ); double *r8vec_linspace_new ( int n, double a, double b ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: fresnel_test() tests fresnel(). Licensing: This code is distributed under the MIT license. Modified: 11 July 2025 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "fresnel_test():\n" ); printf ( " C version\n" ); printf ( " Test fresnel()\n" ); fresnel_cos_values_test ( ); fresnel_sin_values_test ( ); fresnel_cos_plot ( ); fresnel_sin_plot ( ); fresnel_phase_plot ( ); /* Terminate. */ printf ( "\n" ); printf ( "fresnel_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void fresnel_cos_plot ( ) /******************************************************************************/ /* Purpose: fresnel_cos_plot() tests fresnel_cos(). Licensing: This code is distributed under the MIT license. Modified: 11 July 2025 Author: John Burkardt */ { double a; double b; char *header = "fresnel_cos_plot"; int i; int n; double *x; double *y; printf ( "\n" ); printf ( "fresnel_cos_plot():\n" ); printf ( " Plot (X,C(X)), where C(X) is the Fresnel cosine integral.\n" ); a = -5.0; b = +5.0; n = 501; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { y[i] = fresnel_cos ( x[i] ); } gnuplot_fx ( n, x, y, header ); free ( x ); free ( y ); return; } /******************************************************************************/ void fresnel_cos_values ( int *n_data, double *x, double *fx ) /******************************************************************************/ /* Purpose: fresnel_cos_values() returns values of the Fresnel cosine integral function. Discussion: The Fresnel cosine integral is defined by: C(X) = integral ( 0 <= T <= X ) cos ( PI * T^2 / 2 ) dT In Mathematica, the function can be evaluated by: FresnelC[x] Licensing: This code is distributed under the MIT license. Modified: 16 November 2015 Author: John Burkardt Reference: Milton Abramowitz, Irene Stegun, Handbook of Mathematical Functions, National Bureau of Standards, 1964, ISBN: 0-486-61272-4, LC: QA47.A34. Stephen Wolfram, The Mathematica Book, Fourth Edition, Cambridge University Press, 1999, ISBN: 0-521-64314-7, LC: QA76.95.W65. Input: int *N_DATA. The user sets N_DATA to 0 before the first call. Output: int *N_DATA. The routine increments N_DATA by 1, and returns the corresponding data; when there is no more data, the output value of N_DATA will be 0 again. double *X, the argument of the function. double *FX, the value of the function. */ { # define N_MAX 16 static double fx_vec[N_MAX] = { 0.0000000000000000E+00, 0.1999210575944531E+00, 0.3974807591723594E+00, 0.5810954469916523E+00, 0.7228441718963561E+00, 0.7798934003768228E+00, 0.7154377229230734E+00, 0.5430957835462564E+00, 0.3654616834404877E+00, 0.3336329272215571E+00, 0.4882534060753408E+00, 0.6362860449033195E+00, 0.5549614058564281E+00, 0.3889374961919690E+00, 0.4674916516989059E+00, 0.6057207892976856E+00 }; static double x_vec[N_MAX] = { 0.0E+00, 0.2E+00, 0.4E+00, 0.6E+00, 0.8E+00, 1.0E+00, 1.2E+00, 1.4E+00, 1.6E+00, 1.8E+00, 2.0E+00, 2.2E+00, 2.4E+00, 2.6E+00, 2.8E+00, 3.0E+00 }; if ( *n_data < 0 ) { *n_data = 0; } *n_data = *n_data + 1; if ( N_MAX < *n_data ) { *n_data = 0; *x = 0.0; *fx = 0.0; } else { *x = x_vec[*n_data-1]; *fx = fx_vec[*n_data-1]; } return; # undef N_MAX } /******************************************************************************/ void fresnel_cos_values_test ( ) /******************************************************************************/ /* Purpose: fresnel_cos_values_test() tests fresnel_cos_values(). Licensing: This code is distributed under the MIT license. Modified: 11 July 2025 Author: John Burkardt */ { double fx1; double fx2; int n_data; double x; printf ( "\n" ); printf ( "fresnel_cos_values_test():\n" ); printf ( " fresnel_cos_values() stores values of\n" ); printf ( " the Fresnel cosine integral C(X).\n" ); printf ( " fresnel_cos() evaluates the Fresnel cosine integral C(X).\n" ); printf ( "\n" ); printf ( " X C(X)\n" ); printf ( "\n" ); n_data = 0; for ( ; ; ) { fresnel_cos_values ( &n_data, &x, &fx1 ); if ( n_data == 0 ) { break; } fx2 = fresnel_cos ( x ); printf ( " %12g %24.16g %24.16g\n", x, fx1, fx2 ); } return; } /******************************************************************************/ void fresnel_phase_plot ( ) /******************************************************************************/ /* Purpose: fresnel_phase_plot() tests fresnel_cos() and fresnel_sin(). Licensing: This code is distributed under the MIT license. Modified: 11 July 2025 Author: John Burkardt */ { double a; double b; double *c; char *header = "fresnel_phase_plot"; int i; int n; double *s; double *x; printf ( "\n" ); printf ( "fresnel_phase_plot():\n" ); printf ( " Plot (C(X),S(X)), where C(X) and S(X) are\n" ); printf ( " the Fresnel cosine and sine integrals.\n" ); a = -5.0; b = +5.0; n = 501; x = r8vec_linspace_new ( n, a, b ); c = ( double * ) malloc ( n * sizeof ( double ) ); s = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { fresnel ( x[i], c+i, s+i ); } gnuplot_fx ( n, c, s, header ); free ( c ); free ( s ); free ( x ); return; } /******************************************************************************/ void fresnel_sin_plot ( ) /******************************************************************************/ /* Purpose: fresnel_sin_plot() tests fresnel_sin(). Licensing: This code is distributed under the MIT license. Modified: 11 July 2025 Author: John Burkardt */ { double a; double b; char *header = "fresnel_sin_plot"; int i; int n; double *x; double *y; printf ( "\n" ); printf ( "fresnel_sin_plot():\n" ); printf ( " Plot (X,S(X)), where S(X) is the Fresnel sine integral.\n" ); a = -5.0; b = +5.0; n = 501; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { y[i] = fresnel_sin ( x[i] ); } gnuplot_fx ( n, x, y, header ); free ( x ); free ( y ); return; } /******************************************************************************/ void fresnel_sin_values ( int *n_data, double *x, double *fx ) /******************************************************************************/ /* Purpose: fresnel_sin_values() returns some values of the Fresnel sine integral function. Discussion: The Fresnel sine integral is defined by S(X) = integral ( 0 <= T <= X ) sin ( pi * T^2 / 2 ) dT In Mathematica, the function can be evaluated by: FresnelS[x] Licensing: This code is distributed under the MIT license. Modified: 16 November 2015 Author: John Burkardt Reference: Milton Abramowitz, Irene Stegun, Handbook of Mathematical Functions, National Bureau of Standards, 1964, ISBN: 0-486-61272-4, LC: QA47.A34. Stephen Wolfram, The Mathematica Book, Fourth Edition, Cambridge University Press, 1999, ISBN: 0-521-64314-7, LC: QA76.95.W65. Input: int *N_DATA. The user sets N_DATA to 0 before the first call. Output: int *N_DATA. The routine increments N_DATA by 1, and returns the corresponding data; when there is no more data, the output value of N_DATA will be 0 again. double *X, the argument of the function. double *FX, the value of the function. */ { # define N_MAX 16 static double fx_vec[N_MAX] = { 0.0000000000000000E+00, 0.4187609161656762E-02, 0.3335943266061318E-01, 0.1105402073593870E+00, 0.2493413930539178E+00, 0.4382591473903548E+00, 0.6234009185462497E+00, 0.7135250773634121E+00, 0.6388876835093809E+00, 0.4509387692675831E+00, 0.3434156783636982E+00, 0.4557046121246569E+00, 0.6196899649456836E+00, 0.5499893231527195E+00, 0.3915284435431718E+00, 0.4963129989673750E+00 }; static double x_vec[N_MAX] = { 0.0E+00, 0.2E+00, 0.4E+00, 0.6E+00, 0.8E+00, 1.0E+00, 1.2E+00, 1.4E+00, 1.6E+00, 1.8E+00, 2.0E+00, 2.2E+00, 2.4E+00, 2.6E+00, 2.8E+00, 3.0E+00 }; if ( *n_data < 0 ) { *n_data = 0; } *n_data = *n_data + 1; if ( N_MAX < *n_data ) { *n_data = 0; *x = 0.0; *fx = 0.0; } else { *x = x_vec[*n_data-1]; *fx = fx_vec[*n_data-1]; } return; # undef N_MAX } /******************************************************************************/ void fresnel_sin_values_test ( ) /******************************************************************************/ /* Purpose: fresnel_sin_values_test() tests fresnel_sin_values(). Licensing: This code is distributed under the MIT license. Modified: 11 July 2025 Author: John Burkardt */ { double fx1; double fx2; int n_data; double x; printf ( "\n" ); printf ( "fresnel_sin_values_test():\n" ); printf ( " fresnel_sin_values() stores values of\n" ); printf ( " the Fresnel sine integral S(X).\n" ); printf ( " fresnel_sin() evaluates the Fresnel sine integral S(X).\n" ); printf ( "\n" ); printf ( " X S(X)\n" ); printf ( "\n" ); n_data = 0; for ( ; ; ) { fresnel_sin_values ( &n_data, &x, &fx1 ); if ( n_data == 0 ) { break; } fx2 = fresnel_sin ( x ); printf ( " %12g %24.16g %24.16g\n", x, fx1, fx2 ); } return; } /******************************************************************************/ void gnuplot_fx ( int n, double *x, double *y, char *header ) /******************************************************************************/ /* Purpose: gnuplot_fx() plots n values y=f(x). Discussion: Actually, we simply create two files for processing by gnuplot(). Licensing: This code is distributed under the MIT license. Modified: 24 March 2025 Author: John Burkardt */ { char command_filename[255]; FILE *command_unit; char data_filename[255]; FILE *data_unit; int i; char png_filename[255]; /* Create a graphics data file. */ strcpy ( data_filename, header ); strcat ( data_filename, "_data.txt" ); data_unit = fopen ( data_filename, "wt" ); for ( i = 0; i < n; i++ ) { fprintf ( data_unit, "%14.6g %14.6g\n", x[i], y[i] ); } fclose ( data_unit ); printf ( " Created graphics data file '%s'\n", data_filename ); strcpy ( png_filename, header ); strcat ( png_filename, ".png" ); /* Create graphics command file. */ strcpy ( command_filename, header ); strcat ( command_filename, "_commands.txt" ); command_unit = fopen ( command_filename, "wt" ); fprintf ( command_unit, "# %s\n", command_filename ); fprintf ( command_unit, "#\n" ); fprintf ( command_unit, "# Usage:\n" ); fprintf ( command_unit, "# gnuplot < %s\n", command_filename ); fprintf ( command_unit, "#\n" ); fprintf ( command_unit, "set term png\n" ); fprintf ( command_unit, "set output '%s'\n", png_filename ); fprintf ( command_unit, "set xlabel '<--- X --->'\n" ); fprintf ( command_unit, "set ylabel '<-- Y(X) -->'\n" ); fprintf ( command_unit, "set title '%s'\n", header ); fprintf ( command_unit, "set grid\n" ); fprintf ( command_unit, "set style data lines\n" ); fprintf ( command_unit, "plot '%s' using 1:2 lw 3 linecolor rgb 'blue'\n", data_filename ); fclose ( command_unit ); printf ( " Created command file '%s'\n", command_filename ); return; } /******************************************************************************/ double *r8vec_linspace_new ( int n, double a, double b ) /******************************************************************************/ /* Purpose: r8vec_linspace_new() creates a vector of linearly spaced values. Discussion: An R8VEC is a vector of R8's. 4 points evenly spaced between 0 and 12 will yield 0, 4, 8, 12. In other words, the interval is divided into N-1 even subintervals, and the endpoints of intervals are used as the points. Licensing: This code is distributed under the MIT license. Modified: 29 March 2011 Author: John Burkardt Input: int N, the number of entries in the vector. double A, B, the first and last entries. Output: double R8VEC_LINSPACE_NEW[N], a vector of linearly spaced data. */ { int i; double *x; x = ( double * ) malloc ( n * sizeof ( double ) ); if ( n == 1 ) { x[0] = ( a + b ) / 2.0; } else { for ( i = 0; i < n; i++ ) { x[i] = ( ( double ) ( n - 1 - i ) * a + ( double ) ( i ) * b ) / ( double ) ( n - 1 ); } } return x; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: timestamp() prints the current YMDHMS date as a time stamp. Example: 17 June 2014 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 01 May 2021 Author: John Burkardt */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }