include iostream include math include fstream define _DEBUG_ define _I

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <math.h>
#include <fstream>
//#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;
}