# include # include # include using namespace std; int main ( ) // // SURFACE_GRID.CPP evaluates a formula Z(X,Y) over a grid of (X,Y) // points -2.0 <= X <= 2.0, -2.0 <= Y <= 2.0. // // N = 41 points are used in X and in Y. // // This data is to be read by Gnuplot and displayed by the "splot" command, // which could be as simple as the single line: // // splot "surface_grid_gnuplot.txt" with lines // // Gnuplot requires that the data be listed in groups, with a blank line // between each group. Within one group, the X values should be fixed, // while the Y values increase. // { float dx; float dy; int n; float x; float x1; float x2; float y; float y1; float y2; float z; // // Use the same number of points for X and Y. // n = 41; // // Define the limits of X and Y, and the spacing. // x1 = -2.0; x2 = +2.0; dx = ( x2 - x1 ) / ( n - 1 ); y1 = -2.0; y2 = +2.0; dy = ( y2 - y1 ) / ( n - 1 ); // // Generate each (X,Y) point and evaluate Z(X,Y) and print it. // // Even though our FOR loop is "correct", we might not generate the last // values where X = 2.0 or Y = 2.0, because of roundoff. // // Using an integer counter is a safer way to guarantee you get // exactly what you want! // for ( x = -2.0; x <= 2.0; x = x + dx ) { for ( y = -2.0; y <= 2.0; y = y + dy ) { z = exp ( - ( x * x + y * y ) ) * cos ( x / 4.0 ) * sin ( y ) * cos ( 2 * ( x * x + y * y ) ); cout << x << " " << y << " " << z << "\n"; } cout << "\n"; } cerr << "\n"; cerr << "Created table of Z(X,Y) data for Gnuplot.\n"; return 0; }