#include #include #include //#define _DEBUG_ //#define _INTERACTIVE_ //#define _SUPERPOSITIONEXAMPLE_ using namespace std; const double PI = 4.0*atan(1.0); double waveApprox( double position, double velocity, double time, int numTerms); int main (int argc, char * const argv[]) { #ifdef _SUPERPOSITIONEXAMPLE_ const string baseDirString = "/Users/rj/Desktop/"; const string fiveTerms = baseDirString + "fiveTerms.dat"; const string twentyTerms = baseDirString + "twentyTerms.dat"; const string twoHundredTerms = baseDirString + "twoHundredTerms.dat"; ofstream fiveTermsOut; ofstream twentyTermsOut; ofstream twoHundredTermsOut; fiveTermsOut.open(fiveTerms.c_str()); twentyTermsOut.open(twentyTerms.c_str()); twoHundredTermsOut.open(twoHundredTerms.c_str()); ofstream* fileArray[] = {&fiveTermsOut, &twentyTermsOut, &twoHundredTermsOut}; #endif int numTerms; double position, finalPosition, initialTime, initialVelocity, stepSize; //set some initial conditions that will work with _INTERACTIVE_ turned off position = 2; initialVelocity = 1; stepSize = 0.1; initialTime = 0; numTerms = 5; finalPosition = 20; #ifdef _DEBUG_ cout << waveApprox(2,1,1,5) << endl; #endif #ifdef _INTERACTIVE_ cout << "Interactive wave motion data generator\n"; cout << "Please enter the number of harmonics: "; cin >> numTerms; cout << "Please enter the initial position: "; cin >> position; cout << "Please enter the final position: "; cin >> finalPosition; cout << "Please enter the initial time: "; cin >> initialTime; cout << "Please enter the initial velocity: "; cin >> initialVelocity; cout << "Please enter the set size: "; cin >> stepSize; #ifdef _DEBUG_ cout << "Number of Terms: " << numTerms << " Initial Position: " << position << " Final Position: " << finalPosition << " Initial Time: " << initialTime << " Initial Velocity: " << initialVelocity << " Step size: " << stepSize; #endif #endif #ifdef _SUPERPOSITIONEXAMPLE_ double tempPos = 0; for(int i = 0; i <= finalPosition/stepSize; i++){ tempPos = i*stepSize; for(int j = 0; j < 3; j++){ if(0 == j) numTerms = 5; else if(1 == j) numTerms = 20; else numTerms = 100; *fileArray[j] << tempPos << " " << waveApprox(tempPos, initialVelocity, initialTime, numTerms) << endl; } } #endif #ifdef _PROPNONDISPERSIVE_ #endif cout << "That's all folks!\n"; return 0; } double waveApprox( double position, double velocity, double time, int numTerms){ double solution = 0; int termCount = 0; for (int i = 1; i <= 2*numTerms; i++){ if( 0 == ((i+1) % 2)){ solution += (1/(double)i) * sin(i * position - i * velocity * time); termCount++; } }// Don't forget to add the other term and multiply by the weight factor solution *= (4/PI); solution += (4/PI); #ifdef _DEBUG_ cout << "Number of terms: " << termCount << endl; #endif return solution; }