/* Some code illistrating how to put numerical output into a file suitable for Matlab. See http://www.math.nyu.edu/faculty/goodman/teaching/SciComp2003 for more information. This routine created a file called lis.m, which is a Matlab m-file, that contains all the data from the run. To examine the data, start Matlab and type "lis", which will execute this file. */ #include /* Declarations of the routines in the math library. */ #include /* The C++ io library. This makes "cin" and "cout" possible. */ #include /* The C++ routines that communicate with files. These make "ifstream" and "ofstream" possible. */ #include /* So that we can format the output. */ #define N 1000 /* The size of the array for output. */ #define AMAX 10 /* A bound on a. */ #define TMAX 50 /* The maximum time for plotting */ #define OutputFileName "lis.m" /* The name of the output file */ int main() { int k; float t, dt, x, y, a; // Get the input data from the user. cout << "Type a" << endl; cin >> a; if ( abs(a) > AMAX ) { cout << "Stopping because specified a = " << a << " is bigger than AMAX = " << AMAX << endl; return 1; } // Open the ouput file and set up the parameters for output ofstream outFile; // Declare the output file, an object of typo ofstream. outFile.open(OutputFileName, ios::out ); // Open the file. if ( ! outFile ) { // If the file was not opened properly, bail out. cout << "In main program, outfile.open could not open file " << OutputFileName << ". Stopping here and returning." << endl; return 2; } /* Set up the format so that floating point numbers are printed in a uniform way. */ outFile << setprecision( 7 ); // Print six digits after the decimal point. outFile.setf( ios::scientific, ios::floatfield ); // Scientific notation with all zeros printed. // Start the computation. // The first lines are Matlab commands that record parameters. // If N is 100, the first line will be: "N = 100;". We often // end Matlab commands with a semicolon. outFile << "N = " << N << ";" << endl; outFile << "a = " << a << ";" << endl; outFile << "T = " << TMAX << ";" << endl; outFile << "V = [ " << endl; // The beginning of the output array. t = 0; dt = ( (float) TMAX)/ N ; for ( k=0; k < N; k++ ){ x = cos(t); y = cos(a*t); outFile << t << " " << x << " " << y << endl; t += dt; } outFile << " ] ; " << endl; return 0; }