# include # include # include # include # include # include "runge.h" int main ( ); void runge_antideriv_test ( ); void runge_deriv_test ( ); void runge_deriv2_test ( ); void runge_fun_test ( ); void runge_power_series_test ( ); void gnuplot_fx ( int n, double *x, double *y, char *header ); void gnuplot_fxdd ( int n, double *x, double *y1, double *y2, char *header ); double *r8vec_linspace_new ( int n, double a, double b ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: runge_test() tests runge(). Licensing: This code is distributed under the MIT license. Modified: 24 March 2025 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "runge_test():\n" ); printf ( " C version\n" ); printf ( " Test runge().\n" ); runge_antideriv_test ( ); runge_deriv_test ( ); runge_deriv2_test ( ); runge_fun_test ( ); runge_power_series_test ( ); /* Terminate. */ printf ( "\n" ); printf ( "runge_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void runge_antideriv_test ( ) /******************************************************************************/ /* Purpose: runge_antideriv_test() tests runge_antideriv(). Licensing: This code is distributed under the MIT license. Modified: 24 March 2025 Author: John Burkardt */ { double a; double b; char *header = "runge_antideriv"; int i; int n = 51; double *x; double *y; printf ( "\n" ); printf ( "runge_antideriv_test():\n" ); printf ( " runge_antideriv() evaluates the antiderivative\n" ); printf ( " of the runge function.\n" ); a = -1.0; b = +1.0; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { y[i] = runge_antideriv ( x[i] ); } gnuplot_fx ( n, x, y, header ); free ( x ); free ( y ); return; } /******************************************************************************/ void runge_deriv_test ( ) /******************************************************************************/ /* Purpose: runge_deriv_test() tests runge_deriv(). Licensing: This code is distributed under the MIT license. Modified: 24 March 2025 Author: John Burkardt */ { double a; double b; char *header = "runge_deriv"; int i; int n = 51; double *x; double *y; printf ( "\n" ); printf ( "runge_deriv_test():\n" ); printf ( " runge_deriv() evaluates the derivative\n" ); printf ( " of the runge function.\n" ); a = -1.0; b = +1.0; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { y[i] = runge_deriv ( x[i] ); } gnuplot_fx ( n, x, y, header ); free ( x ); free ( y ); return; } /******************************************************************************/ void runge_deriv2_test ( ) /******************************************************************************/ /* Purpose: runge_deriv2_test() tests runge_deriv2(). Licensing: This code is distributed under the MIT license. Modified: 24 March 2025 Author: John Burkardt */ { double a; double b; char *header = "runge_deriv2"; int i; int n = 51; double *x; double *y; printf ( "\n" ); printf ( "runge_deriv2_test():\n" ); printf ( " runge_deriv2() evaluates the second derivative\n" ); printf ( " of the runge function.\n" ); a = -1.0; b = +1.0; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { y[i] = runge_deriv2 ( x[i] ); } gnuplot_fx ( n, x, y, header ); free ( x ); free ( y ); return; } /******************************************************************************/ void runge_fun_test ( ) /******************************************************************************/ /* Purpose: runge_fun_test() tests runge_fun(). Licensing: This code is distributed under the MIT license. Modified: 24 March 2025 Author: John Burkardt */ { double a; double b; char *header = "runge_fun"; int i; int n = 51; double *x; double *y; printf ( "\n" ); printf ( "runge_fun_test():\n" ); printf ( " runge_fun() evaluates the runge function.\n" ); a = -1.0; b = +1.0; x = r8vec_linspace_new ( n, a, b ); y = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { y[i] = runge_fun ( x[i] ); } gnuplot_fx ( n, x, y, header ); free ( x ); free ( y ); return; } /******************************************************************************/ void runge_power_series_test ( ) /******************************************************************************/ /* Purpose: runge_power_series_test() tests runge_power_series(). Licensing: This code is distributed under the MIT license. Modified: 25 March 2025 Author: John Burkardt */ { double a; double b; char *header = "runge_power_series"; int i; int n = 51; int n2 = 8; double *x; double *y1; double *y2; printf ( "\n" ); printf ( "runge_power_series_test():\n" ); printf ( " runge_power_series() compares a power series to\n" ); printf ( " the Runge function.\n" ); a = -0.2; b = +0.2; x = r8vec_linspace_new ( n, a, b ); y1 = ( double * ) malloc ( n * sizeof ( double ) ); y2 = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { y1[i] = runge_fun ( x[i] ); y2[i] = runge_power_series ( x[i], n2 ); } gnuplot_fxdd ( n, x, y1, y2, header ); free ( x ); free ( y1 ); free ( y2 ); 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; } /******************************************************************************/ void gnuplot_fxdd ( int n, double *x, double *y1, double *y2, char *header ) /******************************************************************************/ /* Purpose: gnuplot_fxdd() plots n values of y1=f(x) (curve) and y2 = x2 (data) Discussion: Actually, we simply create two files for processing by gnuplot(). Licensing: This code is distributed under the MIT license. Modified: 25 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 %14.6g\n", x[i], y1[i], y2[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 ); fprintf ( command_unit, " '%s' using 1:3 with points pointtype 7 linecolor 'red'\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 }