# include # include # include # include # include int main ( ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: grid_surface() draws a surface from a table Z(X,Y). Discussion: Here, the grid is a 41x41 array of equally spaced points in [-2,2]x[-2,2]. and Z(X,Y) is a function which can be evaluated by: Z = exp ( - ( X^2 + Y^2 ) ) * cos ( 0.25 * X ) * sin ( Y ) * cos ( 2 * (X^2 + Y^2 ) ) Licensing: This code is distributed under the MIT license. Modified: 26 September 2026 Author: John Burkardt */ { char command_filename[80]; FILE *command; char data_filename[80]; FILE *data; char header[] = "grid"; int i; int j; int m; int n; double r2; double x; double y; double z; timestamp ( ); printf ( "\n" ); printf ( "grid_surface():\n" ); printf ( " C version\n" ); printf ( " Make a surface plot Z(X,Y) for a 41x41 table.\n" ); /* Set number of points in plot. */ m = 41; n = 41; /* Define the data. */ strcpy ( data_filename, header ); strcat ( data_filename, "_data.txt" ); data = fopen ( data_filename, "wt" ); for ( i = 0; i < m; i++ ) { if ( 0 < i ) { fprintf ( data, "\n" ); } x = ( ( m - i - 1 ) * ( -2.0 ) + ( i ) * ( 2.0 ) ) / ( m - 1 ); for ( j = 0; j < n; j++ ) { y = ( ( n - j - 1 ) * ( -2.0 ) + ( j ) * ( 2.0 ) ) / ( n - 1 ); r2 = x * x + y * y; z = exp ( - r2 ) * cos ( 0.25 * x ) * sin ( y ) * cos ( 2 * r2 ); fprintf ( data, " %g %g %g\n", x, y, z ); } } fclose ( data ); printf ( "\n" ); printf ( " Data stored in \"%s\".\n", data_filename ); /* Create the command file. */ strcpy ( command_filename, header ); strcat ( command_filename, "_commands.txt" ); command = fopen ( command_filename, "wt" ); fprintf ( command, "# %s\n", command_filename ); fprintf ( command, "#\n" ); fprintf ( command, "# Usage:\n" ); fprintf ( command, "# gnuplot < %s\n", command_filename ); fprintf ( command, "#\n" ); fprintf ( command, "set term png\n" ); fprintf ( command, "set output '%s_surface.png'\n", header ); fprintf ( command, "set xlabel 'X'\n" ); fprintf ( command, "set ylabel 'Y'\n" ); fprintf ( command, "set zlabel 'Z'\n" ); fprintf ( command, "set title 'Grid surface'\n" ); fprintf ( command, "set view 77, 77\n" ); fprintf ( command, "set xyplane -0.5\n" ); fprintf ( command, "set hidden3d\n" ); fprintf ( command, "set timestamp\n" ); fprintf ( command, "set grid\n" ); fprintf ( command, "set style data lines\n" ); fprintf ( command, "splot '%s' with lines\n", data_filename ); fprintf ( command, "quit\n" ); fclose ( command ); printf ( "\n" ); printf ( " Plot commands stored in \"%s\".\n", command_filename ); /* Terminate. */ printf ( "\n" ); printf ( "grid_surface():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ 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: 17 June 2014 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 }