# include # include # include # include # include # include "steinerberger.h" int main ( ); 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: steinerberger_test() tests steinerberger(). Licensing: This code is distributed under the MIT license. Modified: 29 March 2025 Author: John Burkardt */ { double *fx; char header[22]; int i; int j; int n; static int ntest[6] = { 1, 5, 10, 25, 50, 100 }; int nx = 5001; double *x; timestamp ( ); printf ( "\n" ); printf ( "steinerberger_test():\n" ); printf ( " C version\n" ); printf ( " Test steinerberger().\n" ); /* Plots. */ printf ( "\n" ); printf ( " Plot f(n,x) for several values of n\n" ); printf ( "\n" ); x = r8vec_linspace_new ( nx, 0.0, 1.0 ); fx = ( double * ) malloc ( nx * sizeof ( double ) ); for ( i = 0; i < 6; i++ ) { n = ntest[i]; for ( j = 0; j < nx; j++ ) { fx[j] = steinerberger_function ( n, x[j] ); } sprintf ( header, "steinerberger_%03d", n ); gnuplot_fx ( nx, x, fx, header ); } free ( fx ); free ( x ); /* Terminate. */ printf ( "\n" ); printf ( "steinerberger_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ 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 }