# include # include # include # include # include int main ( ); double *correlation_damped_sine ( int n, double rho[], double rho0 ); void mark_points_plot ( int n, double rho[], double c[], char *header, char *title ); double *r8vec_linspace_new ( int n, double a, double b ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: mark_points() shows how to add marked points to a plot. Discussion: You can't add more items to a gnuplot() plot once it's created, so if you want to plot some points, and then include some markers, then you have to create some auxilliary data files identifying those marked points, and refer to them in an extra-long single plot() command. Licensing: This code is distributed under the MIT license. Modified: 02 December 2025 Author: John Burkardt */ { double *c; int n = 101; double *rho; double rho0; printf ( "\n" ); printf ( "mark_points():\n" ); printf ( " C version\n" ); printf ( " Plot a damped sine function over a range\n" ); printf ( " Mark the first data point green, and the last one red.\n" ); rho0 = 1.0; rho = r8vec_linspace_new ( n, -12.0, 12.0 ); c = correlation_damped_sine ( n, rho, rho0 ); mark_points_plot ( n, rho, c, "mark_points", "Marking points in gnuplot" ); /* Free memory. */ free ( rho ); free ( c ); /* Terminate. */ printf ( "\n" ); printf ( "damped_sine():\n" ); printf ( " Normal end of execution.\n" ); return 0; } /******************************************************************************/ double *correlation_damped_sine ( int n, double rho[], double rho0 ) /******************************************************************************/ /* Purpose: correlation_damped_sine() evaluates the damped sine correlation function. Licensing: This code is distributed under the MIT license. Modified: 05 January 2013 Author: John Burkardt Reference: Petter Abrahamsen, A Review of Gaussian Random Fields and Correlation Functions, Norwegian Computing Center, 1997. Input: int N, the number of arguments. double RHO[N], the arguments. double RHO0, the correlation length. Output: double CORRELATION_DAMPED_SINE[N], the correlations. */ { double *c; int i; double rhohat; c = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { if ( rho[i] == 0.0 ) { c[i] = 1.0; } else { rhohat = fabs ( rho[i] ) / rho0; c[i] = sin ( rhohat ) / rhohat; } } return c; } /******************************************************************************/ void mark_points_plot ( int n, double rho[], double c[], char *header, char *title ) /******************************************************************************/ /* Purpose: mark_points_plot() adds two marked points to a plot. Licensing: This code is distributed under the MIT license. Modified: 02 December 2025 Author: John Burkardt Input: int N, the number of arguments. double RHO[N], the arguments. double C[N], the correlations. char *HEADER, an identifier for the files. char *TITLE, a title for the plot. */ { char command_filename[80]; FILE *command_unit; char data_filename[80]; FILE *data_unit; char end_data_filename[80]; int i; char start_data_filename[80]; /* Create the main datafile. */ 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", rho[i], c[i] ); } fclose ( data_unit ); printf ( " Created data file \"%s\".\n", data_filename ); /* Create a data file for the green point. */ strcpy ( start_data_filename, header ); strcat ( start_data_filename, "_start_data.txt" ); data_unit = fopen ( start_data_filename, "wt" ); fprintf ( data_unit, "%14.6g %14.6g\n", rho[0], c[0] ); fclose ( data_unit ); printf ( " Created data file \"%s\".\n", start_data_filename ); /* Create a data file for the red point. */ strcpy ( end_data_filename, header ); strcat ( end_data_filename, "_end_data.txt" ); data_unit = fopen ( end_data_filename, "wt" ); fprintf ( data_unit, "%14.6g %14.6g\n", rho[n-1], c[n-1] ); fclose ( data_unit ); printf ( " Created data file \"%s\".\n", end_data_filename ); /* The commands file will contain instructions to gnuplot about how to make the desired plot of the data. */ 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.png\"\n", header ); fprintf ( command_unit, "set xlabel 'Distance Rho'\n" ); fprintf ( command_unit, "set ylabel 'Correlation C(Rho)'\n" ); fprintf ( command_unit, "set title '%s'\n", title ); 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:2 with points pointtype 7 pointsize 3 linecolor rgb \"green\", \\\n", start_data_filename ); fprintf ( command_unit, " \"%s\" using 1:2 with points pointtype 7 pointsize 3 linecolor rgb \"red\" \n", end_data_filename ); fprintf ( command_unit, "quit\n" ); 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; }